vim

Vim Keyboard Shortcuts and Commands

Vim is a feature-rich and fully customizable text editor with hard-to-remember countless commands and key bindings. Vim users usually forget many commands and shortcuts, and they need a quick reference to recall them back.

In this quick guide, I will be listing down the common Vim shortcuts that every Vim user must have in their pockets.

Mode Switching

Vim has multiple modes, that are important for editing, such as normal mode for key binds and commands, insert mode to type the text, and visual mode to select the text.

Insert Mode

i To insert text before the cursor
I To insert text at the start of the line
a To append text after the cursor
A To append text after the line
o To insert text to a new line – below
O To insert text to a new line – up

Normal Mode

Esc To enter the normal mode
: To type the command in normal mode

Visual Mode

v To enter the visual mode
V To enter the visual line mode
Ctrl+v To enter the visual block mode
aw Select a word
ab Select a block with ()
aB Select a block with {}
at Select a block with tags <>
ib Select inner block with ()
iB Select inner block with {}
it Select inner block with tags <>

Replace

R To enter the replace mode

Opening File

The file in Vim can be opened by using the vim command in terminal. However, if you are already in the buffer, then :edit command is used with the file name in normal mode.

:e <filename> To open the file in the current buffer
:v <filename> To open the file in the read-only mode

Note that the :e <filename> and :v<filename> commands can also be employed using only :edit <filename> and :view <filename> commands, respectively.

Writing File

The Vim editor does not automatically save the file unless you write it with the command.

:w To save changes in the current buffer

Vim saves file changes to a swap file after 4 seconds or if 200 characters have been typed. If you accidentally close the session, you will get a recovery prompt.

Exiting File

There are various commands and key bindings to exit the file in the Vim editor.

:q To quit the current buffer if no changes have made in the file
:q! To quit the current buffer without saving
:qa To close all files
:wq or 😡 To save the changes and quit the current buffer
ZZ To save changes and quit the current buffer
ZQ To quit the current buffer without saving the changes

If you have created the file in the current buffer without the name, then you will be prompted to enter the file name before exiting the Vim editor.

Motion

Though many modern features have blended into the vanilla Vim, from the core it is a keyboard-oriented text editor. The movement in the Vim is mainly done through key bindings.

The movement of the cursor in Vim by a character, line, word, or sentence is mentioned below.

Character and Line

h Move left by a character
j Move down by a character
k Move up by a line
l Move right by a line
^ Move to the first character of the line skipping white space
$ Move to the end of the line
0 Move to the beginning of the line, including whitespace
gg Move to the first line of the file, excluding whitespace
G Move to the last line of the file, excluding whitespace
+ Move to the next line, excluding whitespace
Move to the previous line, excluding whitespace
fs *Move to the next s in the current line
Fs Move to the previous s in the current line
ts Move till the next s in the current line
Ts Move till the previous s in the current line

*Use ; (forward) and , (backward) to repeat the f, F, t, and T movements.

Word

w Move forward by a word (to the first letter)
b Move backward by word (to the first letter)
e Move forward by a word (to the last letter)
ge Move backward by a word (to the last letter)

