Vi tips & tricks

| | |
in the ancient unix days the editors with powers were created. nine editors were given to the X-windows .. 7 to the console .. 3 were stream editors .. but there was this ONE editor for stream, console and X , one to rule them all, one to bind them .. VI VI VI .....

Basic vim tips..

(Start writing in vi)

Using h,j,k,l you can move around (left,down,up,right). Pressing i, you enter the "insert" mode where you can start writing under the cursor, while with I you start at the beginning of the line. Likewise with a and A you start after the cursor and at the end of the line. press ESC to get back to normal mode where you can delete with x one letter and with dd a line. To quit vi, type :w to save ( or :w filename to save it under the name filename) and :q to quit. Shorter, you :wq to save and quit or :q! to quit without saving. (By pressing : in normal mode you enter command mode wich is the third vi mode).

lets get started ..

First of all, undo with u and redo with ctrl-r :) With :e file2 you can open a second file and close the first. But vi keeps all the buffers so you can copy-paste (yank-paste actually).. so to yank 3 lines 3yy, move to another file with :e file2 then p. To get all lines you only need a ggyG. To get the paragraph {y}. Paste is p in normal mode and ctrl-r " in insert mode. By the way you can easily move between two files with e#. Everybody (well almost) knows that you can delete a line with dd. The registers 0-9 keep the last 10 deleted things so you can see them with :reg and to paste number 6 type "6p in normal mode (or ctrl-r 6 in insert mode). The latest deleted is also in the register " and that's why ctrl-r " is paste in insert mode. Of course you can use your own registers to yank for example m : "myy (keep in register m the current line) and "mp to paste it. And it gets even better...

You can keep more things in one register if you capitalize the register after the first time: "myy to get the current line, them move two lines down, "Myy to get this one as well, go to the next paragraph, {"My} to get the paragraph etc.. and with "mp (or ctrl-r m in insert mode) you magically paste two lines and a paragraph. Use capital J to bind two lines and >> to move the line one tab right. The thing with vi is that it understands numbers: 6J will bind 6 six lines together, 8>> will move 8 lines and 18l will move the cursor 18 characters to the right ... Find and replace:
:%s/whattofind/replacewith/g for the whole file :%s/whattofind/replacewith/gw whole file but confirm each replace :2,35s/whattofind/replacewith/g only for lines 2-35 :5,$s/whattofind/replacewith/g from line 5 to the end
Capitalize everything? gg gUG. Lowercase everything? gg guG Auto-complete with ctrl-p in insert mode. Type /whattofind to search for whattofind .. but you can y/whattofind to yank untill whattofind (or d/whattofind to delete up to whattofind).. And you can repeat your last command with the dot . . But if you want to search for the word under the cursor you can easier do it with *. And if you would like the man-page of the word under the cursor ctrl-k. In insert mode ctrl-y copies the line above the current and ctrl-e the one underneath !!