Вы находитесь на странице: 1из 4

Vi Unix Editor Reference

Contents
Insert Commands Miscellaneous Commands Movement Commands Word/Line Screen Text Modification Commands Delete Change Text Yank and Put Commands Moving Text Copying Text Moving/Copying Text Between Files Saving, Quitting, and Colon Commands Search and Replace Commands Customization - Set Options ($HOME/.exrc)

The vi editor has several modes used to complete file editing tasks. The insert and append modes allow the user to type in changes. All other editor commands (such as deletions, cut and paste, etc) are done in the command mode. To enter command mode press the ESC key. Some commands will put you into one of the other modes, so before using a command mode sequence press ESC to assure you are in the correct mode. All commands are case sensitve, like UNIX. Many of the commands will operate multiple times when the command is preceeded with a number. Example, deleting a single line is dd and deleting 5 lines is 5dd.

Insert Commands
i a I A r R o O (Issue one of these commands, after hitting ESC, to begin typing or changing the text) insert mode - can begin typing right where the cursor is in the file. append mode insert at beginning of line append at end of line replace a single character overstrike mode open a line below the cursor - Insert mode will be turned on and the cursor moved down 1 line open a line above the cursor - Insert mode will be turned on and the cursor moved up 1 line

Miscellaneous Commands
ctrl-g ctrl-l J . u U ~ xp show status line, number of lines redraw screen join two lines together repeat the last command undo the last command restore line to original change the case of character transpose two characters

Movement Commands
Word/Line
h l j k w W b B e ^ $ #<return> go one space left go one space right go one line down go one line up go to next word go to next space go to start of word go to previous space go to end of word beginning of line end of line Down # lines

Screen
ctrl-d ctrl-u ctrl-f ctrl-b H M L 1G G xG mx `x go down half a screen go up half a screen go forward a screen go back a screen go to top of screen go to middle of screen go to bottom of screen go to top of file go to end of file go to line x mark this line by x go to line marked by x

Text Modification Commands


Delete
dd xdd dw dW db dB D x X dO d$ delete the current line delete x lines delete to end of word delete to the next space delete to beginning of word delete to the previous space delete to end of line delete character delete character to left delete from beginning of line to last character before cursor delete from cursor to the end of line

Change
cc xcc cw cW cb cB C s S change the current line change x lines change to end of word change to the next space change to beginning of word change to previous space change to end of line substitute text for character substitute for entire line

Text Yank and Put Commands


p P yy nyy nyw (Like Copy and Paste) put the last deleted/yanked lines(s) after current line put the last deleted/yanked lines(s) before current line yank the current line into the default buffer yank n lines into the default buffer yank n words into the default buffer

Moving Text
1. 2. 3. First place the text to be moved into a buffer using dd, ndd, dw, x, or nx (i.e. any delete operation). Move the cursor to the new location. Paste the text at the new location using p or P.

Copying Text
2. 3. First, place the text to be copied into a buffer using yy, nyy, yw, or nyw (i.e. any yank operation). Move the cursor to the new location. Paste the copy of the text at the new location using p or P. 1.

Moving/Copying Text Between Files


NOTE: Use of a named buffer and the :e filename command is required to move/copy text between files. 1. vi the file containing the text to be moved/copied. 2. Next place the text to be moved/copied into a named buffer. Valid names for a buffer are 1-9 and a-z; the buffer name can only be 1 character long. Use the double quote " to indicate use of a named buffer. Example: "r3yy will yank (or copy) 3 lines into the buffer named r. 3. Edit the file that you wish to move/copy the text to. Example: :e newfile will close the original file without saving it and open the newfile to be edited. 4. Use the named buffer you created to paste the text in the desired location in the new file. Example: "rp will paste the lines in the buffer named r after the current line.

Saving, Quitting, and Colon Commands


:w :w file :wq :q :q! ZZ :r file :e file :! Cmd :r! cmd write the contents of the editor to the original file write the contents of the editor to file write out the file and quit the editor quit the editor (allowed only if no changes) quit the editor and discard any changes write out the file and quit the editor read the contents of file into editor edit a different file execute cmd read output of cmd into text

Search and Replace Commands


search forward through the text for pattern search backward through the text for pattern repeat the most recent search show all lines containing pattern. :1,$g/compiler/p will show all lines containing the pattern compiler :beg,ends/patt1/patt2/g substitute patt2 for all patt1 found. :%s/notfound/found/g will change all occurences of notfound to found search backwards N /pattern ?pattern n :beg,endg/patt/p

Customization - Set Options ($HOME/.exrc)


display all set options :set all automatically indent following lines to the indentation of previous line :set autoindent ignore case during pattern matching :set ignorecase show special characters in file :set list display line numbers :set number :set shiftwidth=n width for shifting operators <<,>> display mode when in Insert, Append, or Replace mode :set showmode :set wrapmargin=n set right margin 80-n for autowrapping lines (inserting newlines), 0 turns off

Вам также может понравиться