Sentence and Paragraph

) Move to the next sentence (beginning)
( Move to the previous sentence (beginning)
} Move to the next paragraph (beginning)
{ Move to the previous paragraph (beginning)

Programming

% Toggle between brackets or comments such as (), {}, [] or #
gd Go to the local variable
gD Go to the global variable

Page

Ctrl+b Page Up
Ctrl+f Page Down
Ctrl+u Page Up (half)
Ctrl+d Page Down (half)

Screen

zz Move the current line to the top of the screen
zt Move the current line to the center of the screen
zb Move the current line to the end of the screen
H Move the cursor to the top of the screen
M Move the cursor to the center of the screen
L Move the cursor to the bottom of the screen
Ctrl+e Move down the screen by one line
Ctrl+y Move up the screen by one line

Copy Cut & Paste

In any text editor or word processor, copy, cut, and paste are core functionalities. In Vim, it is all done through commands.

y To yank a character or selection
d To delete a character or selection
x To delete a character
s To delete a character and enable the insert mode (s is substitute)
S To delete a line and enable the insert mode
C To delete a line from cursor position and enable the insert mode
cc To delete a line and enable the insert mode
yy To yank a line
dd To delete a line
“*y To yank to system clipboard

The y and d operators in Vim are akin to copy and cut, respectively. These operators can be used with count and motion commands. For example, to copy the current line and 2 lines below the current line use y2j, and to copy 3 lines use 3yy.

To paste the yanked or cut text, the :put command or p is used. The p command also accepts count and motion.

p To paste the text after the cursor
P To paste the text before the cursor
“*p To paste from the system clipboard

To paste the current yanked line 4 times, use 4p.

Undo & Redo

To undo and redo the changes, there are dedicated commands in Vim.

u To undo a change
U Undo the last change in a line
Ctrl+r To redo the last change

Search

Searching becomes important when editing large files. Vim provides various commands to make search easy.

/text To search a text forward; where text can be a letter or a word
/text\c To search text, ignoring the case
?text To search text backward
n Forward search the last searched text (\ and ?)
N Backward search the last searched text (\ and ?)
:noh To remove the search highlight

Note that the search is not a command and does not require a colon (:) before searching a pattern with /, and ?.

Replace

To replace a character or text, there is a dedicated key binding in Vim.

r To replace the character under the cursor
R To enable the replace mode and replacing text till the Esc key is pressed

Find and Replace

Finding and replacing features can be achieved using the commands.

:%s/text/replace/g To search for text in the entire file and replace all occurrences of text with replace string (all occurrences of text)
:%s/text/replace To search for text in the entire file and replace all occurrences of text with replace string (only the first occurrence of text in a line)

Substitution

Substitution is a way of removing text and substituting it with another character or string.

s To delete a character and enable the insert mode
S To delete a line and enable the insert mode

After deleting the text, the insert mode is enabled and any substituted text can be inserted.

Tabs

Vim also allows working with multiple buffers at a time with the help of tabs. There are various commands to manage tabs effectively in Vim.

:tabe To create a new tab
:tabc To close the current tab
:tabo To close all the tab except the current tab
:tabdo <command> To execute a command on all the tabs
gt To move to the next tab
gT To move to the previous tab
<N>gt Move to the <N>the tab
:tabedit <filename> To open the file in a new tab
:tabfind <filename> Find a file and open it in a new tab if exist
:tabs To list all the tabs

Window Splits

Splitting windows in Vim allows opening multiple files in the same window.

:sp <filename> To open the file into a new split window horizontally
:vs <filename> To open the file into a new split window vertically
Ctrl+w s To split a window horizontally
Ctrl+w v To split a window vertically
Ctrl+w c To close the current window
Ctrl+w w To switch between the windows
Ctrl+w Up To switch to up in split window
Ctrl+w Down To switch down in split windows
Ctrl+w Left To switch to left in split window
Ctrl+w Right To switch right in split windows
Ctrl+w <Num> + To increase the size of the current window by a <Num>
Ctrl+w <Num> – To decrease the size of the current window by a <Num>
:resize <Num> To resize the current window

<Num> is any integer.

Marks

Marks in Vim are like location markings or bookmarks that can be accessed later, especially useful for navigating large files.

:ma To create a mark ‘a’ at the current cursor position
`a To jump mark ‘a’; put the cursor at the exact marked location
‘a To jump mark ‘a’; put cursor at the beginning of the line
:marks To list the marks
:delmarks a To delete mark ‘a’
:delmarks! To delete all lowercase marks
d’a To delete lines from the current line to the mark a
Y’a To yank lines from the current line to the mark a
:’a,’bd To delete all the lines from mark a to mark b
:’a,’by To yank all the lines from mark a to mark b

Registers

In Vim, registers are memory locations used to store text or operations for later access. Vim has 10 types of registers working on various tasks by default, such as storing text after yanking, or deleting.

:reg<CR> To list all registers
“ay To yank text to register ‘a’
“ap To paste text stored in register ‘a’
“*y To yank text to the system clipboard
“*p To put text from system clipboard

The <CR> represents carriage return or Return key.

Macros

Macros in Vim are used to store a set of commands with the help of registers.

qm To start recording the macro m
q To stop recording of the macro
@m To execute the macro m
@@ To execute the last macro

Key Maps

In Vim, key mapping is used to bind complex commands with keys.

:map <lhs> <rhs> The <lhs> contains the set of keys and <rhs> contains key binds or commands, for example; to use ctrl+a to select all text in a file the key map would be :map <C-a> ggVG
:unmap <key> To unmap a mapping in the current buffer
:mapc To remove all the mappings in the current buffer

Folds

In Vim, folds are used to fold several lines in one line to keep the text organized.

zf To create a fold with a motion and count
za To toggle a fold
zo To open a fold
zc To close a fold
zd To delete a fold
zR To open all folds

Indent

Indentation is very important, especially when it comes to using Vim for development purposes.

>> Indent text right
<< Indent text left
>% Indent block right if the cursor on braces () or {}
<% Indent block left if the cursor on braces () or {}
>ib Indent the inner block of braces ()
>iB Indent the inner block with braces {}
5== Re-indent the 5 lines
%= Re-indent the blocks with () and {}
gg=G Re-indent the entire file

Editing

Many commands can help edit the text.

J To join the line below the current line
gJ To join the line below the current line without the space
~ To toggle the case of the character under the cursor and the selection
. To repeat the last change or command
ggVG To select all
:set list It shows the hidden characters, such for tab it is the ^I sign, and $ shows the end of the line

History

The keyboard shortcuts of Vim command history and search history are mentioned below.

q: To list the command history
q/ To list the search history
Ctrl+C To close the command or search history split

Conclusion

Vim is a terminal-based editor designed to work with keyboard shortcuts. This guide listed all the significant Vim keyboard shortcuts. All the shortcuts are categorized by the functionality they support. A few commands are also mentioned for quick reference.

About the author

Sam U

I am a professional graphics designer with over 6 years of experience. Currently doing research in virtual reality, augmented reality and mixed reality.
I hardly watch movies but love to read tech related books and articles.