There are two main modes in VI: command and insert. To that we can add the "ex" mode, called up with ":". Cursor mode is the default mode when starting vi, it's also the top-level mode. To go back to it when in any other mode, press ESC a few times (once is actually enough for most command modes).
There are tons of ways to move the cursor around, so I'll just give the basic ones:
Normal motion: cursor keys, (which are emulated by h,j,k,l if the terminal (or the currrent keymap) doesn't support cursor keys)
Line motion: ^ go to the first non blank character in current line, $ last character in current line
gg go to beginning of file Goto: G go to end of file, xxG go to line xx
In order to repeat a command several times, you may specify the number of repeats and then the command.(eg: 10h will move the cursor 10 characters to the left)
Now that you can move the cursor around, let's see how you can type text:
BE CAREFUL:
When in insert mode, you can delete only the text you've just insered.To delete other parts of the file, get out of insert mode (ESC) and go toDeleting Text
Vim specific: If you add the following command to your ~/.vimrc or type it while running vim you will be able to backspace through the entire buffer, not just the text entered during the current "input session". :set backspace=2 |
When you delete text that has just been inserted, see that the letters aren't erased from the screen. Instead the cursor just moves backwards and what you type will overwrite what you had "deleted". Just get used to it.
Vim specific: This occured in original vi, and apparently in some modern clones (it was probably originally due to lack of processing power). In vim this isn't a problem -- all the characters you delete will disappear immediately, except perhaps when being used over a slow dialup line, but then the problem is the dialup line and not vi(m). |
Most common insertion commands, all the following commands will get you in insert mode, but in different ways:
Appending (a): insert text after current cursor position
Inserting (i): insert text before current cursor position
Open New Line (o): open new line below current line and insert
All those commands have their counterparts in capital letters:
Appending at end of line (A): insert text after last character on the line
Inserting at beginning of line (I): insert text before first non blanck character on the line
Open New Line (O): open new line above current line and insert
N.B.: insertion (as well as most commands) can be combined with a "multiplier", in the same way than cursor commands: |
10a(type your text...) will repeat 10 times the command a. So vi will append the text you've just typed in ten times.
The delete command, as many others can be mixed with cursor commands, so that all text is deleted between the current cursor position and the new cursor position.
Deletion commands:
Deleting a character (x or X): deletes the character under (x) or before (X) the current cursor position
Deleting a word (dw or db): deletes word after (dw) or before (db) cursor
Deleting half a line (d^ or d$): deletes from the beginning of the line to the current cursor's position (d^) or from the cursor's position to the end of the line (d$)
Deleting a line (dd): deletes current line
Deletion can be combined with a "multiplier", in the same way as cursor commands: 10dd will delete 10 lines |
Deleted text will be stored in the 'yank' buffer, replacing the previous one. (see Yanking and Pasting) |
Copy and paste in vi is very powerful. We'll concentrate on single-buffer yanking, but be informed that, just like emacs, you can copy text into many buffers, although it is not automated.
Yanking text means 'copy'. The corresponding command is y and its syntax is exactly that same as delete's:
Yanking commands:
Yanking a word (yw or yb): yanks word after (yw) or before (yb) cursor
Yanking half a line (y^ or y$): yanks line from the current cursor position to the first non-blank character on the line (y^) or from current to the end of line (y$)
Yanking a line (yy): yanks current line
WARNING: entering insert mode will erase the yank buffer.
Vim specific: Vim doesn't erase the yank buffer when you enter insert mode. |
Pasting commands:
There is only one pasting command: p , which means paste text in yank buffer after current cursor position.There is also its counterpart P for pasting before cursor.