Vi/Vim differences and quick reference
The key to effective vi use is to spot the symmetries in the commands.Note that if you have a command
c then shift c will often do the same as command
c but do it backwards/before, eg: `/' searches forwards, `?' searches backwards;
`p' pastes text after the cursor, `P' pastes text before the cursor.
Many commands can be prefixed with a count, eg: `5w' moves forwards 5 words;
`5x' deletes 5 characters.
For effective use of vi you must master the movement commands, they are used in conjunction with others, eg: `dw' deletes the next word (and `5dw' or `d5w' deletes the next 5 words); `c/fred' changes from the current position to the next occurrence of `fred'.
| h | move one character left |
| j | move one line down |
| k | move one line up |
| l | move one character right |
| H | move to Home (start of first line displayed) |
| M | move to start of Middle line displayed |
| L | move to start of Last line displayed |
| 0 | move to start of current line |
| ^ | move to first non white space character on current line |
| $ | move to end of current line |
| tc | move forwards to just before character `c' |
| Tc | move backwards to just after character `c' |
| fc | move forwards on to character `c' |
| Fc | move backwards on to character `c' |
| ; | repeat last t, T, f, F command |
| w | move forwards to the start of the next word |
| e | move forwards to the end of the next word |
| b | move backwards a word |
| % | move to matching bracket: () [] {} |
| / | Search forwards for regular expression |
| ? | Search backwards for regular expression |
| n | repeat last search in the same direction |
| N | repeat last search in the reverse direction |
| i | Insert before the current character |
| a | Insert after the current character |
| o | Open a new line after the current line |
| O | Open a new line before the current line |
| I | Insert before first non white space character on the current line |
| A | Insert after the last non white character on the current line |
Entering insert mode: In insert mode characters are inserted into the buffer, to leave it type Escape. To insert a special character type `C-vc' which inserts character `c' without interpretation (useful with Escape).
| x | Delete the character under the cursor |
| dM | Delete to where the movement `M' would take the cursor |
| dd | Delete the current line |
| D | Delete from the current position to the end of line |
| rc | Replace the current character with `c'. |
| s | Substitute the current character: enter insert mode |
| cM | Change to where the movement `M' would take the cursor: enter insert mode |
| cc | Change the entire current line: enter insert mode |
| C | Change from the current position to the end of line |
| R | Replace text, end with Escape |
| o | open new line below, end with Escape |
| O | open new line above, end with Escape |
These store text, if a buffer is not named text is put into the current yank buffer. Deleted text in put into buffer '1', text that was in `1' is put into `2', ... to `9'. Buffers can also be named with a lower case letter, these names buffers may be used to copy text between files. To use a named buffer prefix a command with `"N', eg: `"add' deletes the current line and puts it into yank buffer `a', `"ap' pastes the contents of yank buffer `a'.
| yM | Copy text into yank buffer to where the movement `M' would take the cursor |
| yy | Copy the current line into the yank buffer |
| Y | Copy from the current position to the end of line into the yank buffer |
| p | Paste the text into the yank buffer after the current line |
| P | Paste the text into the yank buffer before the current line |
In many of these `%' will be substituted with the name of the current file, and `#' with the name of the previous file. Some commands may be prefixed with line number ranges. `.' means the current line, `$' means the last line in the file: `2,5 s/fred/joe/' changes the first `fred' for `joe' on lines 2, 3, 4, 5; `1,$ s/fred/joe/g' changes every `fred' for `joe' in the buffer. `%' may be used as a number range and is short for `1,$'. Number ranges may also be: `.+N' (N lines after current line), `$-N' (N lines before end of buffer), `/fred/+N' (N lines after next `fred'). If you only want one line, omit the comma and second number: `.-4 s/fred/joe/'.
| :q | Quit |
| :q! | Quit without the sanity checks (unsaved buffers, etc) |
| :w [f] | Write the buffer to file `f' |
| :w !cmd | Write the buffer as stdin to shell command `cmd' |
| :wq | Write and quit |
| :x | Like `:wq' but only write if changes have been made |
| :r file | Read file into the current buffer after current line |
| :e f | Edit file `f'. |
| :e # | Edit the previous file, often bound to C-^ |
| :n | Edit next file |
| :p | Edit previous file |
| :rew | Rewind to first file |
| :! cmd | Execute shell command `cmd' |
| :s/a/b/ | Substitute `a' with `b' on the current line. |
| :s/a/b/g | Substitute every `a' with `b' on the current line. (g means global) |
| :s/a/b/gc | Substitute every `a' with `b' on the current line, ask for confirmation |
| :d | Delete the current line |
| C-e | scroll line with cursor up a line |
| C-y | scroll line with cursor down a line |
| C-d | scroll display down 1/2 a screen |
| C-u | scroll display up 1/2 a screen |
| C-f | scroll display forward a screen |
| C-b | scroll display backward a screen |
| C-l | redisplay the screen |
| J | Join the next line to the current line |
| u | undo the last change, vim: repeat for previous changes vi: a second `u' undoes the first undo |
| C-r | Redo a change undone by `u' (vim only) |
| U | Undo all the changes made to the current line |
| . | Repeat the last change command |
| ~ | Invert case (upper/lower) of the current character |
| !!cmd | Replace the current line with the current line filtered by the shell command `cmd' |
| C-g | Display current line number |
| G | Move to the last line in the buffer |
| nG | Move to line number n in the buffer |
| ml | Mark the current line with the label `l' |
| 'l | Go to the line with label `l' |
| '' | Go to the last line that you jumped from Labels may be used in `:' commands: `'s,'e:s/fred/joe/g' changes all `fred' for `joe' for all lines between those labeled `s' and `e'. |
| nC | Repeats the command C n times |
| qr | Start recording into register `r' (vim only). If `r' in upper case append |
| q | Stop recording |
| @r | Execute keystrokes in register `r' |
| << | Move text left by an indent |
| >> | Move text right by an indent |
| C-z | Suspend (shell job control) |
| v | Start character visual mode |
| V | Start line visual mode |
| c-V | Start block visual mode |
| Once a visual area is marked: | |
| d | delete |
| c | change |
| y | yank |
| ~ | swap case |
| u | make lower case |
| U | make upper case |
| ! | filter through external program |
| :dis | Display the contents of all yank buffers |
| C-ws | Split the window in two |
| C-wc | Close the current window |
| C-wM | Move to window in direction M, eg `k' means up |
| NC-w+ | Increase display size of current window by N lines |
| NC-w- | Decrease display size of current window by N lines |
| :files | Display files currently being edited and buffer numbers |
| :N b | Switch to buffer N in the current window |
| set ic | Ignore case in searches |
| set number | Line number mode |
| set showmode | Display `INSERT' on bottom line in insert mode |
| syntax off | Switch off syntax highlighting |
Misc vim additions (ie not in vi)
- In `:' mode the line can be edited with arrow keys (including up/down)
- Search patterns (enter with `/') can also be edited
Handy sequences
- A combination of `n' and `.' may be used to do an interactive search and replace.
- `xp' swaps two adjacent characters
- `10dd' deletes 10 lines
- `:.,$d' deletes from the current line to the end of the buffer
- `:. w ! od -c' filter the current line through `od' to see if it contains any strange characters.
- `:. w ! sh' Execute the current line as a shell command
If you want a tutorial use the command:
vimtutorialThe commands below are the most commonly used commands, but by no means all.
Other resources on vi/vim
http://www.viemu.com/a-why-vi-vim.htmlhttp://www.viemu.com/a_vi_vim_graphical_cheat_sheet_tutorial.html
http://jmcpherson.org/editing.html
If you want any help using the above, or have any comments or suggestions, please contact us.
