List of practical Vim commands

April 24, 2021

Vim is a text editor that runs on a console or terminal windows.
Vim was created based on the Vi text editor. At the time Vi was created there were no graphical user interfaces which is why it relied on keyboard shortcut to perform the editor tasks.


There are several modes in which Vim operate:

  • The default or normal mode where you use the keyboard to navigate through the file.
  • The insert mode where you can type the content of the file.
  • The visual mode where you use the keyboard to select portions of the file
  • The command mode where you can run commands

List of Vim commands


Move cursor


h           Move left
j           Move down
k           Move up
l           Move right

0           Move to the beginning of line
$           Move to the end of line

w           Move to the next word
b           Move to the previous word
e           Move to the end of the next word

<enter>     Move cursor to next line
+           Move cursor to next line
-           Move cursor to previous line

H           Place cursor on top of screen
M           Place cursor on middle of screen
L           Place cursor on bottom of screen

^G          Show filename and current line
<nnn>G      Go to line <nnn>
gg          Go to top of file
G           Go to bottom of file

Move page


^F          Page forward 
^B          Page backward
^D          Half Page down 
^U          Half Page up

z <enter>   Place current line to top of screen
z.          Place current line to center of screen
z-          Place current line to bottom of screen

^E          Page down one line 
^Y          Page up one line 

Edit mode


i           Insert
a           Insert (at next character position)

o           Open new line below
s           Substitute character

c           To change a character
cw          Change word
c2b         Change two previous words
c$          Change text up to end of sentence
c0          Change text from the beginning of sentence

cc          Change current line

Save / Quit


:w <file_name>      Write file as <file_name>
:w                  Write file
:q                  Quit file

Delete / Copy / Paste


r           Replace character (without entering Edit mode)

d           To delete a character
dw          Delete word
db          Delete previous word

dd          Delete line
2dd         Delete two lines
x           Delete character

p           Paste copied or deleted text
y           To copy a character
yw          Copy word
y$          Copy text up to end of sentence

yy          Copy line
4yy         Copy four lines

J           Join lines

u           Undo changes

Search


/           Search forward
?           Search backwards
n           Repeat search
N           Repeat search in opposite direction

f<x>        Find <x> character in current line
F<x>        Find <x> character in current line, search backwards
t<x>        Find <x> character in current line, place cursor in previous character
T<x>        Find <x> character in current line, search backwards, place cursor in next character
;           Find next
,           Find next, backwards

Replace


:s/<old>/<new>/               Substitute first occurrence of string <old> with <new>, in current line
:s/<old>/<new>/g              Substitute all occurrences of string <old> with <new>, in current line

:50,100s/<old>/<new>/g        Substitute string in the specified range only
:1,$s/<old>/<new>/g           Substitute string in the specified range only
:%s/<old>/<new>/g             Substitute string in the entire file
:%s/<old>/<new>/gc            Substitute string in the entire file, ask for confirmation

:g/<pattern>/s/<old>/<new>/g  Find lines that contains the "pattern", 
                              substitute string in those lines
:g/<pattern>/s//<new>/g       Find lines that contains the "pattern", 
                              substitute the "pattern" with <new> in those lines 

Bookmarks


m<x>        Set bookmark labeled with character <x>
'<x>        Go to bookmark <x>, at the beginning of the line
`<x>        Go to bookmark <x>, at the cursor position
''          Go to the next bookmark, at the beginning of the line
``          Go to the next bookmark, at the cursor position


Editing more than one file


:hide edit example.txt       Open file "example.txt"

:buffers                     Show opened files
:ls                          Show opened files

:buffer <n>                  Switch to file in buffer number <n>
:buffer <filename>           Switch to file <filename>
:buffer <part-of-filename>   Switch to file with the name that contains <part-of-filename>

:bnext                       Switch to next file
:bprevious                   Switch to previous file
:bfirst                      Switch to first file
:blast                       Switch to last file

:bdelete <n>                 Close file in buffer number <n>
:bdelete <filename>          Close file <filename>
:bdelete <part-of-filename>  Close file with the name that contains <part-of-filename>