Friday, July 13, 2007

vi Reference Sheet

Visual Display Editor(vi) Command Reference Sheet

Starting vi
Command Meaning
vi filename Open or create a file
vi Open a new file to be named later
vi –r filename Recover a crashed file
view filename Open a file as read-only

Three Modes of operation in vi
1 Command mode: Allows positioning and editing commands to perform functions. Entered via Esc, from entry mode, or Ret from Last-line mode.
2 Entry mode: Allows you to enter text. Entered via typing; A i I o O c C s S or R from command mode.
3 Last-line mode: Initiated from command mode by entering advanced editing commands like :, /, ?, !.

Input Commands Deleting Text
Command Meaning Command Meaning
a Append text after the cursor x or dl Delete character at the cursor
A Append text at the end of the line X or dh Delete character to the left of the cursor
i Insert text before the cursor dw Delete word (or part of word to left of cursor)
I Insert text at the beginning of the line #dw Delete # of words
o Open a new line below the cursor dd Delete line containing the cursor
O Open a new line above the cursor #dd Delete # of lines
Positioning Commands D or d$ Delete line to left of cursor (from cursor position to the end of the line)
Command Meaning dG Delete to the end of the file
h, Left Arrow, or Back Space Move left one character d1G Delete from beginning of file to cursor
j or Down Arrow Move down one line :#1,#2d Delete lines #1 through #2
k or Up Arrow Move up one line Undoing Repeating and Changing Text
l, Right Arrow or Spacebar Move left (forward) one character Command Meaning
0 Move to beginning of current line cw Change word (or part of word) at cursor location to the end of the word
$ Move to end of current line #cw Change # number of words
w Move forward one word (including punctuation) c#b Change backwards # of words
W Move forward one word (past punctuation) cc or S Replace entire current line
b Move back one word (including punctuation) C or c$ Change from cursor to end of line
B Move back one word (past punctuation) c0 Change from cursor to beginning of line
e Move to end of current word s Substitute string for character(s)
E Move to end of word (past punctuation) R Overwrite or replace characters on line
( Move to beginning of sentence r Replace character at cursor with one other character
) Move to end of sentence i (Return) Break line
{ Move to beginning of paragraph J Join current line and line below
} Move to end of paragraph xp Transpose character at cursor and character to the left
]] Move to next section/function ~ Change case of letter at cursor
[[ Move to previous section/function u Undo previous command (only goes back one level.)
% Find matching ( ) or { } U Undo all changes to current line
- Move up to beginning of previous line :u Undo previous last-line command
Return Move down to beginning of next line Copying and Pasting Text
H Move to top of screen Command Meaning
M Move to middle of screen yy or Y Yank a copy of line
L Move to bottom of screen p Put yanked or deleted line below current line
G Go to last line of file P Put yanked or deleted line above current line
#G Go to # line of file :#1,#2 co #3 Copy lines #1 through #2 and put after line #3
:# Go to line # :#1,#2 m #3 Move lines #1 through #2 to line #3
CTRL-f Page forward one screen Saving and Quitting
CTRL-d Scroll down one-half screen Command Meaning
CTRL-b Page back one screen :w Save changes
CTRL-u Scroll up one-half screen :w new_filename Write buffer to new filename
CTRL-E Scroll window down one line :wq or ZZ Save changes and quit vi
CTRL-Y Scroll window up one line :q! Quit without saving changes. ('!' bang forces the system to perform action)

Setting Options Reading Files and Commands
Command Meaning Command Meaning
:set nu Show line numbers :r filename Insert (read) file at line after cursor
:set nonu Hide line numbers : # r filename Insert file after line #
:set ic Searches should ignore case :r !command Insert output from command after current line
:set noic Searches should be case-sensitive :!command Run command from subshell
:set list Display invisible characters such as Tab and end-of-line :sh Create subshell. (Exit returns to vi)
:set nolist Turn off the display of invisile characters Searching and Replacing Text
:set showmode Display current mode of operation Command Meaning
:set noshowmode Turn off mode display /string Search forward for string
:set lisp Inserts indents in appropriate lisp format ?string Search backward for string
:set nolisp Turn off lisp formatting n Find next occurrence of string
:set ai Insert mode will indent each line equal to the line above N Find previous occurrence of string
:set noai Turns off autoindent :%s/old/new/ Search and replace first occurrence of old with new
:set all Displays list of all available options :%s/old/new/g Search and replace all occurrences of old with new

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home