Useful Vim Commands for Navigating, Editing & Searching Files

Vim is a really powerful editor for the Linux command line and offers features that you would typically find on GUI-based code editors. The main challenge of using Vim effectively is knowing the keyboard shortcuts to perform different tasks and enter different modes.

 

In this guide, we will go through some of the most useful Vim commands for editing files. If you don't know how to open a file in Vim check out my tutorial on how to open, save files and quit Vim.

 

How to Enter Insert Mode

There are two main modes in Vim, insert and command. Insert mode is used for inserting text VIA pasting or typing. To enter insert mode press the i key in the editor; you will see -- INSERT -- appear at the bottom left corner indicating you are in insert mode.

 

insert mode

 

To exit insert mode press the ESC key.

 

How to Enter Command Mode

The command mode is where we can utilise the useful command utilities Vim has to offer. When opening a file you will already be in command mode, though if you are in a different mode press the ESC key.

 

Vim Movement Commands

Moving the cursor in basic command line text editors can be super annoying as the movements are slower. In Vim we can use the following movement commands to make navigating the cursor much quicker.

 

  • $ - move to the end of the line
  • 0 - move cursor to the start of the line
  • H - move to the top of the terminal window
  • M - move to the middle of the terminal window
  • L - move to the bottom of the terminal window
  • e - move to the end of the word
  • w - move to the start of the next word
  • b - move to the start of the previous word
  • G - move to the end of the file
  • gg - move to the start of the file
  • h - move one place to the left
  • l - move one place to the right
  • ( - move to the start of the previous sentence
  • ) - move to the start of the next sentence
  • Ctrl + f - move a page down
  • Ctrl + b - move a page up
  • j - move down one line
  • k - move up one line
  • } - move to the next block of text
  • { - move to the previous block of text
  • # - type a line number to move to that line

 

Vim Edit Commands

Vim provides functionality to copy, paste, delete, select and undo/redo. The main thing to note is that Vim uses a different set of commands to achieve the edits than what you are probably used to using on a Linux/Mac/Windows GUI. A command like yy means you press y and y immediately after.

 

  • u - undo the last action
  • Ctrl + r - redo last undo
  • . - repeat last action
  • yy - copy the whole line
  • yw - copy selected word
  • y - copy selected area (see v and V for selecting an area)
  • y$ - copy the character selected by the cursor and everything after it on the line.
  • p - paste copied item
  • v - -- VISUAL --  select mode - use the LEFT, RIGHT, UP and DOWN arrows to control what is selected. Press ESC to exit
  • V - select the whole line in -- VISUAL -- mode.
  • d - delete selected text
  • dd - delete the whole line
  • dw - delete the selected word
  • D - delete the character selected by the cursor and everything after it on the line.
  • d0 - delete the character selected by the cursor and everything before it on the line.
  • x - single character delete

 

Note - copied items using one of the Vim y commands will be only available for pasting back into a Vim window.

 

Vim Search Command

Vim provides powerful search functionality. This includes the ability to quickly repeat the previous search when scanning through a large file and pattern/match replacing.

 

To search for a string in Vim type / (forward slash) followed by the string you are looking for. Press ENTER to execute the search.

 

/keyword

 

You can also search up the document by typing ? followed by the string.

 

?keyword

 

n - repeats the search in the same direction as your previous search command.

N - repeats the search in the opposite direction.

 

Search and Replace in Vim

Firstly let's have a look at the syntax for matching a pattern and replacing it in Vim.

 

:<range> s/<search_string>/<replace_string>/<modifier>

 

  • : - a colon initiates the command
  • range - this is where within the file the find and replace should be applied. There are two options for range:
    • % - the entire file
    • start _line,end_line - between a start line and an end line
  • search_string - the string to find
  • replace_string - what to replace the search string with
  • modifier - what to do when a match is found. There are three options:
    • g - replace all without confirming
    • gc - ask for confirmation before replacing each match
    • gn - just highlight the matches and don't replace anything

 

Let's try out some examples considering the options shown above. Firstly we will find and replace every occurrence of foo with bar in a file.

 

note - these examples include : (colon) at the beginning; if you are pasting the command type : into Vim before you paste the rest of the command minus : for it to work.

 

:%s/foo/bar/g

 

Now let's perform the same command except this time we will restrict the range between line 50 and 100.

 

:50,100 s/foo/bar/g

 

We will now replace foo with bar in the file, this time with the gc modifier. gc will provide a confirmation for each match within which we have several options available to choose the action to take.

 

:%s/foo/bar/gc
replace with bar (y/n/a/q/l/^E/^Y)?

 

Here are the options you have for handling the match confirmation:

 

  • y – allow performing the change.
  • n – disallow performing the change.
  • a – substitute all.
  • q – quit the task.
  • l – substitute this occurrence only, then quit.
  • ^E (CTRL + E) – scroll up a screen
  • ^Y (CTRL + Y) – scroll down a screen

 

Conclusion

You now know how to manoeuvre around a file effectively, edit its contents and search for strings and replace them if needed in Vim. This should have you covered to the point where you can competently use Vim, though with it being such as a comprehensive program there will always be new things to learn and make use of.