Useful Vim Commands

superior_hosting_service

100 Useful Vim Commands Daily Use

vim-editor.png

No matter whether you’re a sysadmin by profession or just an old school tech enthusiast like me, if you ever find yourself in love with the awe-inspiring Unix OS, chances are you’re gonna feel the need for a versatile terminal-based Linux text editor at one point or another. For today’s guide, we’ll keep our focus on Vim, considered the best by many, if not all, as the best Linux code editor ever. So, let’s just skip on all those holy wars among you Vim vs. Emacs pundits and just keep our eye’s on the 100 best Vim commands, today’s topic. Emacs fanatics, however, please don’t get upset; we’ll cover that extensible editor of yours definitely in some time future.

Vim Commands That Will Turn You into A Vim Guru


Below, we’re presenting the 100 most useful Vim commands that will come in handy during your time with Vim. As we’re aiming mainly at beginner Vim users, don’t get daunted all you seasoned Vim masters. This guide will enhance the Vim experience of new users and prove to be a valuable reference to more mature users.

Linux Command Cheat Sheet for Exploring the Vim Modes


Before delving directly into the commands this guide is meant for, we want to draw your attention to Vim modes. Vim features two distinct modes, a command mode and an insert mode.

When you open Vim for the first time, you’re in the command mode. The command mode lets you navigate the screen, delete and copy text, among many others. The insert mode, on the other hand, as implied, lets users insert text into Vim’s console. You can use the ESC key to switch back to the command mode after finishing typing.

vim_cheat_sheet.png
  • i

Pressing i in the console gets you to the insert mode. Now you can start typing your texts inside Vim. The text you type will appear in front of the cursor key.

  • a

It’s among another commonly used Vim commands you can use to insert text. The texts will be appended right after the cursor.

  • I

Want to place your texts at the beginning of the current line? The l command lets you insert text right at your desired location.

  • A

This is one of my favorite Vim commands to append text at the end of my current line. It also falls under those Vim commands used for inserting text.

  • o

Pressing the lowercase o creates a new line just after the current line you’re in.

  • O

The uppercase variant creates the new line before the current line.

  • gf

You can use this handy little command to open your files under the cursor.

  • gi

One of my most favorite Vim commands, gi restarts the insert mode in the last insert point you used.

  • wq

Typing in long strings of text doesn’t matter if you can’t save them for future usage. Enter the wq command in the command mode to save your file and exit Vim.

  • q!

You’ll often find yourself in a position where you’re not satisfied with the texts you have typed and would like to close the editor without saving your content. Entering q! in the command mode lets you do exactly so.

Vim Commands for Moving Around The Console


One of the foremost reasons behind Vim’s ultra-fast user experience is its fast movement features. Vim features and supports numerous convenient Vim commands to make your life in front of the terminal blazing fast. Below are some of the most useful Vim commands for faster navigation.

vim-commands.png
  • h

Use the h command to navigate to the left side of your Vim console. This is analogous to the left arrow of your keyboard.

  • l

The l commands help you navigate to the right and is a substitute for the right arrow key.

  • k

Use the lowercase k command to move upwards. Similar to the arrow up key.

  • j

The j command takes you to downward and is a substitute for the down arrow key.

The h j k l keys help you move across the screen much faster than the standard arrow keys. Prefix them with a number to get the most out of these Vim commands. So, for going down ten lines, instead of holding the key for a prolonged period of time, just type 10j. Mastering these passive Vim commands for navigation will enhance your productivity much faster.

  • H

This command takes the cursor at the topmost region of your screen.

  • M

It will put the cursor in the middle of your Vim screen.

  • L

The capital L puts the cursor at the end of the screen.

The H M L Vim commands come in handy whenever you want to get to an approximate portion of your console screen.

  • 0 or <HOME>

Entering either 0 or the <HOME> button will take you to the start of the line.

  • ^

This is among the most commonly used Vim commands to get to the first non-blank character in a line.

  • $

This command will take the cursor at the end of the current line.

  • b

The lowercase b command lets you move back by tokens.

  • w

This command helps you move forward by tokens.

  • B

The uppercase variant of b lets you move back by words.

  • W

This command will let you move forward by words.

A token is any sequence of digits, letters, or underscores, whereas a word is any non-blank sequence separated by whitespace.

  • ctrl+u

This Vim command is basically a Page Up command. However, it moves the screen by half and holds onto the current cursor position.

  • ctrl+d

The Page Down variant of the above command. Functions in the same way.

  • G

Prefix the G command with a number to jump directly to a specific line number. Suppose, if you have line numbering enabled and are writing a shell script, you can go to the 10th line by merely typing 10G.

  • #

Given, # is the number of a specific line, entering this command will take you to that line directly. It’s one of the most used Vim commands to jump from line to line.

This command (two back ticks) takes you wherever you just were.

  • )

