vi
A self-compiled cheatsheet for usage of vi
Posted February 28, 2021 by Adrian Wyssmann
Introduction#
vim knows different modes
Mode
Description
normal mode
This is the standard mode when you enter vim. Here you can move around but not modify text unless you issue a command lik insert or append
insert mode
In the insert mode you can modify the text. Importantly to notice is that you will see -- INSERT --
on the last line. In this mode you can move with the arrow keys . To exit this mode, you have to press [ESC]
visual mode
In the visual mode you can navigate and manipulate text selections. It is similar to normal mode and you can perform the same commands.
ex-mode
This mode allows you to enter ex commands as you can do in the command line. Unless the command line, the ex-mode does not return automatically to the normal mode. Note that the Ex mode is designed for Batch processing and as such won’t support mappings or command-line editing. This mode is entered with Q
and exited by entering :visual
command line#
On the command line allows you to run ex commands enter search patterns, and enter filter commands. As soon as you start typing :
or /
you are on the command line and you can enter commands. After that vim automatically return to normal mode.
Terms#
Many commands that change text are made from an operator and a motion so e.g. an example of deleting text with d
d
is the delete operator
motion
is what the operator will operate on e.g. w
would delete text until the start of the next word, EXCLUDING its first character.
Also know that a word
punctuation considered words
spaces separates words
Editing a file#
Command
Description
:e[dit]
Edit the current file. This is useful to re-edit the current file, when it has been changed outside of Vim.
:e[dit]!
Edit the current file always. Discard any changes to the current buffer. This is useful if you want to start all over again.
:e[dit] {file}
Edit {file}
:e[dit]! {file}
Edit {file} always. Discard any changes to the current buffer.
gf
Edit the file whose name is under or after the cursor. Mnemonic: “goto file”.
General#
Keys
Description
u
undo
Ctrl+r
redo
.
repeat last command
Cursor movement (Inside command/normal mode)#
Keys
Command
Description
h
move cursor left
j
move cursor down
k
move cursor up
l
move cursor right
w
jump by start of words
W
jump by words
e
jump to end of words
E
jump to end of words
b
jump backward by words
B
jump backward by words
0
(zero)
start of line
^
first non-blank character of line (same as 0w
)
$
end of line
Ctrl+d
move down half a page
Ctrl+u
move up half a page
}
go forward by paragraph (the next blank line)
{
go backward by paragraph (the next blank line)
gg
go to the top of the page
G
go the bottom of the page
[num] G
:[num] [enter]
Go to that line in the document
Search and Replace#
Keys
Command
Description
f [char]
Move to the next char on the current line after the cursor
F [char]
Move to the next char on the current line before the cursor
t [char]
Move to before the next char on the current line after the cursor
T [char]
Move to before the next char on the current line before the cursor
;
go to the next searched item
,
go to the previous searched item
/pattern
search for pattern
?pattern
search backward for pattern
n
repeat search in same direction
N
repeat search in opposite direction
:%s/old/new/g
replace all old with new throughout file (gn is better though)
:%s/old/new/gc
replace all old with new throughout file with confirmations
Insert/Appending/Editing Text#
Any of these results in Insert mode. Press Esc
or Ctrl+[
to exit insert mode
Keys/Command
Description
i
start insert mode at cursor
I
insert at the beginning of the line
a
append after the cursor
A
append at the end of the line
o
open (append) blank line below current line (no need to press return)
O
open blank line above current line
cc
change (replace) an entire line
c [movement command]
change (replace) from the cursor to the move-to point
r [char]
replace a single character with the specified char (does not use Insert mode)
d
delete
d [movement command]
deletes from the cursor to the move-to point
dd
delete the current line
J
join line below to the current one
:r[ead] {name}
Insert the file [name] below the cursor.
:r[ead] !{cmd}
Execute {cmd} and insert its standard output below the cursor.
Marking text (visual mode)#
In visual mode you can move around as in normal mode (h
, j
, k
, l
etc.) and can then do a command (such as y
, d
, or c
)
Keys
Command
Description
v
starts visual mode
V
starts line-wise visual mode
Ctrl+v
starts visual block mode
Esc
or Ctrl+[
exit visual mode
O
move to other corner of block
Cut and paste#
Keys
Description
y
yank (copy) marked text
d
delete marked text
c
delete the marked text and go into insert mode (like c does above)
yy
yank (copy) a line
p
put (paste) the clipboard after cursor
P
put (paste) before cursor
dd
delete (cut) a line
x
delete (cut) current character
X
delete previous character (like backspace)
Exiting#
Keys/Commands
Description
:q[uit]
Quit Vim. This fails when changes have been made
:q[uit]!
Quit without writing
:cq[uit]
Quit always, without writing
:wq
Write the current file and exit
wq!
Write the current file and exit always
:wq {file}
Write to {file}. Exit if not editing the last
:wq! {file}
Write to {file} and exit always
:[range]wq[!] [file]
Same as above, but only write the lines in [range]
ZZ
Write current file, if modified, and exit
ZQ
Quit current file and exit (same as :q!
)
Marks#
Marks allow you to jump to designated points in your code.
Keys
Description
m{a-z}
Set mark {a-z} at cursor position. A capital mark {A-Z}
sets a global mark and will work between files
‘{a-z}
move the cursor to the start of the line where the mark was set
‘’
go back to the previous jump location
Split Windows#
Keys
Command
Description
Ctrl-wn
:new
new horizontal split (editing a new empty buffer)
Ctrl-ws
:split
split window horizontally (editing current buffer)
Ctrl-wv
:vsplit
or :vsp
split window vertically (editing current buffer)
Ctrl-wc
:close
close window
Ctrl-wo
:only
close all windows, leaving :only the current window open
Ctrl-ww
go to next window
Ctrl-wp
go to previous window
Ctrl-wq
:exit
quit a window
Ctrl-w<Up>
go to window above
Ctrl-w<Down>
go to window below
Ctrl-w<Left>
go to window on left
Ctrl-w<Right>
go to window on right
:vsplit {filename}
use the filename for the split command
Tabs#
Keys
Command
Description
:tabe
Make a new tab
:tabnew
To open an existing file in a new tab
:tabedit [FILENAME]
To open an existing file in a new tab
Ctrl-PageDown
or gt
:tabn
next tab
Ctrl-PageUp
or gT
:tabp
previous tab
:tabc
close current tab
:tabo
close all other tabs leaving ONLY the current tab open
Edit this page