vi Basics

You can start vi by typing "vi" and hitting return. Most people, however, know the name of a file they want to edit or the name of a new file they want to create, so something like the following

vi NameOfFileToEditOrCreate.txt
followed by a return is more common.

To exit from vi, saving whatever work you've just done, hit the <Esc> key and then type ":wq" and return.

To exit from vi, NOT saving any of your edits, hit the <Esc> key and then type ":q!" and return.


The most important thing to know about vi is that it is always operating in one of two modes: Insert mode or Command mode. In Insert mode, *everything* you type will be inserted into the file you are editing, up to the point when you exit insert mode using the <Esc> key. In command mode, you can cut-n-paste, move the cursor, search, delete characters, lines or words, or modify them etc. In the discussion above, hitting escape made sure you were out of insert mode and into command mode. Typing :wq commanded vi to "(w)rite the file and then (q)uit".

The second most important thing to know is that the mouse is now pretty much useless to you while in vi. It cannot be used to move around in the file.

The third most important thing you should know about vi is that almost *every* key on the keyboard (loevercase, uppercase and control shifted) does something in command mode. So, be careful what you type. It's unlikely you'll every discover anything useful about vi by accident!

Since everyone makes typing mistakes (some with disastrous consequences in command mode), the most useful command is the lowercase letter 'u'. Lowercase 'u' will undo the immediately preceding change, whatever it was, no matter how complex or extensive, which definitely makes 'u' one of the most useful of vi commands.

Finally, you need to master a basic set of commands. When vi starts, it is automatically in command mode, so your first commands will normally be to move the cursor and start inserting. The "current line" is whatever line the cursor is on.

  

Enter text:

i Insert at the cursor position. All characters typed after 'i' and before <Est> will be inserted. a Append after the cursor position. All characters typed after 'a' and before <Est> will be inserted.

Move cursor:

h,j,k,l Move cursor left, down, up and right, respectively. Check it out, these keys are all under your right hand on the keyboard when your hands are in a 'standard' typing position. $ Move cursor to the end of the current line. :1 Move cursor to line 1. Substitue any line number & cursor will move to that line. :$ Move cursor to the last line. % Typing a % while cursor is resting on a { or } will move cursor to the paired } or { . Very handy for programming! Also works with ()'s & []'s

Editing:

x Lower case 'x' deletes the single character under the cursor. D Upper case 'D' deletes whatevers under the cursor to the end of the line. "d$" does the same thing. dd Deletes the line the cursor is on. Typing some number first will delete that many lines. For instance "5dd" will delete the current line and the next four. These lines will ALSO be copied into a buffer (aka clipboard). yy Copies the line the cursor is on into a buffer without modifying the current text. The letter 'y' stands for "yank". Typing some number first will yank that many lines. For instance "5yy" will yank a copy of the current line and the next four into a buffer. yw Copies the word the cursor is on into a buffer without modifying the current text. Typing some number first will yank that many words. p Lower case 'p', pastes whatever's in the buffer after the cursor position. P Upper case 'P', pastes whatever's in the buffer at the cursor position. J Upper case 'J' joins the following line to the end of the current one

Other:

<Ctrl>g displays number of lines in the file AND the filename :w Save all changes to the file that you have made so far. Stands for "write". Will not exit vi :q! Exit vi without saving any changes. Stands for "quit". If you haven't made any changes, the exclamation point is optional :wq Save all changes and then exit vi.

More Complex:

:51,100s/old/new/g global substituion on only on lines 51 through 100 :%s/old/new/g global substituion on entire file. :%s/(ctrl-v then m)/\r/g get's rid of annoyiny CtrlM end-of-lines