Introduction
Vim is a widely used open source Unix text editor. Learning how to use Vim commands is a matter of practice and experience. That’s why it’s helpful to have a useful cheat sheet while mastering them.
In this tutorial, you will find the most important Vim commands as well as a downloadable cheat sheet.
Move
within a file
You can move the cursor within a file by individual characters, words, tabs, 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 white space and can include punctuation.
In addition, you can move to different parts of a text per screen view.
Move through characters, words, and tabs
The basic keys for moving the cursor in a character are: h – move the cursor to the left j – move the cursor down k – move the cursor
- up
- l –
- to the right You can also use these keys with a number as a prefix to
move the cursor
move
in a specific direction multiple times.
For example, if you run 5j, the cursor moves down 5 lines.
b – move to the beginning of a word B – move to the beginning of a token W – move to the beginning of the next word W – move to the beginning of the next token e – move to the end of a
- word
- E – move to the end of
- a
token
For example, 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-s tep”. However, if you use B, the cursor moves completely back to: “step-by-step” since there is no white space between these characters.
Move through lines
0 (zero) – jump to the beginning of the line$ – jump to the end of the
- line
- ^ – jump to the first (not blank) character of
- #G / #gg / :# – move to a specified line number (replace # with the line number)
the line
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 on the line).
For more information about pairings and how to use more pairs than the default supported pairs, run the following commands in the text editor: :h pairings.
Moving through
screens The following commands are used as a quick way to move within text without scrolling.
Ctrl + b – rewind a full screen Ctrl + f – forward a full screen Ctrl + d – forward 1/2 screen Ctrl + u – back 1/2 screen Ctrl + e – move the screen down one line (without moving the
- cursor)
- Ctrl +
- y – move the screen up one line (without moving the cursor
- Ctrl +
)Ctrl + o – move back through the jump history
i – move through the jump history H – move to the top of
the screen (H = high) M – move to the center of the screen (M = middle) L
- – move to the bottom of the screen (L = low
- )
Insert text
- i
- switch to insertion mode after cursorA
- O – open a new line above
- EA – insert text at the end of the
- Esc – Output insertion mode; switch to
– switch to insertion mode before cursor I – insert text at the beginning of line A –
– insert text at the end of line O – open a new line below the current
the current
word
command mode
Some of these commands switch between command and insert mode. By default, Vim starts 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, insert mode allows you to type and add text to the file. To enter insert mode, press i.
Text editing
r – replace a single character (and return to command mode)cc – replace an entire line (delete the line and go into insert mode)C / c$ – replace from the cursor to the end of a
- line cw – replace from the cursor to the end of a word s – remove a
- character (and switch to insert mode
- )
- J
- gJ – merge the bottom line with the current one with no space between them
- u – undo
- Ctrl + r – redo
- . – Repeat the last command
– merge the bottom line with the current one with a space between them
Cut, copy and paste
- and – copy (throw) the entire line
- #yy – Copy
- dd
- #dd – cut the specified number of lines
- cursor P – paste before cursor
the specified number of lines
– cut (delete) the entire line
p – paste after
Mark text (
visual mode)
In addition to command mode and insert mode, Vim also includes visual mode. This mode is mainly used for markup text.
Depending on the piece of text you want to select, you can choose from three versions of visual mode: character mode, line mode, and block mode.
V – select text using character mode V – select
- lines using
- Ctrl + V
line mode – select text using block
mode
Once you have enabled one of the modes, use the navigation keys to select the desired text.
- or – Move from one end of the selected text to the other
- aw
- ab
- iB
- {}it – select internal block with <>
– select a word
– select a block with () aB – select a block with {}at – select a block with <>ib – select internal block with ()
– select internal 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:
- and – throw (copy
- d – delete (cut)
- market text to uppercase
- – jump to
) the marked text
the marked text p – paste the text after the cursor u – change the market text to lowercase U – change the
Search in file *
the
- next instance of the current word
- # – jump to the previous instance of
- look back the specified pattern
- N – repeat the search in the opposite
the current word/pattern – search forward the specified pattern?pattern –
n – repeat the search in the same direction
direction
Save and exit
the file :
- w – save the file
- wq /😡/ZZ – Save and close
- Q! / ZQ – exit without saving changes
- continue editing the new copy:w !sudo tee % – write the file using sudo and
- tee command
- with
:
file:Q – Exit:
:w new_file_name – save the file with a new name and continue editing the original:sav – save the file with a new name and
Working
multiple files
:e file_name – open a file in a new buffer: bn – move to the next buffer:bp – return to previous buffer:bd – close buffer:b# – move to specified buffer (by number):
- b file_name – move to
- a buffer
- (by name)
- :
- ls – list all open buffers
- :vert ba – Edit all files as vertical windows:tab ba – edit all buffers as tabs gt –
:sp file_name – open a file in a new buffer and split the viewport horizontally:vs file_name – Open a file in a new buffer and split the viewport vertically
- move to next
- tab
- – move to the previous tab
gT
Ctrl+ws – split the viewport Ctrl+wv – split the viewport vertically Ctrl+ww – change viewports Ctrl+wq – exit a viewport Ctrl+wx – Swap the current viewport with the next Ctrl+
- – make all viewports equal in height and width
=
Marks and jumps
m[a-z] – mark text using character mode (from a to z)M[a-z
- ] –
- mark lines using line mode (from a to z)’a – jump to the position marked with a ‘y’a –
- pull the text
- .
- position where Vim last exited’‘
- jump to last jump:marks – list all marks:jumps – list of all hops:changes –
- list of all
- changes
- Ctrl+i
- Ctrl+o
- ,
- ; – move to the previous instance in the change list
to the position marked >a>’
– skip to the last change in file’0 – jump to the
–
– move to the next instance in the
jump list – move to the previous instance in the g jump list
– move to the next instance in the g change list
- Macros qa – record
- q – stop recording
- macros@a
- @ – run the last macro again
macro to
– run macro a@
Enabling
Vim Color schemes:color scheme [colorscheme_name] –
- change to specified scheme:
- [space]+Ctrl+d – available list Vim color scheme The list
color scheme
of color
schemes
Vim shows you the ones that come by default with the text editor, as in the image below:
You can also configure color settings manually or download user-created schemes. Find out how in How to change and use Vim color schemes.
Vim
Command Cheat Sheet
This article includes a one-page Vim command cheat sheet. Save the cheat sheet in PDF format by clicking the Download cheat sheet button below.
Conclusion
Knowing the basic Vim commands is useful since most Linux distributions have it installed by default. Once you get used to using Vim commands, mastering Vim should be simple.
Until then, keep a Vim cheat sheet handy.