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. Everyday Vim – A Basic Vim Commands Cheat Sheet Vim is a pretty great text editor, but learning to use it effectively can be a challenge. Even if you keep a quick-reference card or cheatsheet around, it can be difficult to figure out which commands are the most useful.
Vim is a very powerful editor with an enormous set of functions. This article describes the basic commands that are essential to working with Vim. Time you spend on learning Vim commands and functions will pay big dividends for you in your future use of the program. It is essential for you to not just read this article, but also to try all the actions in some test text file so that you can better learn and remember them.
Modes
There are three text operation modes in Vim:
- Command mode: all keystrokes are interpreted as commands.
- Insert mode: used for text input.
- Visual mode: used for highlighting text or a text block, this mode can be considered as a submode for command mode.
To switch from Insert mode or Visual mode to Command mode, press the Esc key.
To switch from Command mode to editing mode, press one of the following keys:
- i – switches to Insert mode before the current cursor position.
- a – switches to Insert mode after the current cursor position.
- I – jumps to the first non-space symbol in the current line and switches to Insert mode.
- A – jumps to the last symbol in the current line and switches to Insert mode.
To switch from Command mode to Visual mode, press one of the following keys:
- v – switches to Visual mode (symbols highlighting).
- V – switches to Visual mode (lines highlighting).
Ctrl+v – switches to block Visual mode (highlighting of rectangular text fields)
Commands
Replacing commands
These commands do not work in Insert mode, but in all three modes you can use cursor-moving keys. If you remember at least some of these commands, you will be able to easily move from point A to point B. One important thing to note is that these commands set the range for other commands.
h, j, k, l | moving through the text in different directions |
|, 0, home | move the cursor to the beginning of the line |
^ | move to the first non-space symbol in the line |
$, end | ove to the end of the line |
m | move one-half of the screen width |
g | move to the lower line |
e | move to the end of the current word |
E | move to the end of the current word ignoring punctuation marks |
– | move to the first non-space symbol in the line above |
+, Enter | move to the first non-space symbol in the line below |
G | move to the last line |
G | move to line |
H | move to the first screen line |
M | move to the middle screen line |
L | move to the last screen line |
w | move to the beginning of the next word |
b | move cursor to the beginning of the current word |
B | move to the beginning of the word ignoring punctuation marks |
ge | move to the end of the next word |
gE | move to the end of the previous word ignoring punctuation marks |
( – | move to the previous sentence (to the dot) |
) | move to the next sentence (to the dot) |
{ | move to the previous paragraph (to the empty line) |
} | move to the next paragraph (to the empty line) |
3w | move to beginning of the fourth word counting from the current position |
4e | move to the end of the fourth word counting from the current position |
: | move to line number |
% | move to the next pair of brackets |
:ju | shift list |
Ctrl+e | scroll one line down |
Ctrl+d | scroll 1/2 of the page down |
Ctrl+f | scroll 1 page down |
Ctrl+y | scroll 1 line up |
Ctrl+u | scroll 1/2 of the page up |
Ctrl+b | scroll 1 page up |
Deleting commands
Del, x | delete the symbol under the cursor |
X | delete the symbol before cursor |
x | delete of symbols, beginning with the one under the cursor |
C | delete all from cursor to the end of the line and switch to Insert mode |
D | delete all from the current symbol to the end of the line |
d | delete n+1 lines |
DEL | delete the next symbol |
cc | change the whole line |
c$ | change the line beginning from the cursor position to the end |
d | delete the symbol from the current cursor position to the position set by the next command (for example: d$ deletes all the symbols from the current cursor position to the end of the line) |
Highlighting commands
v + hjkl | text highlighting |
SHIFT + v | line highlighting |
vip | paragraph highlighting |
viw | word highlighting |
Shift+v or 0v$ | line highlighting |
^v$ | line highlighting beginning with the first non-space symbol |
vi( | highlight everything between the nearest round brackets (similarly, use ‘vi[‘ and ‘vi{‘ for square and curved brackets) |
va( | highlight all between the nearest round brackets including the brackets themselves |
v2j | highlight all up to two lines down |
vt, | highlight all up to the nearest coma |
:syntax on | enable highlighting syntax |
:syntax off | disable highlighting syntax |
Cut / paste commands
P | paste before the current line |
p | paste after the cursor |
[p | paste before the cursor |
i | paste text before the cursor |
I | paste text in the beginning of the line |
CTR+p or CTR+n | automatic text insert (in the editing mode) |
CTR+r,=, | paste expression, for wxample 5*2 – 3 (in the editing mode) |
a | add text after the cursor |
A | add in the end of the line |
o | add line right after the current one |
O | add line after the current one |
dip | cut paragraph |
di( | cut the content of round brackets |
da( | cut the content of round brackets and the brackets themselves |
dd | cut the line where the cursor is. Before any command you may enter a number modifier that denotes the number of times to perform the command. For example: 3dd – deletes three lines beginning with the current one. |
Copying commands
Vim Cheat Cheat Sheets
yy | copies the line. Also you may use a number modifier |
y | copy n+1 line |
y2y | copy two lines |
yiw | copy the word |
y | copy symbols from the current position to the position set by the next command |
Other editing commands
<< | left shift the current line |
>> | right shift the current line |
J | unification of the current line with the next one |
R | replace symbols beginning with the current position |
xp | exchange positions of two symbols |
Vim cases
Vu | shift the line to the lowercase |
VU | shift the line to the uppercase |
g~~ | invert case |
vEU | shift the case of the word under the cursor to the uppercase |
vE~ | invert the case of the word |
ggguG | shift all the text into the lower case |
:setignorecase | case-independent search |
:set smartcase | ignore register while searching if there are no uppercase symbols in the target expression |
:%s/<./u&/g | shift the first letter of each word into the uppercase |
:%s/<./l&/g | shift the first letter of each word into the lowercase |
:%s/.*/u& | shift the first letter of the first word of each line into the uppercase |
:%s/.*/l& | shift the first letter of the first word of each line into the lowercase |
Latest changes cancelation
U | discard all changes that affect the current line |
u, :u[ndo] | discard the previous action (undo) |
CTR-R, :red[o] | cancel the discard of the previous action (redo) |
Saving
To save the changes in the file, you need to do the following: switch to the normal mode by pressing enter the command :write , or its shortened variant :w
Word wrap
Vim Cheat Sheet Pdf
:set wrap | enable word wrap (default) |
:set nowrap | disable word wrap |
Printing
:ha[rdcopy] | print document |
:set printoptions=duplex:off | disable duplex printing |
Folding commands
zc | fold block |
zo | unfold block |
zM | close all blocks |
zR | open all blocks |
za | inverting |
zf | see :set foldmethod=manual |
:set foldenable | enable folding |
:set foldmethod=syntax | syntax based folding |
:set foldmethod=indent | shift based folding |
:set foldmethod=manual | highlight the area using v and say zf |
:set foldmethod=marker | text markers based folding |
:set foldmarker=bigin,end | set markers for the beginning and the end of the block |
Marks
ma | set local marker a |
mB | set global marker B |
‘c | move to local marker c |
‘0 | return to the position where you were working when Vim was closed last |
:marks | display markers |
Shifts
[#]> | shift the highlighted to the right |
[#]< | shift the highlighted to the left |
[#]>> | shift the line to the right |
[#]<< | shift the line to the left |
set tabstop=# | for tabbing # of spaces |
set shiftwidth=# | in commandх for the shift # of spaces are used |
set [no]expandtab | change tabbing for the corresponding number of spaces |
Search commands
/door | search for word “door” upside down |
?door | search for word “door” bottom-up |
/jo[ha]n> | search for “john” or “joan” |
n | go to the next search result item |
N | go to the previous search result item |
:10,20s/old/new/g | the same, but for lines from 10th to 20th |
/< …. > | search for four-symbol words |
/fred|joe | search for “fred” or “joe” |
/<dddd> | search for 4 numbers nearby |
/^n | search for 2 empty lines |
:bufdo /searchstr/ | search in all files opened |
If the function ‘incsearch’ is enabled, Vim immediately goes to the first search correspondence when you begin to enter the word for search. If the function ‘hlsearch’ is enabled, all of the correspondences are highlighted. To disable highlighting, enter :nohl.
Replacement
:s/old/new/g | replace all combinations of ‘old’ in the current line for ‘new’ |
:%s/old/new/g | replace all appearances of ‘old’ for ‘new’ |
:%s/old/new/gw | replace all appearances of ‘old’ for ‘new’ with confirmation request |
:%s/old/new/gi | replace ‘old’ for ‘new’ taking the case into account |
:5,30s/old/new/g | replace all appearances of ‘old’ for ‘new’ between 5th and 30th lines |
:10,$s/old/new/g | replace all appearances of ‘old’ for ‘new’ beginning with 10th line and to the end of the file |
:%s/^/Welcome/g | add Welcome at the beginning of each line |
:%s/$/Ending/g | add Ending at the end of each line |
:%s/ *$//g | remove all the spaces |
:g/door/d | remove all the lines containing ‘door’ |
:v/door/d | remove all the lines not containing ‘door’ |
:s/old/new/ | replace the first appearance of ‘old’ for ‘new’ in the current line |
:%s/r//g | remove the carriage return char |
:%s#>[^<]+>##g | clear all HTML-tags from the text |
:%s/^(.*)n$// | remove repeating lines |
:g/^$/d | remove all empty lines |
Ctrl+a | increase the number under the cursor by one |
Ctrl+x | decrease the number under the cursor by one |
ggVGg? | modify the text into Rot13 |
Shell-commands performing
:! | perform in the current directory |
Line numbers
:set number | enable lines numeration |
:set nonumber | disable lines numeration |
Tab operation
:tabnew [fname] | create tab |
:tabs | show tabs list |
:tabn | next tab |
:tabp | previous tab |
gt | move to tab #n |
gt | next tab |
gT | previous tab |
Windows operation
:split | tile horizontally |
:vsplit | tile vertically |
Ctr+Wc | close the window |
Ctr+W, hjkl or arrows | switch between windows |
Ctr+W_ | maximize current window |
Ctr+W= | adjust windows size |
10 ctrl-w+ | increase current window by 10 lines |
:vsplit file | split the window vertically |
:sview file | split the window and open file in read-only mode |
:hide | close current window |
:only | close all windows except the current |
:b 2 | open #2 in current window |
Spelling check
mkdir -p ~/.vim/spell
cd ~/.vim/spell
wget http://ftp.vim.org/vim/runtime/spell/en.ascii.sug
wget http://ftp.vim.org/vim/runtime/spell/en.ascii.spl
setlocal spell spelllang=en | enable spelling check |
set spell! | turn on/off spelling check when working |
]s | next incorrect word |
[s | previous incorrect word |
Work with encoding
e ++enc= | edit file in ??? encoding |
w ++enc=< encoding name > | save file in new encoding |
set fileencodings=utf-8,koi8-r | the list of automatically determined encodings in descending order of priority |
Reading the file
:r | insert filename content after cursor |
Exit commands
:wqclose and save file:wqaclose and save all files
:q | close file if there is no need for saving, i.e. if there were no changes made in the file. Otherwise VIM will display an error notification as seen in this picture |
:q! | close file without saving |
:qa! or 😡 | close all files without saving |
Other
:set [no]wildmenu | when auto-adding in the command line the possible variants will be shown above it |
:set list | show tabbing and line breaks |
q: | Command history |
. | latest command retry |
:color | choosing colour scheme. Colour schemes: /usr/local/share/vim/vim72/colors/*.vim |
:pwd | previous catalogue |
:cd [path] | move to next catalogue |
:! | perform command |
Help topics
:help – help topics for the editor command.
Vim Cheat Sheet Reddit
Remember, you do not need to know all of the commands: you only need to know where to find them when required. For example, see :help usr_toc to view help topics. You can see :help index to search for the exact topic that is interesting to you. For example run /insert mode to see information about Insert mode.