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.
In this tutorial, you will find the most important Vim commands as well as a downloadable cheat sheet.

Moving Inside a File
You can move the cursor within a file by single characters, words, tokens, or lines.
According to Vim, a word can be a group of letters, numbers, and underscores. On the other hand, a token is anything separated by whitespace and can include punctuation.
Additionally, you can move to different parts of a text by screen view.
Moving by Characters, Words and Tokens
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
For instance, you have the noun phrase โstep-by-stepโ as part of a text and the cursor is placed at the end of it. The first time you press b, the cursor moves back to โstep-by-stepโ. However, if you use B, the cursor moves all the way back to: โstep-by-stepโ since there is no whitespace between these characters.
Moving by Lines
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)
To illustrate the difference between 0 and ^, take a look at the following example. In the first bullet, the command moves the cursor to the blank space before the bullet. On the other hand, in the third bullet, the ^ key moves the cursor to the hyphen (the first character in the line).

Note: To use the #G / #gg to jump to the wanted line, line numbering needs to be enabled. Check out how to show or hide line numbers in Vim/Vi.
To learn more about matchpairs and how to use more than the default supported pairs, run the following commands in the text editor: :h matchpairs.

Moving by Screens
The following commands are used as a quick way to move within the text without scrolling.
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)
Inserting Text
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.

Editing Text
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
Note: Bear in mind that Vim undoes and redoes changes by entries (changes made within one insert mode session). For more details, refer to the article How to Undo and Redo Changes in Vim.
Cutting, Copying And Pasting
yyโ copy (yank) entire line#yyโ copy the specified number of linesddโ cut (delete) entire line#ddโ cut the specified number of linespโ paste after the cursorPโ paste before the cursor
Note: Find more commands and options in How to Cut, Copy, and Paste in Vim/Vi.
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.
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 <>
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:
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
Search in File
*โ 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
Note: Searching in Vim/Vi is a task you will certainly perform often. Get to know all the search options in How To Search To Find A Word In Vim Or Vi Text Editor. To keep track of all the changes Vim shows how to use find and replace function.
Saving and Exiting File
: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
Note: Learn more about how to exit using Vim commands or shortcut keys with How To Exit (Quit) Linux Vim/Vi Editor.
Working with Multiple Files
:e file_nameโ open a file in a new buffer:bnโ move to the next buffer:bpโ go back to previous buffer:bdโ close buffer:b#โ move to the specified buffer (by number):b file_nameโ move to a buffer (by name):lsโ list all open buffers

:sp file_nameโ open a file in a new buffer and split viewport horizontally:vs file_nameโ open a file in a new buffer and split viewport vertically:vert baโ edit all files as vertical viewports:tab baโ edit all buffers as tabsgtโ move to next tabgTโ move to previous tab

Ctrl+wsโ split viewportCtrl+wvโ split viewport verticallyCtrl+wwโ switch viewportsCtrl+wqโ quit a viewport- Ctrl+wx โ exchange current viewport with next one
Ctrl+=โ make all viewports equal in height and width
Marks and Jumps
m[a-z]โ mark text using character mode (fromatoz)M[a-z]โ mark lines using line mode (fromatoz)`a- jump to position markeda`y`aโ yank text to position marked >a>`.โ jump to last change in file`0โ jump to position where Vim was last exited``โ jump to last jump:marksโ list all marks:jumpsโ list all jumps:changesโ list all changesCtrl+iโ move to next instance in jump listCtrl+oโ move to previous instance in jump listg,โ move to next instance in change listg;โ move to previous instance in change list
Macros
qaโ record macroaqโ stop recording macro@aโ run macroa@@โ run last macro again
Enabling Vim Color Schemes
:colorscheme [colorscheme_name]โ change to specified scheme:colorscheme [space]+Ctrl+dโ list available Vim color scheme
The list of Vim color schemes shows you the ones that come by default with the text editor, as in the image below:

You can also configure the color settings manually or download user-made schemes. Find out how to do so in How to Change and Use Vim Color Schemes.
Vim Commands Cheat Sheet
This article includes a one-page Vim commands reference sheet. Save the cheat sheet in PDF format by clicking the Download Cheat Sheet button below.

Conclusion
Knowing basic Vim commands is useful as most Linux distributions have it installed by default. Once you get use to using Vim commands, mastering Vim should be simple.
Until then, keep a Vim cheat sheet at hand.



