vi – the text editor of choice

This article contains a hands-on quick reference guide to vi commands. It also includes examples where necessary. It was meant to be a personal vi documentation, but I think it will be useful to whosoever is learning  to use vi or just want to brush up their existing knowledge.

This article contains a hands-on quick reference guide to vi commands. It also includes examples where necessary. It was meant to be a personal vi documentation, but I think it will be useful to whosoever is learning  to use vi or just want to brush up their existing knowledge.

Basic moves
Note: Make sure you are in COMMAND mode before executing any of the following commands. If not sure, please press the ESC (escape key) first to enter into COMMAND mode.

h – left one character
j – down one line
k – up one line
l – right one character
G – navigate to the last line in the file
nG – navigate to line number “n”. (e.g. 10G , 50 , 100G )
:n[ENTER] – same as above
n – repeat previous command

Screen operations

^G – Show status of current file
^L – Refresh screen
^D – DOWN one half-screen (in insert mode, shift one tab BACK)
^U – UP one half-screen
^F – FORWARD one full-screen
^B – BACKWARD one full-screen
^E – Move window down one line, without moving cursor
^Y – Move window up one line, without moving cursor
H – Navigate to the HIGHEST position in the window
M – Navigate to the MIDDLE position in the window
L – Navigate to the LOWEST position in the window

Paragraph operations

{ – Move to the beginning of a paragraph
} – Move to the end of a paragraph
( – Move to the beginning of a sentence
) – Move to the beginning of the next sentence
[[ – Move to the beginning of a section
]] – Move to the end of a section

Line operations

$ – Move to the end of the line
^ – Move to the first non-white character on the line
0 – Move to column zero
n| – Move to column “n” on a line e.g . 20| navigates to the 20th column on the line you are on.
w – Move beginning of the next WORD (word is delimited by WHITE SPACES)
e – Move END of the next word
b – Move BACK to beginning of previous word
W,E,B – Same as above w,e,b
+ – Move to first non-white char of next line
– Move to first non-white char of previous line

Edit operations

ESC – Enter into COMMAND mode
. – (dot) repeat last change
o – OPEN a line below the cursor and enter into INSERT mode
O – OPEN a line above the cursor and enter into INSERT mode
i – INSERT, starting before the cursor
I – INSERT, at the beginning of the line
a – APPEND starting after the cursor
A – APPEND at the end of the line
J – JOIN the line below the cursor position to the current one.
ns – SUBSTITUTE for “n” characters (default is one char)
nS – SUBSTITUTE for “n” whole lines (default is one char)
nr – REPLACE “n” characters, default 1 char
R – enter OVERTYPE mode
c[move] – CHANGE to where the ‘move’ specifies
cc – CHANGE whole line
C – CHANGE till end of line
Note: In all of above text insertion modes (i,a,o,c,r), a number may be given before entering, this will duplicate the edit as many times.

^D – kill one indent
^^D – kill all indent for current line (type carat ‘^’ then a CTRL-D)
^W – word erase (can be set by ‘stty’ command )
^U – line erase (can be set by ‘stty’ command )
^H – same as backspace (can be set by ‘stty’ command )

EXIT methods

ZZ – Exit and save any changes
:[n,n]w[!] [filename] – WRITE without quitting (to “filename” if given)
Note:
use n,n to specify ‘from’ and ‘to’ line numbers, and ! to force.
Example:
:3,30 w fred – write lines 3-30 to file called fred
:10,+5 w file.txt – write line 10 and 5 lines after line 10 to file.txt

:x – WRITE and QUIT
:wq – WRITE and QUIT
:w !UNIX-CMD – WRITE output and pipe through a UNIX command
Example:
:1,10 w !lpr – sends first 10 lines to printer

:q[!] – QUIT without saving changes (! to force quit)

UNDO commands

u – UNDO last change
U – UNDO all changes made to the current line as long as your cursor has not moved off the line
“[1-9]PASTE – use a PASTE command to paste one of the numbered buffers. The numbered buffers store the last 9 deletes in first in last out method. For e.g “1P “2p

CUT commands

ndd – DELETE “n” lines, default is one line
d – DELETE to where “move” or “search” specifies
Examples:
dfe – del till ‘e’ found
dta – del till ‘a’
d10| – del till 10th col

dnmove – DELETE to where the nmove specifies (e.g. d4k del 4 lines above, including the current line)
d/ – , delete till pattern is found (forward)
d? – , delete till pattern is found (backward)
d’ – , delete till mark ‘chr’ (see miscellaneous to learn how to mark)
D – delete till end of line
nx – X-OUT one character under cursor (or “n” chars)
nX – X-OUT char before the cursor (or “n” chars)

YANK commands

y[MOVESRCH] – YANK to where “move” or “search” specifies
Examples
yw – yank word
yfg – yank till ‘g’ found

ynmove – YANK to where the nmove specifies (e.g. y4j yank 4 lines down inclusive of the current line.
y/pattern/ – YANK till ‘pattern’ found (forward)
y?pattern? – YANK till ‘pattern’ found (backward)
y'[CHR] – YANK till mark ‘chr’ (see miscellaneous to learn how to mark)
Y or yy – YANK one line
nY, nyy – YANK n lines below
“[char]YANK – Copy the yank into char

PASTE commands

P – (uppercase p) PASTE buffer before the cursor
p – (lowercase p) PASTE buffer after the cursor
“[CHAR]PASTE – PASTE the named buffer (a-z) using one of the commands above.
nPASTE – PASTE the numbered buffer (1-9) using one of the commands above.


Rating:
59.0
144 votes
1 2 3 4 5

Leave a Reply