vi
Text Editor: Part TwoSearching and replacing
/findme | go to the next instance of 'findme' |
?findme | go to the previous instance 'findme' |
N | repeat the previous seard, but in the opposite direction |
n | repeat the previous search, in the same direction (= find next) |
/\<word \> | find the next instance of the word 'word' |
/^function | find the next line staffing with 'function' |
/^[0-9][abc] | find the next line beginning with 0a, 0b, 0c, 1a, 1b, or 1c, etc. |
:range s/old / new /g | find and replace all instances of the old string in range with the new string. Omitting the /g will cause it to replace only the first instance on each line. See Range specifiers section. |
Range specifiers
These are used in copy, move,write, and substitute commands. This list is not complete.
56 | line 56 |
99,125 | lines 99 through 125; 99 and 125 are affected |
% | the whole buffer (= the file you are editing) |
. | the current line |
.,$ | the current line through the end of the buffer |
.,.+4 | the current line through the fourth line following the current line |
'a,'b | from mark a through mark b . To set a mark, put the cursor where you want the mark; now press mx where x is a letter from a through z. |
copying and moving text
Delete commands remove the text at the old location; yank commands make a copy, leaving the original where it was. Unless you use named buffers, deletions and cuts go into an anonymous buffer; each deletion or cut erases the previous contents of that buffer. You can do many put commands on the same buffer contents, as put copies, but does not delete, the buffer contents. To save and put several different cuts, precede the delete, yank, and put commands with "x, where x is a letter from a to z. This creates a named buffer, which will not be automatically clobbered. If you want to add material to a named buffer, rather than erase its current contents, use the capital letter.
yy | yank the current line |
P | put the contents of the general purpose buffer before the current position. Characters or words go before the current word, lines go before the current line. |
p | put the contents of the buffer after the current position. Characters or words go after the current word, lines go after the current line |
"bdd | delete this line, putting it in named buffer b |
"bp | put a copy of the material in buffer b at the current position |
"B13,16d | delete lines 13 through 16, and append them to the contents of buffer b. Do not erase buyer b's old contents. |
'c,'dm | move the block from mark c through mark d to the current cursor position |
'b,.co25 | copy the lines from mark b through the current line to line 25 |
Writing text
:w fname | write the entire file to fname |
:56,$w fname | write lines 56 to the end to fname |
:45,'fw"g | write lines 45 through the line marked with mark f to buffer g |
Panic avoidance
U | Undo all changes to this line |
u | undo the last change you made |
:q! | quit without saving changes. [Without the exclamation point, :q will keep prompting you to save the file firsts] |
Other useful commands
:! command | fork a shell, do the command |
:!! command | fork a shell, do the command, and put output in the file being edited |
:r filename | insert the contents of the file filename at the current spot |
:sh | fork an interactive shell, return to vi with CTRL-D |
Terminal type affects how vi runs
If vi thinks your terminal cannot handle full screen editing, you may find yourself in "open" mode. You need to type a capital Q then type :q! To get out without garbaging the file. You must now convince vi that your terminal can do full screen editing. Set the terminal variable to what ever your terminal or emulator is % set term= then type in your terminal type. It recognizes your terminal type as a useable terminal type.