[phpwiki] Vi Crib Sheet

Overview

vi is a text editor available in most Unix and Linux variants.

vi has two modes, a line editing mode and a command mode. The command mode is access by pressing the <ESC> key. It is used for issuing guess what - commands. The following is a shorten summary of vi commands. Start vi in command line with:

 $ vi filename

Please report problems and direct all inquiries to Contact US.


Command List

Cursor Movement

	+-----------+-----------------------------------------------------------+
	|     h     | Move left one character.                                  |
	+-----------+-----------------------------------------------------------+
	|     j     | Move down one line.		                        |
	+-----------+-----------------------------------------------------------+
	|     k     | Move up one line.	                                        |
	+-----------+-----------------------------------------------------------+
	|     l     | Move right one character.                                 |
	+-----------+-----------------------------------------------------------+
	|     w     | Move right one word.                                      |
	+-----------+-----------------------------------------------------------+
	|     W     | Move right one word past punctuation (upper case W.)      |
	+-----------+-----------------------------------------------------------+
	|     b     | Move left one word.                                       |
	+-----------+-----------------------------------------------------------+
	|     B     | Move left one word past delimiter.                        |
	+-----------+-----------------------------------------------------------+
	|     e     | Move to end of current word.                              |
	+-----------+-----------------------------------------------------------+
	|<backspace>| Move left one character.                                  |
	+-----------+-----------------------------------------------------------+
	|<spacebar> | Move right one character.                                 |
	+-----------+-----------------------------------------------------------+
	|  <return> | Move down one line.                                       |
	+-----------+-----------------------------------------------------------+
	|     $     | Move to end of line.                                      |
	+-----------+-----------------------------------------------------------+
	|     0     | Move to begining of line; this is the zero key.           |
	+-----------+-----------------------------------------------------------+
	|     H     | Move to top of screen.                                    |
	+-----------+-----------------------------------------------------------+
	|     M     | Move to middle of screen.                                 |
	+-----------+-----------------------------------------------------------+
	|     L     | Move to bottom of screen.                                 |
	+-----------+-----------------------------------------------------------+
	| <ctrl> f  | Page forward one screen.                                  |
	+-----------+-----------------------------------------------------------+
	| <ctrl> d  | Scroll forward one-half screen.                           |
	+-----------+-----------------------------------------------------------+
	| <ctrl> b  | Scroll backward one screen.                               |
	+-----------+-----------------------------------------------------------+
	| <ctrl> u  | Scroll backward one-half screen.                          |
	+-----------+-----------------------------------------------------------+
	|    :88    | Move to line 88.                                          |
	+-----------+-----------------------------------------------------------+

Inserting Characters and Lines

	+-----------+-----------------------------------------------------------+
	|     a     | Insert characters to the right of the cursor.             |
	+-----------+-----------------------------------------------------------+
	|     A     | Insert characters at the end of the line.                 |
	+-----------+-----------------------------------------------------------+
	|     i     | Insert characters to the left of the cursor.              |
	+-----------+-----------------------------------------------------------+
	|     I     | Insert characters at the beginning of the line.           |
	+-----------+-----------------------------------------------------------+
	|     o     | Insert line below the cursor.                             |
	+-----------+-----------------------------------------------------------+
	|     O     | Insert line above the cursor.                             |
	+-----------+-----------------------------------------------------------+

Changing Text

	+-----------+-----------------------------------------------------------+
	|     cw    | Change word (or part of word) to right of cursor.         |
	+-----------+-----------------------------------------------------------+
	|     cc    | Change line.                                              |
	+-----------+-----------------------------------------------------------+
	|     C     | Change from cursor to end of line.                        |
	+-----------+-----------------------------------------------------------+
	|     s     | Substitute string for character(s) from cursor forward.   |
	+-----------+-----------------------------------------------------------+
	|     r     | Replace character at cursor with one other character.     |
	+-----------+-----------------------------------------------------------+
	| r <return>| Break line.                                               |
	+-----------+-----------------------------------------------------------+
	|     J     | Join current line and line below.                         |
	+-----------+-----------------------------------------------------------+
	|     xp    | Transpose character at cursor and character to the right. |
	+-----------+-----------------------------------------------------------+
	|     -     | Change case of letter (upper or lower.)                   |
	+-----------+-----------------------------------------------------------+
	|     u     | Undo previous command.                                    |
	+-----------+-----------------------------------------------------------+
	|     U     | undo all changes to current line.                         |
	+-----------+-----------------------------------------------------------+
	|     :u    | Undo previous last-line command.                          |
	+-----------+-----------------------------------------------------------+

