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.
- Opening a file in vim
- Moving
- Inserting Text
- Editing Text
- Cutting, Copying And Pasting
- Marking Text (Visual Mode)
- Visual Commands
- Search in File
- Saving and Exiting File
- Enabling Vim Color Schemes
$ vim <file path>
Example:
$ vim './myRepo/README.md'
or
$ vim README.md
The basic keys for moving the cursor by one character are:
h
– move the cursor leftj
– move the cursor downk
– move the cursor upl
– move 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.
b
– move to the start of a wordB
– move to the start of a tokenw
– move to the start of the next wordW
– move to the start of the next tokene
– move to the end of a wordE
– move to the end of a token
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)
Ctrl + b
– move back one full screenCtrl + f
– move forward one full screenCtrl + d
– move forward 1/2 a screenCtrl + u
– move back 1/2 a screenCtrl + e
– move screen down one line (without moving the cursor)Ctrl + y
– move screen up one line (without moving the cursor)Ctrl + o
– move backward through the jump historyCtrl + i
– move forward through the jump history
H
– move to the top of the screen (H=high)M
– move to the middle of the screen (M=middle)L
– move to the bottom of the screen (L=low)
i
– switch to insert mode before the cursorI
– insert text at the beginning of the linea
– switch to insert mode after the cursorA
– insert text at the end of the lineo
– open a new line below the current oneO
– open a new line above the current oneea
– insert text at the end of the wordEsc
– exit 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.
r
– replace a single character (and return to command mode)cc
– replace an entire line (deletes the line and moves into insert mode)C
/c$
– replace from the cursor to the end of a linecw
– replace from the cursor to the end of a words
– delete a character (and move into insert mode)J
– merge the line below to the current one with a space in between themgJ
– merge the line below to the current one with no space in between themu
– undoCtrl + r
– redo.
– repeat last command
-
yy
– copy (yank) entire line -
#yy
– copy the specified number of lines -
dd
– cut (delete) entire line -
#dd
– cut the specified number of lines -
p
– paste after the cursor -
P
– paste before the cursorReplace # by number
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.
v
– select text using character modeV
– select lines using line modeCtrl+v
– select text using block mode
Once you have enabled one of the modes, use the navigation keys to select the desired text.
o
– move from one end of the selected text to the otheraw
– select a wordab
– select a block with ()aB
– select a block with {}at
– select a block with <>ib
– select inner block with ()iB
– select inner block with {}it
– select inner block with <>
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:
y
– yank (copy) the marked textd
– delete (cut) the marked textp
– paste the text after the cursoru
– change the market text to lowercaseU
– change the market text to uppercase
*
– jump to the next instance of the current word#
– jump to previous instance of the current word/pattern
– search forward for the specified pattern?pattern
– search backward for the specified patternn
– repeat the search in the same directionN
– repeat the search in the opposite direction
:w
– save the file:wq
/:x
/ZZ
– save and close the file:q
– quit:q!
/ZQ
– quit without saving changes:w
new_file_name – save the file under a new name and continue editing the original:sav
– save the file under a new name and continue editing the new copy:w !sudo tee %
– write out the file using sudo and tee command
:colorscheme [colorscheme_name]
– change to specified scheme:colorscheme [space]+Ctrl+d
– list available Vim color scheme