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

To startup vi:

vi filename

Insert text (Insert mode):

Vi has two modes insert mode and command mode. The editor begins in command mode, where cursor movement and copy/paste commands may be issued.

   
Open line above cursor
O        
Insert text at beginning of line
I
Insert text at cursor
i
append text after cursor
a
Append text at line end
A
   
Open line below cursor
o        

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
[ctrl] b  
   
Scroll Up 1/2 screen
[ctrl] u  
Go to beginning of line
0
Go to line n
nG
Go to end of line
$
   
Scroll Down 1/2 screen
[ctrl] d
Go to line number ##
:##
   
Scroll Forward 1 screen
[ctrl] f  
   
Go to last line
G  
Scroll by sentence f/b
( )      
Scroll by word f/b
w b
Move left, down, up, right
h j k l
Left 6 chars
6h
Scroll by paragraph f/b
{ }
Move left, down, up, right
Arrow Keys
Go to line #6
6G

Delete text (command mode):

Change word
cw
Replace one character
r    
Delete word
dw
Delete text at cursor
x
Delete entire line (to buffer)
dd
   
Delete (backspace) text at cursor
X
Delete 5 lines (to buffer)
5dd
   
Delete current to end of line
D
Delete lines 5-10
:5,10d

Editing (command mode):

Copy line
yy
Copy n lines
nyy
Copy lines 1-2 /paste after 3
:1,2t3
Paste above current line
P
     
Paste below current line
p    
Move lines 4-5 /paste after 6
:4,5m6
       
Join previous line
J
Search backward for string
?string
Search forward for string
/string
Find next string occurrence
n
% (entire file) s (search and replace) /old text with new/ c (confirm) g (global - all)
:%s/oldstring/newstring/cg    
Ignore case during search
:set ic
Repeat last command
.
Undo previous command
u
Undo all changes to line
U

Save and Quit (command mode):

Save changes to buffer
:w
Save changes and quit vi
zz or :wq
Save file to new file
:w file
 
Quit without saving
:q!
Save lines to new file
:10,15w 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 << command to outdent
: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)

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
Programmers Vim cheat sheet
VIM tutor
How to grok vi - Stack Overflow
HappyEdit - The modern Vim-inspired text editor
vim.spf13 - The Ultimate Vim Distribution
VIM from novice to professional - Video by Derek Wyatt
bcvi - Back Channel vi - remote file editing with SSH
Book - Learning the vi & vim Editor
MacVim & MacVim vs TextMate


© Copyright SS64.com 1999-2013
Some rights reserved