This is a quick Vim reference, it takes you through the things that you should know. It is not a complete reference.
The older (Unix) Vi editor is much the same but does not do all that vim does. Only some differences are noted here.
The key to effective vim use is to spot the symmetries that often exist in the commands.
c
’ then shift ‘c
’ will often do the same as command
‘c
’ but do it backwards/before, eg: ‘/
’ searches forwards, ‘?
’ searches backwards;
‘p
’ pastes text after the cursor, ‘P
’ pastes text before the cursor.5w
’ moves forwards 5 words; ‘5x
’ deletes 5 characters.
For effective use of vim you must master the movement commands, they are used in
conjunction with others, eg: ‘dw
’ deletes the next word (and ‘5dw
’ or ‘d5w
’ deletes
the next 5 words); ‘c/fred
’ changes from the current position to the next occurrence
of ‘fred
’.
These move the position of the cursor in the buffer
h | move one character left |
j | move one line down |
k | move one line up |
l | move one character right |
C-n | move one line down |
C-p | move one line up |
H | move to Home (start of first line displayed) |
M | move to start of Middle line displayed |
L | move to start of Last line displayed |
0 | move to start of current line |
^ | move to first non white space character on current line |
$ | move to end of current line |
tc | move forwards to just before character ‘c ’ |
Tc | move backwards to just after character ‘c ’ |
fc | move forwards on to character ‘c ’ |
Fc | move backwards on to character ‘c ’ |
; | repeat last t , T , f , F command |
w | move forwards to the start of the next word |
e | move forwards to the end of the next word |
b | move backwards a word |
) | move forwards to sentence start |
( | move backwards to sentence start |
} | move forwards to paragraph start |
{ | move backwards to paragraph start |
C-g | Display the current line number |
ng | move to line number n |
G | move to last line |
% | move to matching bracket: () [] {} |
The arrow, home and end keys should also work as expected.
‘C-x
’ means press the Control key when you press ‘x
’
/ | Search forwards for regular expression |
? | Search backwards for regular expression |
n | repeat last search in the same direction |
N | repeat last search in the reverse direction |
When ‘/
’ or ‘?
’ is pressed the cursor
moves to the bottom line of the screen which is where you type the search string
(regular expression)
You may give 26 positions names, named with a letter (a
.. z
).
ma | mark current position with the name a |
`a | return the position a |
'a | return the first non white space position of the line marked a |
'' | return to the last line that you jumped from |
:marks | show current marks |
Note the difference between the look-similar characters: ‘`
’ (grave accent) & ‘'
’ (single quote)
There are another 26 positions that are named with a CAPITAL letter (A
.. Z
).
Whereas the lower case letters (a
.. z
) are marks within a buffer, the marks A
.. Z
remember the file name as well, so returning to one of them will reopen the file.
These commands change which lines in the buffer are displayed.
C-e | scroll line with cursor up a line |
C-y | scroll line with cursor down a line |
C-d | scroll display down 1/2 a screen |
C-u | scroll display up 1/2 a screen |
C-f | scroll display forward a screen |
C-b | scroll display backward a screen |
C-l | redisplay the screen |
i | Insert before the current character |
a | Insert after the current character |
o | Open a new line after the current line |
O | Open a new line before the current line |
I | Insert before first non white space character on the current line |
A | Insert after the last non white character on the current line |
Entering insert mode:
In insert mode characters are inserted into the buffer, to leave it type Escape.
To insert a special character type ‘C-vc
’ which inserts character ‘c
’ without
interpretation (useful with Escape).
x | Delete the character under the cursor |
dM | Delete to where the movement ‘M ’ would take the cursor |
dd | Delete the current line |
D | Delete from the current position to the end of line |
‘dM
’ deletes from the current position to where the movement command ‘M
’
would take the cursor. Illustrations:
dt:
’ will delete to just before the next ‘:
’ df:
’ will delete to (and including) the next ‘:
’ dw
’ will delete the next wordd5w
’ will delete the next 5 words (as will ‘5dw
’)d$
’ will delete to the end of lined/fred
’ will delete to the next word fred
, possibly several lines awayd`a
’ will delete to the position of mark a
d%
’ when placed on bracket will delete to the matching bracket
Deleted text is put into the 'unnamed' yank register, you may specify a register, eg:
‘"bdT:
’ deletes backwards to the ‘:
’ and puts the deleted text into yank register ‘b
’.
See Yank registers below for multiple delete undo via numbered yank registers.
rc | Replace the current character with ‘c ’ |
s | Substitute the current character: enter insert mode |
cM | Change to where the movement ‘M ’ would take the cursor: enter insert mode |
cc | Change the entire current line: enter insert mode |
C | Change from the current position to the end of line |
R | Replace text, end with Escape |
o | open new line below, end with Escape |
O | open new line above, end with Escape |
Again: the ‘M
’ of ‘cM
’ is a movement. Eg:
ct.
’ deletes to the next ‘.
’ (full stop) and enters insert modecw
’ allows you to change the next wordThese store text that can be pasted into an edit buffer. Yank registers can be used to copy text between edit buffers.
yM | Copy text into yank register to where the movement ‘M ’ would take the cursor |
yy | Copy the current line into the yank register |
Y | Copy from the current position to the end of line into the yank register |
p | Paste the text in the yank register after the current line in the edit buffer |
P | Paste the text in the yank register before the current line in the edit buffer |
:dis | Display the contents of all yank registers |
Again: the ‘M
’ of ‘yM
’ is a movement. Eg:
yt.
’ yanks to the next ‘.
’ (full stop)y5w
’ yanks the next five wordsy`w
’ yanks to the position of mark w
There are 26 yank registers, named with a letter (a
.. z
).
The above commands may be prefixed by this name: ‘"a
’ operates on yank register ‘a
’, eg:
"b5yy
’ copies five lines into yank register ‘b
’"dp
’ pastes from yank register ‘d
’
There are another nine yank registers, named with a digit (1
.. 9
).
Deleted text in put into register ‘1
’, text that was in ‘1
’ is put into ‘2
’, ... to ‘9
’.
An unnamed yank register is used if a yank command is not prefixed by a name.
Many other commands can be prefixed by a register name, eg:
"add
’ deletes the current line and puts it into yank register ‘a
’The contents of the yank registers may be shown with the :registers
command. Abbreviate to :reg
or :dis
Beware: there is a maximum size of what a register can contain
When you enter a ‘:
’ the cursor is moved to the message line (at the bottom of the screen)
where you enter the rest of the command.
You can recall previous commands with up-arrow and then edit them.
:q | Quit |
:q! | Quit without the sanity checks (unsaved buffers, etc) |
:w | Write the buffer to the file that it was read from |
:w f | Write the buffer to file ‘f ’ |
:w! [f] | Write forcefully, eg if permissions/ownership would not allow |
:w !cmd | Write the buffer as stdin to shell command ‘cmd ’ |
:wq | Write and quit |
:x | Like ‘:wq ’ but only write if changes have been made |
ZZ | Same as ‘:x ’ |
:r file | Read file into the current buffer after current line |
:e f | Edit file ‘f ’. |
:e! f | Edit file ‘f ’, do not warn if current buffer has not been saved |
:n | Edit next file |
:p | Edit previous file |
:rew | Rewind to first file |
:! cmd | Execute shell command ‘cmd ’. If a range is given the lines are filtered |
:s/a/b/ | Substitute ‘a ’ with ‘b ’ on the current line. |
:s/a/b/g | Substitute every ‘a ’ with ‘b ’ on the current line. (g means global) |
:s/a/b/gc | Substitute every ‘a ’ with ‘b ’ on the current line, ask for confirmation |
:d | Delete the current line |
:d n | Delete the n lines, several commands take a number |
:m n | Move lines to position n |
:p | print — show the lines |
:l | as print but show line end & unprintable characters |
:help | Help. May be followed by a help topic |
In many of these ‘%
’ will be substituted with the name of the current file, and ‘#
’
with the name of the previous file. Eg:
:!chmod 644 %
’ change permissions on the current file:e #
’ edit the previous file, this is often bound to C-^Some of the colon commands can act on many lines. These can be given zero, one or two address ranges:
:d
’ deletes the current line:w file
’ writes all lines to file
:10 d
’ deletes line number 10:10 w file
’ writes line 10 to file
:10,15 d
’ deletes line number 10 to 15 inclusive (6 lines):10,15 w file
’ writes line 10 to 15 inclusive (6 lines) to file
Line numbers can be specified in several ways:
:10 d
’ deletes line number 10.
’, eg:
:. d
’ deletes the current line$
’, eg:
:$ d
’ deletes last line in the buffer%
’, eg:
:% d
’ deletes every line in the buffer:1,$ d
’ also deletes every line in the buffer'm
’, eg:
:'f d
’ deletes the line marked with f
:'F d
’ deletes line marked with F
in another file:'F,$ d
’ cannot be done/pattern/
’, eg:
:/fred/ d
’ deletes the line containing ‘fred
’, search starts at ‘.
’?pattern?
’, eg:
:?fred? d
’ deletes the line containing ‘fred
’, search starts at the line above ‘.
’:10 /fred/ d
’ start searching at line 10, delete the line containing ‘fred
’:'f /fred/ d
’ start searching at the line marked with f
for the line containing ‘fred
’, delete it:/bill/ /fred/ d
’ start searching at the line containing bill
for the line containing ‘fred
’, delete it+
’ or ‘-
’ number, eg:
:.-1,.+1 d
’ deletes the current line and the lines above & below it:g/pattern/
’, eg:
:g/fred/ d
’ deletes the lines containing ‘fred
’:g!/pattern/
’ or ‘:v/pattern/
’, eg:
:g!/fred/ d
’ deletes the lines not containing ‘fred
’:v/fred/ d
’ deletes the lines not containing ‘fred
’:.,.+10g!/fred/ d
’ deletes nearby lines not containing ‘fred
’:'f,$ g/fred/ d
’ deletes the lines containing ‘fred
’ starting with the line marked with f
ending the search at the end of bufferExamples:
:'f d 4
’ deletes 4 lines starting with the line marked f
2,5 s/fred/joe/
’ changes the first ‘fred
’ for ‘joe
’ on lines 2, 3, 4, 51,$ s/fred/joe/g
’ changes every ‘fred
’ for ‘joe
’ in the buffer% s/fred/joe/gc
’ as above but requests confirmation for each change.,/fred/ w ! lp
’ print the current line to the line containing ‘fred
’:'f-1,'l+1 s/^/#/
’ put a #
at the start of lines starting with the line above the one marked f
and ending with the line below the one marked l
:.,.+1s/\([a-z]\+\) \+\([a-z]\+\)/\2 \1/
’ swap the first 2 words on the next 2 lines:. w ! od -c
’ send the current line to the command od -c
(to check for strange characters):'f,. y q
’ yank to register q
lines starting with the line marked f
to .
:'f y q 5
’ yank to register q
5 lines starting with the line marked f
:put q
’ yank from register q
after the current line:'s,'e m .
’ the line range starting s
ending e
to after the current line:.! rev
’ the current line is filtered through the rev
command:g/fred/p
’ show all lines that contain ‘fred
’ — gREpJ | Join the next line to the current line |
u | undo the last change, vim: repeat for previous changes
vi: a second ‘u ’ undoes the first undo |
C-r | Redo a change undone by ‘u ’ (vim only) |
U | Undo all the changes made to the current line |
. | Repeat the last change command |
~ | Invert case (upper/lower) of the current character |
!!cmd | Replace the current line with the current line filtered by the shell command ‘cmd ’ |
nC | Repeats the command C n times |
qr | Start recording into register ‘r ’ (vim only). If ‘r ’ in upper case append |
q | Stop recording |
@r | Execute keystrokes in register ‘r ’ |
<< | Move text left by an indent |
>> | Move text right by an indent |
C-z | Suspend (shell job control) |
Examples:
qm3l5jq
’ creates register m
containing movement commands
@m
’ executes register m
running the movement commandsStart visual mode, move the cursor somewhere else (the area back to the start point is highlightened), enter a command that will be applied to every highlightened character.
Example: once a visual area is marked, the text in the region:
d | delete |
c | change |
y | yank |
~ | swap case |
u | make lower case |
U | make upper case |
! | filter through external program |
The screen may be split into multiple windows and different buffers (files) edited in the different windows. The normal yank/paste commands enable copying between buffers.
C-ws | Split the window horizontally in two (top/bottom) |
C-wv | Split the window vertically in two (left/right) |
C-wc | Close the current window |
C-wo | Make the current window the only one on the screen |
C-wM | Move to window in direction M , eg: ‘k ’ means up |
NC-w+ | Increase height of current window by N lines |
NC-w- | Decrease height of current window by N lines |
NC-w> | Increase width of current window by N columns |
NC-w< | Decrease width of current window by N columns |
:files | Display files currently being edited and buffer numbers |
:N b | Switch to buffer N in the current window |
set ic | Ignore case in searches |
set number | Line number mode |
set showmode | Display ‘INSERT ’ on bottom line in insert mode |
syntax off | Switch off syntax highlighting |
:
’ mode the line can be edited with arrow keys (including up/down)/
’) can also be editedn
’ and ‘.
’ may be used to do an interactive search and replace.xp
’ swaps two adjacent characters10dd
’ deletes 10 lines:.,$d
’ deletes from the current line to the end of the buffer:. w ! od -c
’ filter the current line through ‘od
’ to see if it contains any
strange characters.:. w ! sh
’ Execute the current line as a shell commandIf you want a tutorial use the command:
vimtutorial
The commands below are the most commonly used commands, but by no means all.
https://www.viemu.com/a-why-vi-vim.html
https://www.viemu.com/a_vi_vim_graphical_cheat_sheet_tutorial.html
https://wiki.init.cc/_media/docs/tools/vim_tips1.pdf
Return to tutorial home.
If you want any help using the above, or have any comments or suggestions, please contact us.