Saturday, March 8, 2008

vi Editor Commands

The VI editor is a screen-based editor used by many Unix users. The VI editor has powerful features to aid programmers, but many beginning users avoid using VI because the different features overwhelm them.

General startup
To use vi: vi
To exit vi and save changes: ZZ or :wq
To exit vi without saving changes: :q!
To enter vi command mode: [esc]
Read the original file back in so that you can start over.: :e!


Counts

A number preceding any vi command tells vi to repeat that command that many times.
Deleting
x delete character under cursor
X delete character before cursor
dd delete line under cursor
dw delete word under cursor
db delete word before cursor
D delete to the end of line from the current cursor position
d^ delete from current cursor position to the beginning of the line
d$ delete from current cursor position to the end of the line

Copy/Paste Text
yy copies line
P paste copied data before cursor
p paste copied data after cursor

Find Commands
? finds a word going backwards
/ finds a word going forwards
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
, repeat last f, F, t, T going backwards
n repeat last search given by ‘/’ or ‘?’
N repeat last search given by ‘/’ or ‘?’ going backwards

Miscellaneous Commands
. repeat last command
u undoes last command issued
U undoes 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
^^ Go back to last file
READING/WRITING FILES
:r filename Copies filename after cursor in file currently editing
:n Start editing next file in list
:rew rewind file list, start editing 1st file on argument list
:w Saves the current file without quitting

MOVING
:# Move to line #
:$ Move to last line of file


Character/Line Formatting
~ Switch the case of the character under cursor
< Shifts the line upto where to the left by one shiftwidth
<< Shifts the current line to the left, and can be specified with a count
> Shifts the line upto where to the right by one shiftwidth
>> Shifts the current line to the right, and can be specified with a count
J Join the current line with the next one.

SHELL ESCAPE
:!'CMD' Executes CMD as a shell command
:!sh Fork shell in vi; hit ctrl-d to go back to vi

INSERTING
r replace character under cursor with next character typed
R keep replacing character until [esc] is hit
i insert before cursor
I insert from the beginning of line
a append after cursor
A append at end of line
o open line below cursor and enter append mode
O open line above cursor and enter append mode
c change until . "cc" changes the current line
C change to the end of line from the current cursor position
s substitute one character under the cursor and go into insert mode
S change an entire line


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 line
^D move forward one half screen
^U move backward one half screen
^R redraw screen (does not work with vt100 type terminals)
^L redraw screen (does not work with Televideo terminals)

Cursor Movement
h move left
j move down
k move up
l move right
[return] move to the beginning of next line
$ last column on the current line
0 move cursor to the first column on the current line
^ move cursor to the first nonblank column on the current line
w move to the beginning of the previous word or punctuation
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
^H move cursor one space to the left
^J move cursor down one line in same column
^M move to the first character on the next line
^N move cursor down one line in same column
^P move cursor up one line in same column
% move cursor to the matching parenthesis or brace
( move cursor to the beginning of a sentence
) move cursor to the beginning of the next sentence
{ move cursor to the preceding paragraph
} move cursor to the next paragraph
| move cursor to the column specified by the count
+ move cursor to the first non-whitespace character in the next line
- move cursor to the first non-whitespace character in the previous line
_ move cursor to the first non-whitespace character in the current line

No comments: