Saving the file you're working on and/or leaving vi:
:wq write the file to disk and quit
:q! quit without saving any changes
:w! newfile write all lines from the entire current file into the file 'newfile',
overwriting any existing newfile
:n,m w! newfile write the lines from n to m, inclusive, into the file newfile,
overwriting any existing newfile
|
Moving Around In a File
Key(s) Movement
h one space to the left (also try left arrow)
j one line down (also try down arrow)
k one line up (also try up arrow)
l one space to the right (also try right arrow)
w forward word by word
b backward word by word
e end of next word
^ beginning of current line
0 (zero) to beginning of line
$ to end of current line
Enter beginning first word on the next line
G to last line of file
1G to first line of file
:n line n; use :0 to move the beginning of the file
H to top line of screen
M to middle line of screen
L to last line of screen
ctrl-f jump forward one screen
ctrl-b jump backward one screen
ctrl-d scroll down one-half screen
ctrl-u scroll up one-half screen
% the matching (, ), [, ], {, or }
(Press % with your cursor on one of these characters to move your cursor its mate.)
|
Moving by searching. To move quickly in a file, search for text. In command mode, type a / (slash) followed by the text to search for. Press Return. The cursor moves to the first occurrence of that text. Repeat the search in a forward direction by typing n (lower case), or in a backward direction by typing N (upper case).
Many commands take number prefixes; for example 5w moves to the right by 5 words.
Searching for Text
Type: To:
/string search down for string
?string search up for string
n repeat last search from present position
|
Substituing Text
Change all occurrences on the current line
Type :s/thee/the/g[Ret] meaning substitute globally on the line.
This changes all occurrences on the line.
Change every occurrence of a character string between two lines
Type :#,#s/old/new/g[Ret] where #,# are the numbers of the two lines.
Change every occurrence in the whole file
Type :%s/old/new/g[Ret] to change every occurrence in the whole file.
Inserting Text
Type: To:
a append starting right of cursor
A append at the end of the current line
i insert starting left of cursor
I insert at beginning of the current line
o open line below cursor, then enter insert mode
O open line above cursor, then enter insert mode
:r newfile add the contents of the file newfile starting below the current line
|
Deleting Text
Type: To:
x delete single character; 5x deletes 5 characters
dw delete word; 5dw deletes 5 words
dd delete line; 5dd deletes ... well you get the idea!
cw delete word, leaves you in insert mode (i.e. change word)
cc change line -- delete line and start insert mode
s change character -- delete character and start insert mode
D delete from cursor to end of line
C change from cursor to end of line -- delete and start insert mode
u undo last change
U undo all changes to current line
J join current line with line that follows (press Enter in insert mode to split line)
|
Cutting and Pasting
Type: To:
xp transpose two characters (two commands, x followed by p)
yy yank (i.e. copy) one line into a general buffer (5yy to yank 5 lines)
"ayy yank into the buffer named a
P put the general buffer back before the current line
"aP put from buffer a before current line
p put the general buffer back after the current line
"ap put from buffer a after the current line
|
Note: dd or any other delete will save a copy of the deleted string in the same general buffer. Therefore a cut and paste can be done with dd and p, rather than copy and paste with yy and p. But make sure not to overwrite the buffer with some other yank or delete command before you have a chance to paste.
Miscellaneous Commands
Type: To:
Ctrl-g show line number of current line
Ctrl-l redraw the entire display
:!sh fork a shell; type Ctrl-d to get back to vi
. repeat last text change command at current cursor position
|
No comments:
Post a Comment