VI Three Modes: command mode, insert mode and command line mode
1. Command mode: letters or sequence of letters interactively command vi. Commands are case sensitive. The ESC key can end a command.
2. Insert mode: Text is inserted.Enter insert mode with "i" (insert), "a" (insert after), "A" (insert at end of line), "o" (open new line after current line) or "O" (Open line above current line) commands
3. Command line mode: One enters this mode by typing ":" which puts the command line entry at the foot of the screen.
:q! - quit without change
:w - write to file
:wq - write to file and quit
:wq! - write and quite with override option..
:set nu - set line number
:vsplit. -split the screen vertically
Command mode --> Insert mode: enter "i","a","A" ,"o" or "O"
Insert Mode --> Command mode:ESC
Command mode-->Command line mode: use ":";
Command line mode --->Command mode: when met a error;
Editor command:
Keystrokes Action
i Insert at cursor
a Append after cursor
A Append at end of line
ESC Terminate insert mode
u Undo last change
U Undo all changes to entire line
o Open a new line
dd
3dd Delete line
Delete 3 lines.
D Delete contents of line after cursor
C Delete contents of line after cursor and insert new text. Press esc key to end insertion.
dw
4dw Delete word
Delete 4 words
cw Change word
x Delete character at cursor
r Replace character
R Overwrite characters from cursor onward
s Substitute one character under cursor continue to insert
S Substitute entire line and begin to insert at beginning of line
~ Change case of individual character
ctrl-a
ctrl-x Increment number under the cursor.
Decrement number under the cursor.
/search_string{CR} Search for search_string
?search_string{CR} Search backwards (up in file) for search_string
//<search_string/>{CR} Search for search_wordEx: //<s/>
Search for variable "s" but ignore declaration "string" or words containing "s". This will find "string s;", "s = fn(x);", "x = fn(s);", etc
n Find next occurrence of search_word
N Find previous occurrence of search_word
. repeat last command action.
Cursor movement:
Keystrokes Action
h/j/k/l Move cursor left/down/up/right
spacebar Move cursor right one space
-/+ Move cursor down/up in first column
ctrl-d Scroll down one half of a page
ctrl-u Scroll up one half of a page
ctrl-f Scroll forward one page
ctrl-b Scroll back one page
M (shift-h) Move cursor to middle of page
H Move cursor to top of page
L Move cursor to bottom of page
W
w
5w Move cursor a word at a time
Move cursor ahead 5 words
B
b
5b Move cursor back a word at a time
Move cursor back a word at a time
Move cursor back 5 words
e
5e Move cursor to end of word
Move cursor ahead to the end of the 5th word
0 (zero) Move cursor to beginning of line
$ Move cursor to end of line
) Move cursor to beginning of next sentence
( Move cursor to beginning of current sentence
G Move cursor to end of file
% Move cursor to the matching bracket.
Place cursor on {}[]() and type "%".
Use the matchit or xmledit plug-in to extend this capability to XML/XHTML tags.
'. Move cursor to previously modified line.
'a Move cursor to line mark "a" generated by marking with keystroke "ma"
'A Move cursor to line mark "a" (global between buffers) generated by marking with keystroke "mA"
]' Move cursor to next lower case mark.
[' Move cursor to previous lower case mark.
Find/Replace:
Example:
:%s/fff/rrrrr/ - For all lines in a file, find string "fff" and replace with string "rrrrr" for the first instance on a line.
:%s/fff/rrrrr/g - For all lines in a file, find string "fff" and replace with string "rrrrr" for each instance on a line.
:%s/fff/rrrrr/gc - For all lines in a file, find string "fff" and replace with string "rrrrr" for each instance on a line. Ask for confirmation
:%s/fff/rrrrr/gi - For all lines in a file, find string "fff" and replace with string "rrrrr" for each instance on a line. Case insensitive.
For more info type:
:help substitute
:help pattern
:help gdefault
:help cmdline-ranges
Delete all lines beginning with or matching a pattern:
:1,$ /^#/d
Delete all (first to last line: 1,$ or g) comments lines in file. Delete all lines beginning (^) with "#" (specify text pattern).
:g/#/d
Delete all lines (g) containing comments (comments follow "#") in file. Delete all lines containing "#".
:g!/^#/d
Delete all lines except (g! or v) comment lines beginning (^) with "#".
Executing Unix commands in vi:
Any UNIX command can be executed from the vi command line by typing an "!" before the UNIX command.
Examples:
":!pwd" - shows your current working directory.
":r !date" - reads the results from the date command into a new line following the cursor.
":r !ls -1" - Place after the cursor, the current directory listing displayed as a single column.
资料来源主要来自于:
http://www.yolinux.com/TUTORIALS/LinuxTutorialAdvanced_vi.html
more vi learning material: http://www.jerrywang.net/vi/vitutor4.html