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.
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 line0
- move cursor to the start of the lineH
- move to the top of the terminal windowM
- move to the middle of the terminal windowL
- move to the bottom of the terminal windowe
- move to the end of the wordw
- move to the start of the next wordb
- move to the start of the previous wordG
- move to the end of the filegg
- move to the start of the fileh
- move one place to the leftl
- move one place to the right(
- move to the start of the previous sentence)
- move to the start of the next sentenceCtrl + f
- move a page downCtrl + b
- move a page upj
- move down one linek
- 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 actionCtrl + r
- redo last undo.
- repeat last actionyy
- copy the whole lineyw
- copy selected wordy
- copy selected area (seev
andV
for selecting an area)y$
- copy the character selected by the cursor and everything after it on the line.p
- paste copied itemv
--- VISUAL --
select mode - use theLEFT
,RIGHT
,UP
andDOWN
arrows to control what is selected. PressESC
to exitV
- select the whole line in-- VISUAL --
mode.d
- delete selected textdd
- delete the whole linedw
- delete the selected wordD
- 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 commandrange
- this is where within the file the find and replace should be applied. There are two options forrange
:%
- the entire filestart _line
,end_line
- between a start line and an end line
search_string
- the string to findreplace_string
- what to replace the search string withmodifier
- what to do when a match is found. There are three options:g
- replace all without confirminggc
- ask for confirmation before replacing each matchgn
- 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.