The vi editor (visual editor) - A quick reference guide.

Vi has three main modes Command mode, Insert mode and Command-Line mode.

vi modes

The editor begins in command mode, where cursor movement and copy/paste commands can be issued. If you are ever unsure which mode you're in, press Esc to return to command mode.

Insert text (Insert mode):

Open line above cursor Insert text at beginning of line Insert text at cursor append text after cursor Append text at line end Open line below cursor

Switch to Command mode:

Switch to command mode: [ESC]

Most commands execute as soon as typed except for "colon" commands which execute when you press the return key.

Cursor Movement (command mode):

Scroll Backward 1 screen Scroll Up 1/2 screen Go to beginning of line Go to line n Go to end of line Scroll Down 1/2 screen Go to line number ## Scroll Forward 1 screen Go to last line Scroll by sentence f/b Scroll by word f/b Move left, down, up, right Left 6 chars Scroll by paragraph f/b Move left, down, up, right Go to line # 6

Delete text (command mode):

Change word Replace one character Delete word Delete text at cursor Delete entire line (to buffer) Delete (backspace) text at cursor Delete 5 lines (to buffer) Delete current to end of line Delete lines 5-10

Editing (command mode):

Copy n lines Copy lines 1-2 /paste after 3 Paste above current line Paste below current line Move lines 4-5 /paste after 6 Join previous line Search backward for string Search forward for string Find next string occurrence % (entire file)
s
(search and replace) /old text with new/
c (confirm) g (global - all) Ignore case during search Repeat last command Undo previous command Undo all changes to line

Save and Quit (command mode):

Save changes to buffer Save changes and quit vi Save file to new file :w file Quit without saving Save lines to new file : 10,15 w file

In general a number n preceding any vi command will tell vi to repeat that command n times.

:syntax on Turn on syntax highlighting
:syntax off Turn off syntax highlighting
:set number Turn on Line numbering (shorthand :set nu)
:set nonumber Turn off Line numbering (shorthand :set nonu)

:set ignorecase Ignore case sensitivity when searching
:set noignorecase Restore case sensitivity (default)

:set autoindent Turn on Auto-indentation
Use the command >> to indent and the :set shiftwidth=4 Set indentation to four spaces
:set noautoindent Turn off Auto-indentation
Change all Windows CR/LF to Unix style LF line endings in the current file:
:g/^M/s///g
(To enter the ^M , type CTRL-V CTRL-M)

Command-line mode
Command-line mode commands are typed at the bottom of the screen.
To enter Command-line mode from Command mode, push colon : a colon will appear at the bottom.
Command-line (Ex) commands:

q [Enter] If you haven’t made any modifications, or have already saved them beforehand.
quit [Enter] If you haven’t made any modifications, or have already saved them beforehand.
q! [Enter] ignore any modifications and quit.
w [Enter] Save and return to Command mode.
wq [Enter] Save and quit.
x [Enter] Save and quit, same as wq

The Ex mode is similar to the command line mode as it also allows you to enter Ex commands. Unlike the command-line mode you won’t return to normal mode automatically. You can enter an Ex command by typing a Q in normal mode and leave it again with the :visual command. Note that the Ex mode is designed for Batch processing and as such won’t support mappings or command-line editing.

vi is pronounced as the two letters: "vee eye"

The vi editor was an update on the earlier ex (and ed ) editors, it introduced a Visual Interactive mode which was contracted to give the name vi .

“vi was written for a world that doesn’t exist anymore - unless you decide to get a satellite phone and use it to connect to the Net at 2400 baud” ~ Bill Joy

Related

Full list of VI commands
VIM tutor
Learning the vi Editor - How to use the vi editor.
History and effective use of Vim - Joe Nelson.
How to grok vi - Stack Overflow.
bcvi - Back Channel vi - remote file editing with SSH.
book - Practical vim by Drew Neil.
book - Learning the vi & vim Editor
MacVim & MacVim vs TextMate