Yet another VI(M) cheat sheet
Siegfried Steiner
Siegfried Steiner
4 min read

Categories

Tags

As I tend to forget how to invoke the most essential vi functionality I am in the desperate need of a vi/vim cheat sheet, which is why I stumbled across the vi Editor “Cheat Sheet” from the University at Albany, that, with just very few additions, fits my needs for an everyday vi cheat sheet perfectly.

Invoking vi(m): vi <filename> (vim <filename>), format of vi(m) commands: [count][command] (count repeats the effect of the command)

Command mode versus input mode

Vi (vim) starts in command mode. The positioning commands operate only while vi is in command mode. You switch vi to input mode by entering any one of several vi input commands (see next section). Once in input mode, any character you type is taken to be text and is added to the file. You cannot execute any commands until you exit input mode. To exit input mode, press the escape (<ESC>) key.

Yet another vi(m) cheat sheet

Input commands

a Append after cursor
i Insert before cursor
o Open line below
O Open line above
:r <file> Insert <file> after current line

Any of these commands leaves vi in input mode until you press <ESC>. Pressing the <RETURN> key will not take you out of input mode.

Change commands (Input mode)

cw Change word (<Esc>)
cc Change line (<Esc>) - blanks line
c$ Change to end of line
rc Replace character with c
R Replace (<Esc>) - typeover
s Substitute (<Esc>) - 1 char with string
S Substitute (<Esc>) - Rest of line with text
. Repeat last change

Changes during insert mode

<CTRL>h Back one character
<CTRL>w Back one word
<CTRL>u Back to beginning of insert

Visual modes (copy with y, end with Esc)1

v Enter visual character mode
V Enter visual line mode
<CTRL>v Enter visual block mode

Cursor motions mark text (y copies d cuts to general buffer).

File management commands

:w <name> Write edit buffer to file <name>
:wq Write to file and quit
:q! Quit without saving changes
ZZ Same as :wq
:sh Execute shell commands (<CTRL>d)

Window motions

<CTRL>d Scroll down (half a screen)
<CTRL>u Scroll up (half a screen)
<CTRL>f Page forward
<CTRL>b Page backward
/string Search forward
?string Search backward
<CTRL>l Redraw screen
<CTRL>g Display current line number and file information
n Repeat search
N Repeat search reverse
gg Go to first line
G Go to last line
nG Go to line n
:n Go to line n
z<CR> Reposition window: cursor at top
z. Reposition window: cursor in middle
z- Reposition window: cursor at bottom

Cursor motions

H Upper left corner (home)
M Middle line
L Lower left corner
h Back a character
j Down a line
k Up a line
^ Beginning of line
$ End of line
l Forward a character
w One word forward
b Back one word
fc Find c
; Repeat find (find next c)

Deletion commands

dd or ndd Delete n lines to general buffer
d Delete (cut) marked to general buffer
dw Delete word to general buffer
dnw Delete n words
d) Delete to end of sentence
db Delete previous word
D Delete to end of line
x Delete character

Recovering deletions

p Put general buffer after cursor
P Put general buffer before cursor

Undo commands

u Undo last change
U Undo all changes on line

Rearrangement commands

yy or Y Yank (copy) line to general buffer
y Yank (copy) marked to general buffer
“z6yy Yank 6 lines to buffer z
yw Yank word to general buffer
a9dd Delete 9 lines to buffer a
A9dd Delete 9 lines; Append to buffer a
“ap Put text from buffer a after cursor
p Put general buffer after cursor
P Put general buffer before cursor
J Join lines

Parameters

:set list Show invisible characters
:set nolist Don’t show invisible characters
:set number Show line numbers
:set nonumber Don’t show line numbers
:set autoindent Indent after carriage return
:set noautoindent Turn off autoindent
:set showmatch Show matching sets of parentheses as they are typed
:set noshowmatch Turn off showmatch
:set showmode Display mode on last line of screen
:set noshowmode Turn off showmode
:set all Show values of all possible parameters

Move text from file old to file new

vi <old>  
a10yy yank 10 lines to buffer a
:w write work buffer
:e <new> edit <new> file
ap put text from a after cursor
:30,60w <new> Write lines 30 to 60 in file <new>

Regular expressions (search strings)

^ Matches beginning of line
$ Matches end of line
. Matches any single character
* Matches any previous character
.* Matches any character

Search and replace commands

Syntax: :[address]s/old_text/new_text/

Address components

. Current line
n Line number n
.+m Current line plus m lines
$ Last line
/string/ A line that contains “string
% Entire file
[addr1],[addr2] Specifies a range

Examples

The following example replaces only the first occurrence of Banana with Kumquat in each of 11 lines starting with the current line (.) and continuing for the 10 that follow (.+10).

:.,.+10s/Banana/Kumquat

The following example replaces every occurrence (caused by the g at the end of the command) of apple with pear.

:%s/apple/pear/g

The following example removes the last character from every line in the file. Use it if every line in the file ends with ^M as the result of a file transfer. Execute it when the cursor is on the first line of the file.

:%s/.$//

Further reading

  1. Does not apply to vi, only applicaple to Vim