Skip to main content

VIM-Editor CheatSheet for Developers

Introduction

Vim is a widely used, open-source Unix text editor. Learning to use Vim commands is a matter of practice and experience. That is why it is handy to have a helpful reference sheet while mastering them.

How to open a file in vim

CommandsDescriptionsExamples
$ vim <file path>Open a file in Vim Editor- $ vim './myRepo/README.md'
or
- vim README.md

Moving by Characters, Words and Tokens

The basic keys for moving the cursor by one character are:

CommandsDescription
hmove the cursor left
jmove the cursor down
kmove the cursor up
lmove the cursor right

You can also use these keys with a number as a prefix to move in a specified direction multiple times. For example, if you run 5j the cursor moves down 5 lines.

CommandsDescription
bmove to the start of a word
Bmove to the start of a token
wmove to the start of the next word
Wmove to the start of the next token
emove to the end of a word
Emove to the end of a token

Moving by Lines

CommandsDescription
0 (zero)jump to the beginning of the line
$jump to the end of the line
^jump to the first (non-blank) character of the line
#G / #gg / :#move to a specified line number (replace # with the line number)

Moving by Screens

CommandsDescription
Ctrl + bmove back one full screen
Ctrl + fmove forward one full screen
Ctrl + dmove forward 1/2 a screen
Ctrl + umove back 1/2 a screen
Ctrl + emove screen down one line (without moving the cursor)
Ctrl + ymove screen up one line (without moving the cursor)
Ctrl + omove backward through the jump history
Ctrl + imove forward through the jump history

CommandsDescription
Hmove to the top of the screen (H=high)
Mmove to the middle of the screen (M=middle)
Lmove to the bottom of the screen (L=low)

Inserting Text

CommandsDescription
iswitch to insert mode before the cursor
Iinsert text at the beginning of the line
aswitch to insert mode after the cursor
Ainsert text at the end of the line
oopen a new line below the current one
Oopen a new line above the current one
eainsert text at the end of the word
Escexit insert mode; switch to command mode

Some of these commands switch between command and insert mode. By default, Vim launches in command mode, allowing you to move around and edit the file. To switch to command mode, use the Esc key.

On the other hand, the insert mode enables you to type and add text into the file. To move to insert mode, press i.

Editing Text

CommandsDescription
rreplace a single character (and return to command mode)
ccreplace an entire line (deletes the line and moves into insert mode)
C / c$replace from the cursor to the end of a line
cwreplace from the cursor to the end of a word
sdelete a character (and move into insert mode)
Jmerge the line below to the current one with a space in between them
gJmerge the line below to the current one with no space in between them
uundo
Ctrl + rredo
.repeat last command

Cutting, Copying And Pasting

CommandsDescription
yycopy (yank) entire line
#yycopy the specified number of lines
ddcut (delete) entire line
#ddcut the specified number of lines
ppaste after the cursor
Ppaste before the cursor

Replace # by number

Marking Text (Visual Mode)

Apart from command mode and insert mode, Vim also includes visual mode. This mode is mainly used for marking text.

Based on the chunk of text you want to select, you can choose between three versions of visual mode: character mode, line mode, and block mode.

CommandsDescription
vselect text using character mode
Vselect lines using line mode
Ctrl+vselect text using block mode

Once you have enabled one of the modes, use the navigation keys to select the desired text.

CommandsDescription
omove from one end of the selected text to the other
awselect a word
abselect a block with ()
aBselect a block with {}
atselect a block with <>
ibselect inner block with ()
iBselect inner block with {}
itselect inner block with <>

Visual Commands

Once you have selected the desired text in visual mode, you can use one of the visual commands to manipulate it. Some of them include:

CommandsDescription
yyank (copy) the marked text
ddelete (cut) the marked text
ppaste the text after the cursor
uchange the market text to lowercase
Uchange the market text to uppercase

Search in File

CommandsDescription
*jump to the next instance of the current word
#jump to previous instance of the current word
/patternsearch forward for the specified pattern
?patternsearch backward for the specified pattern
nrepeat the search in the same direction
Nrepeat the search in the opposite direction

Saving and Exiting File

CommandsDescription
:wsave the file
:wq / :x / ZZsave and close the file
:qquit
:q!/ ZQquit without saving changes
:w new_file_namesave the file under a new name and continue editing the original
:savsave the file under a new name and continue editing the new copy
:w !sudo tee %write out the file using sudo and tee command

Enabling Vim Color Schemes

Vim Color SchemesDescription
:colorscheme [colorscheme_name]change to specified scheme
:colorscheme [space]+Ctrl+dlist available Vim color scheme