Deleting Text

	+-----------+-----------------------------------------------------------+
	|     x     | Delete character at the cursor.                           |
	+-----------+-----------------------------------------------------------+
	|     X     | Delete character to the left of the cursor.               |
	+-----------+-----------------------------------------------------------+
	|     dw    | Delete word (or part of word to right of cursor.)         |
	+-----------+-----------------------------------------------------------+
	|     dd    | Delete line containing the cursor.                        |
	+-----------+-----------------------------------------------------------+
	|     D     | Delete part of line to right of cursor.                   |
	+-----------+-----------------------------------------------------------+
	|     dG    | Delete to end of file.                                    |
	+-----------+-----------------------------------------------------------+
	|     d1G   | Delete from beginning of file to cursor.                  |
	+-----------+-----------------------------------------------------------+
	|   :5,10d  | Delete lines 5-10                                         |
	+-----------+-----------------------------------------------------------+

Copying and Moving Text

	+-----------+-----------------------------------------------------------+
	|     yy    | Yank or copy line.                                        |
	+-----------+-----------------------------------------------------------+
	|      Y    | Yank or copy line.                                        |
	+-----------+-----------------------------------------------------------+
	|      P    | Put yanked or deleted line below current line.            |
	+-----------+-----------------------------------------------------------+
	|      p    | Put yanked or deleted line above current line.            |
	+-----------+-----------------------------------------------------------+
	|  :1,2co3  | Copy lines 1-2 and put after line 3.                      |
	+-----------+-----------------------------------------------------------+
	|  :4,5m6   | Move lines 4-5 and put after line 6.                      |
	+-----------+-----------------------------------------------------------+

Setting Line Numbers

	+-----------+-----------------------------------------------------------+
	| :set nu   | Show line numbers.                                        |
	+-----------+-----------------------------------------------------------+
	| :set nonu | Hide line numbers.                                        |
	+-----------+-----------------------------------------------------------+

Setting Case-sensitivity

	+-----------+-----------------------------------------------------------+
	| :set ic   | Searches should ignore case.                              |
	+-----------+-----------------------------------------------------------+
	| :set noic: Searches should be case-sensitive.                         |
	+-----------+-----------------------------------------------------------+

Finding a Line

	+-----------+-----------------------------------------------------------+
	|     G     | Go to last line of the file.                              |
	+-----------+-----------------------------------------------------------+
	|    1G     | Go to first line of the file.                             |
	+-----------+-----------------------------------------------------------+
	|    21G    | Go to line 21.                                            |
	+-----------+-----------------------------------------------------------+

Searching and Replacing.

	+----------------------+-----------------------------------------------------------+
	|   fc                 | Move to next occurence of char 'c' in current line.       |
	+----------------------+-----------------------------------------------------------+
	|   Fc                 | Move backward to previous occurence of char 'c'.          |
	+----------------------+-----------------------------------------------------------+
	|   %                  | Move to the associated end bracket ')', '}' or ']'.       |
	+----------------------+-----------------------------------------------------------+
	|  /String             | Search for 'String.'                                      |
	+----------------------+-----------------------------------------------------------+
	|  ?string             | Search backward for 'String'.                             |
	+----------------------+-----------------------------------------------------------+
	|     n                | Find next ocurrence of string in search direction.        |
	+----------------------+-----------------------------------------------------------+
	|     N                | Find previous occurence of stirng in search direction.    |
	+----------------------+-----------------------------------------------------------+
	|:g/search/s//replace/g| Search and replace for all occurence. add c to confirm.   |
	+----------------------+-----------------------------------------------------------+

note: regular expressions can be use in place of search string. Read more in Wikipedia's Regular Expression Examples

Clearing the Screen.

	+-----------+-----------------------------------------------------------+
	|  <ctrl> L | Clear (refresh) scrambled screen.                         |
	+-----------+-----------------------------------------------------------+

Inserting a file into a File.

	+--------------+-----------------------------------------------------------+
	|:r filename   | Insert (read) file after cursor.                          |
	+--------------+-----------------------------------------------------------+
	|:34 r filename| Insert file after line 34.                                |
	+--------------+-----------------------------------------------------------+

Saving and Quitting.

	+-----------+-----------------------------------------------------------+
	|     :w    | Save changes (write buffer.)                              |
	+-----------+-----------------------------------------------------------+
	|:w filename| Write buffer to named file.                               |
	+-----------+-----------------------------------------------------------+
	|    :wq    | Save changes and quit vi.                                 |
	+-----------+-----------------------------------------------------------+
	|     ZZ    | Save changes and quit vi.                                 |
	+-----------+-----------------------------------------------------------+
	|    :q!    | Quit without saving changes.                              |
	+-----------+-----------------------------------------------------------+

References


Page locked (last edited November 15, 2009) [info] [diff])
FindPage by browsing or searching
5 best incoming links: FrontPage (4)
5 best outgoing links:
5 most popular nearby: FrontPage (2047)