To use the editor enter "vi filename", where filename is a new file or the
name of an existing file you wish to edit. To get out of
the vi editor and save the contents of the editor enter "ZZ"
(note ZZ must be in capitals).
To get out of the editor and not save any of the results enter
":q!".
vi is a full screen editor that has two modes, a Command Mode - where your
keystrokes are commands to the editor, and an Insert (input) Mode - where your
keystrokes are actual text you type in. When you first enter vi you are in
the command mode. If you enter the insert mode, the screen displays the
word "INPUT" in the lower right. Certain commands place you into the Insert
Mode (i,a,o,O,R,s commands). To exit the Insert Mode hit the Escape key
<ESC>.
USEFUL vi COMMANDS: (^ represents the control key)
Command-----Function
File manipulation:
$vi file-----edit file
ZZ-----write and quit vi (saves contents of the editor)
:w-----write w/o quitting
:!command-----execute shell command
:q!-----quit w/o writing (does Not save contents)
Cursor/screen movement:
^d ----- scroll down 1/2 page
^u ----- scroll up 1/2 page
^f ----- page forward 1 page
^b ----- page backward 1 page
<spacebar>----- move cursor right
<return> ----- next line
h ----- move cursor left
j ----- move cursor down
l ----- move cursor right
k ----- move cursor up
(Or use the Arrow Keys to move cursor)
Add text:
i-----insert characters before cursor (<ESC> to stop)
a-----append characters after cursor (<ESC> to stop)
o-----open line below cursor and add text (<ESC> to stop)
O-----open line above cursor and add text (<ESC> to stop)
Delete text:
dd-----delete current line (where cursor is)
5dd-----delete current line and next 4 lines
D-----delete from cursor to the end of the line
x-----delete current character (where cursor is)
10x-----delete 10 characters from the current position
Modify text:
r-----replace current character under cursor with 1 char.
R-----replace many characters under cursor (<ESC> to stop)
:s/old/new/g-----substitute old text with new text in current line
:1,$s/old/new/g-----substitute old text with new in line 1 to last
s-----delete character over cursor and append text
(<ESC> to stop)
5s-----delete 5 characters and append text (<ESC> to stop)
yy-----yank - copy current line into a buffer area
5yy-----yank - copy next 5 lines into buffer area
p-----put previously deleted or yanked text after cursor
Move/copy text:
:5,10 m 40-----move lines 5 thru 10 after line 40
:5,10 t 40-----copy lines 5 thru 10 after line 40
(you can also use the above delete/yank and put commands to move and copy text within the editor.)
Miscellaneous:
G----- move cursor to last line
1G-----move cursor to first line
5G-----move cursor to line 5
/string-----search forward for a string
n-----repeat string search forward
N-----repeat string search backwards
?string-----search backwards for a string
u-----undo last change (command)
.-----repeat last change (command)
^g-----status of current line and file
^l-----clear and redraw screen
~-----change upper case to lower and visa-versa
%-----show matching {} or () or []. Used when
cursor is positioned over one of the above.
<ESC>-----used to end the Insert Mode for certain commands
:set-----show what the vi settings are
:set number-----turn on line numbers
:set nonumber-----turn off line numbers
:set showmode-----turn on the showmode field (shows input mode)
:%s/old/new/g ------- Will search and replace the "old" with the "new"
TIP: Sometimes, I'll accidentally hit "Ctrl-X, Ctrl-S", because I'll think that I'm in emacs. If you do this VI will freeze. It's not actually VI that is freezing, but by hitting "Ctrl-S" you've stopped all terminal output, and the system is waiting for you to push "Ctrl-Q" to resume.