Unix

 
 
This tutorial contains several useful Unix commands.

rss feed

Quick guides

  • Vi commands





Vi commands.




Command Description

Start / Exit vi
vi filename Open file
vi -r filename Recover filename that was being edited when system crashed
ZZ or :wq To exit vi and save changes
:q! To exit vi without saving changes
[ESC] Exit Line Editor mode to enter vi command mode
: Enter Line Editor mode

Cursor movement
h move left
j move down
k move up
l move right
[RETURN] move to the beginning of the next line
$ last column on the current line
0 (zero) move cursor to the first column on the current line
^ move cursor to first nonblank column on the current line
w move to the beginning of the next word or punctuation mark
W move past the next space
b move to the beginning of the previous word or punctuation mark
B move to the beginning of the previous word, ignores punctuation
e end of next word or punctuation mark
E end of next word, ignoring punctuation
H move cursor to the top of the screen
M move cursor to the middle of the screen
L move cursor to the bottom of the screen
:# move to line #
:$ move to last line of file

Screen movement
G move to the last line in the file
xG move to line x
z+ move current line to top of screen
z move current line to the middle of screen
z- move current line to the bottom of screen
^f move forward one screen
^b move backward one screen
^d move forward one half screen
^u move backward one half screen
^r redraw screen
^l redraw screen, removing deleted lines

Inserting
C change (replace) the characters in the current line, until [ESC] is hit
cc change (replace) the entire current line, stopping when [ESC] is hit
cw change the current word with new text, starting with the character under cursor, until [ESC] is hit
cNw change N words beginning with character under cursor, until [ESC] is hit. Example c3w changes 3 words
Ncc or cNc change (replace) the next N lines, starting with the current line, stopping when [ESC] is hit
r replace character under cursor with next character typed
R keep replacing character, starting with current cursor position, until [ESC] is hit
i insert text before cursor, until [ESC] is hit
I insert text at beginning of the current line, until [ESC] is hit
a append text after cursor, until [ESC] is hit
A append text to end of the current line, until [ESC] is hit
o open and put text in a new line below the current line, until [ESC] is hit
O open and put text in a new line above the current line, until [ESC] is hit

Deleting
x delete character under cursor
dd delete line under cursor
dw delete word under cursor
db delete word before cursor
Nx delete N characters, starting with character under cursor
dNw delete N words beginning with character under cursor. Example d3w deletes 3 words
D delete the remainder of the line, starting with current cursor position
Ndd or dNd delete N lines, beginning with the current line. Eaxmple 3dd deletes 3 lines

Cut and Paste
yy copies (=yank) line into the buffer which may then be put by the p(put) command.
Nyy or yNy copies (=yank) the next N lines into the buffer which may then be put by the p(put) command.
P put (paste) the line(s) in the buffer into the text before the current line
p put (paste) the line(s) in the buffer into the text after the current line

Find commands
?string finds a word going backwards
/string finds a word going forwards
n move to next occurrence of search string
N move to next occurrence of search string in opposite direction
f finds a character on the line under the cursor going forward
F finds a character on the line under the cursor going backwards
t find a character on the current line going forward and stop one character before it
T find a character on the current line going backward and stop one character before it
; repeat last f, F, t, T

Miscellaneous Commands
:.= returns line number of current line at bottom of screen
:= returns the total number of lines at bottom of screen
^g provides the current line number, along with the total number of lines, in the file at the bottom of the screen
. repeat last command
u undo last command issued
U undo all commands on one line
xp deletes first character and inserts after second (swap)
J join current line with the next line
^G display current line number
% if at one parenthesis, will jump to its mate
mx mark current line with character x
'x find line marked with character x
number a number preceding any vi command tells vi to repeat that command that many times.

Read / Write files
:r filename read file named filename and insert after current line (the line with cursor)
:w filename saves the current file without quitting
:w newfile saves to a new file without quitting
:w! prevfile write current contents over a pre-existing file named prevfile
:3,20w newfile write the contents of the lines numbered 3 through 20 to a new file

Shell Escape
:$'cmd' executes 'cmd' as a shell command.