You can go-to at the start of the following sentence with this command.

  • (

Go-to at the beginning of the earlier sentence.

  • }

Used for going to the beginning of the next text block.

  • {

Takes cursor at the beginning of the previous text block.

Vim Commands for Editing Texts


Vim combines many powerful yet flexible editing features that let users edit their documents seamlessly while paving the path to a versatile workflow. The power of Vim comes from the fact that you can combine many of such Vim commands to solve your custom needs. This section of the guide covers some elementary Vim commands used in everyday text editing. Continue reading to find out more useful Vim commands for editing.

vim_text_editing.png
  • yy

The yy command lets you copy an entire line. It’s something you’ll often employ during your time with Vim.

  • yw

This is one of the most widely used Vim commands for copying a word across the editor.

  • y$

One of my all-time favorite Vim commands, it gives users the ability to copy texts from the current position of the cursor to the end of a line.

  • v

The v command can be used to highlight a single character at a time in combination with the movement keys (arrows/ h j k l).

  • V

Almost the same as the above one, but instead of a word, this command will highlight an entire line.

  • p

You can paste the contents of your clipboard to the unnamed register with this handy little command.

  • d

This is among the most used Vim commands for deleting highlighted texts.

  • dd

Want to delete an entire line with a single Vim command? The dd command is specially created for this purpose.

  • dw

You can delete a single word pretty quickly with the dw command.

  • D

One of the most powerful Vim commands ever, the D command deletes everything from the current cursor location to the end of the line.

  • d0

This command is used for deleting everything from your cursor’s current position to the beginning of the line.

  • dgg

You can use this command to delete everything from your cursor’s current position to the beginning of the file.

  • dG

This command will wipe out everything from your cursor’s current position to the end of the file.

  • x

Use the x command whenever you need to delete a single character.

  • u

The u command is among the most widely used Vim commands for many to undo the last operation. Combining with a postfix allows users to undo multiple actions. So, u# undo’s the last # number of actions.

  • ctrl+r

Use the above command to redo the last undo operation.

  • .

The dot (.) command is one of those useful Vim commands that significantly decrease your workload by repeating the last action whenever you need such functionality.

  • cc

You can use the cc command to change line(s) by clearing and entering the insert mode simultaneously. The cursor is placed at the current level of indentation.

Useful Vim Commands for Replacing Texts


Apart from the generic command and insert modes, Vim has another mode called the visual mode. You can highlight and change a particular portion of your text document in this mode. But where’s the fun in that, huh? Using a select set of Vim commands can achieve you the same result, just a bit faster. Below are some of my favorite Vim commands for changing texts on the fly.

  • r

The r command is a pretty handy tool when it comes to changing a single character. Follow it with <char>, and it will change the current character under the cursor with <char>.

  • R

The capital R opens the insert mode, but you can replace them with this command instead of inserting texts.

  • ~

The tilda(~) command is pretty useful when you need to change the case of a character in your document. Follow it with a number to flip that many characters.

  • t<char>

Enter t<char> to select up to but not including the next <char> on a particular line.

  • f<char>

Press f<char> to select up to and including the next <char> on a line.

  • i<char>

Want to select everything in between a set of parentheses or some other unique character? Type i<char> to select everything that sits between two consecutive <char>.

  • a<char>

This command is identical to the previous one but includes the <char> at both ends of the text.

Most Used Vim Commands for Searching Through A Document


The searching functionality is of greater importance to any versatile Linux text editor. Vim offers exceptionally powerful commands for searching through a document. It’s one of the most powerful areas of this innovative text editor. Glance over the below Vim commands to search through your document effectively.

vim–search.jpg
  • /

The slash command is the single most used command for searching through huge text files in Vim. Just type / and follow it with the texts you want Vim to search for you and look out at the console’s bottom corner.

  • /\c

The \c option, when piped to the search (/) command, lets users search for texts in a case sensitive manner. Wise utilization of this command can save hours of hard work.

  • ?<pattern>

This is among the most useful Vim commands for searching previous texts for a given <pattern>.

  • n

The n command searches in the direction of your last search. Use this command if you know in which direction your search item is.

  • N

Almost identical to the above command, but searches in the opposite direction of your last search.

  • :%s/[pattern]/[replacement]/g

The above command utilizes regular expression for searching every occurrence of [pattern] and replaces it with [replacement] without confirmation.

  • :%s/[pattern]/[replacement]/gc

Same as the previous command, but asks for confirmation before replacing each instance of [pattern] with [replacement].

  • :s/[pattern]/[replacement]/g

Instead of replacing every single instance of [pattern] on your file, this Vim command will replace only those [pattern] that are in the current line with [replacement].

  • :bufdo /[pattern]

This is among those powerful Vim commands that let users search for [pattern] in every currently opened buffer. It will enhance your productivity and decrease search time significantly.

  • :g/string/d

This is among those useful Vim commands that will come in handy whenever you want to delete every line that contains string from your document.

Linux Command Cheat Sheet for Working With Multiple Files in Vim


Editing in the terminal is fun and alright. But, where’s the edge of terminal editors if you can’t edit multiple files with them. Luckily, as an iconic text editor, Vim lets you work with multiple documents in a pretty neat way. The ability to split the screen when working with multiple files simultaneously is something Vim has been offering even before the concept of the split-screen became mainstream. Now, you should slowly start to realize why this legendary editor is hailed in such high regard.

Multiple_Files_in_Vim.png
  • :sp [filename]

Use this command to create a new file and split the console screen horizontally to show the two different buffers.

  • :vsp [filename]

The functionality of this Vim command is, in essence, identical to the above command, but instead of splitting the console horizontally, it splits the screen vertically.

  • :bn

This Vim command will switch your editor to the next buffer. It’s among those few fundamental Vim commands without which you won’t be able to work with multiple documents in Vim.

  • :bp

Identical to the earlier command, but switches to the previous buffer instead of going next.

  • :bd

Use this Vim command when closing a specific buffer. Make sure to save your data using appropriate Vim commands.

  • :ls

This is one of the handy Vim commands that will present users with a list of all open buffers.

  • ctrl+ws

If you want to split your Vim windows horizontally, then this is the command you’re looking for.

  • ctrl+wv

Instead of splitting the windows horizontally, this Vim command will split it vertically.

  • ctrl+ww

Utilize this command to switch between multiple windows directly from the command mode.

  • ctrl+wq

You can use this handy Vim command to quit a particular window.

  • ctrl+wh

This command will move the location of your cursor to the left window.

  • ctrl+wl

Same as the previous command, but instead of moving the cursor left, this command will point to the right window.

  • ctrl+wj

Use this command whenever you want to move a window beneath your existing one.

  • ctrl+wk

Same as above, but takes the cursor to the window above the current one.

Useful Vim Commands When Working With Multiple Tabs


Vim offers a variety of Vim commands to open and navigate a plethora of tabs right from the terminal. You can use these useful Vim commands whenever you are working with multiple files simultaneously. We encourage new Vim users to glance over such commands as they will often come in handy after you’ve mastered the basic Vim commands and start to do heavyweight staff with it.

Vim_With_Multiple_Tabs.png
  • :tabnew

You can use the :tabnew command to create a new tab and work with some other document while not leaving the current file.

  • gt

The gt command will show you the next tab opened by you.

  • :tabfirst

The above command will show you the first tab you’ve got opened in a particular session.

  • :tablast

The previous command, but instead of showing the first tab, will show the last tab.

  • tabm n(position)

This powerful Vim command will come in handy whenever you feel the need to re-arrange your existing tabs.

  • tabdo %s/foo/bar/g

You can utilize the above command whenever you want to execute a command in every open tab at the same time.

  • :tab ball

This Vim command is one of my favorite Vim commands, and it puts each open files in tabs.

  • :new abc.txt

This is among those Vim commands that lets you open a new file named abc.txt in a new window without exiting the current document.

Miscellaneous Vim Commands


As Vim offers plenty of Vim commands, we cannot cover every one of them categorically. That’s why in this section, we’ll overview some useful Vim commands that are used by veteran users for a wide array of purposes.

  • :w

Pressing this Vim command in the command mode saves your current document but does not exist in the existing session.

  • :q

This command will exit the current session without saving your changes. Note that you will see the E37 error if you have unsaved changes in your document. In such scenarios, you need to override this command and use q! instead.

  • :help [command]

The help command performs a search operation on the command you’ve typed and shown relevant information on the right in the console.

  • :e [file]

This command will open a file named [file] and create a new one if it doesn’t exist in your filesystem already.

  • :w [filename]

Use this command to save your existing document directly into a new file named [filename].

  • :stop

Writing this command in the command mode will suspend your current Vim session. You can also do this by pressing ctrl+z at the same time.

  • :browse e

Use this command whenever you want to invoke the graphical file explorer from your Vim console.

  • :%!fmt

Writing this command will align each line of your current file.

  • !}fmt

Use this whenever you need to align all lines at the present position of your cursor.

  • :set autoindent

This is one of the most used Vim commands you will use during your time with Vim. It sets autoindent for your current session.

Vim has reached the massive popularity it enjoys today among veteran Unix users not only because of its powerful editing capabilities but also for the vast array of versatile commands it offers. Mastering such useful Vim commands is not child’s play, and we strongly suggest you do not try to memorize each of them individually. Instead, try to pick a few commands you can use right away and apply those directly in your next editing session. Only by practically using them you will be able to master these different Vim commands. Hopefully, this guide fulfills your intro to Vim and provides the insights you were looking for.