Name:



Directions on using vi (vim)

vi is the default Linux text editor. It is commonly used by many Linux users although many also use Emacs. If you are interested in learning Emacs, see the separate set of notes on Emacs.

Vi is a simple editor. It has 3 basic modes: command mode, insert mode, replace mode.

Command mode: this is the default mode. Keystrokes are interpreted as commands. Many keystrokes can be preceded by a number such that the command is performed that many times. For instance, the dd command deletes the current line. 5dd deletes 5 consecutive lines.

Insert mode: this is the mode that is most similar to a word processor, as you type, the characters are entered into your document at the point of the cursor. With each keypress, the cursor advances to the next position.

Replace mode: keystrokes overwrite the characters, so it is like insert except that you are replacing what is already there.

In order to know which mode you are in, look at the bottom of the editor where you will either see “--INSERT--”, “--REPLACE--” or nothing (command mode). To toggle from one mode to another, from command mode press “i” to enter insert mode, “R” to enter replace mode, and from either of these modes, press escape to return to command mode.

In command mode, here are numerous commands you can enter. An * in the command denotes a number. This typically means “do this command * times” although is different for * shift+G.

Mode Commands

|Keystroke |Description |

|i |Enter insert mode at the immediate left of the cursor |

|a |Enter insert mode at the immediate right of the cursor |

|o |Insert a blank line after the current line and enter insert mode |

|O |Insert a blank line before the current line and enter insert mode |

|I |Enter insert mode at the beginning of the current line |

|A |Enter insert mode at the end of the current line |

|r |Replace one character with the next character entered |

|R |Enter replace mode and continue to replace (overwrite) characters (until escape is pressed) |

Cursor Movement Commands

|j, k, h, l |Move the cursor down/up/left/right one position (the arrow keys will probably NOT work) |

|w, b |Move the cursor forward/backward by one word |

|shift+G |Move to the last line of the file |

|1 shift+G |Move to the first line of the file |

|* shift+G |Move to line * (where * is a number) |

|H, L |Move to the top/bottom line of the screen |

|0, $ |Move to the first/last character of the line |

Repeat/Undo

|u |Undo the previous command (may be used multiple times to undo several commands) |

|. |Repeat the last edit command |

|*. |Repeat the last edit command * times |

Deletion and cut-and-paste commands

|x |Delete the next character |

|*x |Delete the next * characters (e.g., 5x to delete 5 consecutive characters) |

|dw |Delete the next word |

|*dw |Delete the next * words |

|dd |Delete this line |

|*dd |Delete * lines starting with the current line |

|D |Delete from the cursor to the end of this line |

|yy |Copy the current line into a buffer |

|p |Put any lines stored in the buffer below the current line |

|J |Join the next line to the end of the current line (remove the end-of-line) |

File commands

|:w |Save the file |

|:w name |Save the file as name |

|:q |Exit vi |

|:q! |Exit vi without saving |

|:wq |Save file and exit vi |

|:r name |Insert the file name into the current position of the current file |

Search commands

|/text |Search forward for text |

|/ |Search for the next occurrence of the most recent search |

|n |Search again for the most recent search |

|/regexpr |You can also search given a regular expression, for instance /^[0-9] will search for the next line that |

| |starts with a digit, or /^[^A-Z] will search for the next line that does not start with a capital letter |

More Advanced Cursor Movement Commands

|, |As you would expect, moves the file up or down one screen’s worth in the window, Ctrl+F and Ctrl+B do the |

|Ctrl+F, Ctrl+B |same thing |

|( ) { } |Move forward/backward one sentence/one paragraph (e.g., ‘(‘ moves you backward one sentence) |

|mchar |Enter ‘m’ followed by any letter (lower case) or digit to create a mark, there are 36 marks available (one |

| |for each of a-z, 0-9) |

|‘char |Jump to the position marked by that char |

|‘‘char |Jump to the position you were at before you last typed ‘char |

|% |If the cursor is placed on a C/C++ delimiter (e.g., {, }, (, ), [, ]) then % jumps you to the associated |

| |open or close delimiter, only useful when editing a program |

Other commands

:h – view the vi help file

Ctrl+T – return to your file from the help file

:set all – displays the configuration values for the current vi session (most of these you will not want to alter, but to alter any, you can type :set flag where flag is the name of a value that you want to toggle (on to off or off to on) such as noautoindent.

The / search command searches forward. You can similarly search backward using ? instead of /, as in ?text to search backward for text and ? to search further backward.

If you know the name of the command that you want to perform, you can also execute it by typing :!command where command is the name of the command (e.g., undo).

Quick vi cheat sheet

k

h l b ( word ( w

j

Top: 1G

Bottom: G

Line k: kG

dd – delete this line

kdd – delete k lines

D – delete from here forward on this line

u – undo

i – insert here

o – insert after this line

O – insert before this line

R – replace here

-- return from insert or replace mode to command mode

:w - save as

:w - save

:q - exit

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download