Common Vi Commands For DBA's
On Unix, the most commonly used editor is vi, new users often find Vi difficult to use at time. The guide below will be useful to new / seldom users of vi.

 

Command mode
This is the default when you enter vi. In command mode, most letters, or short sequences of letters, that you type will be interpreted as commands, without explicitly pressing Enter . If you press Esc when you're in command mode, your terminal will beep at you. This is a very good way to tell when you're in command mode.

Insert mode
In insert mode, whatever you type is inserted in the file at the cursor position. Type a (lowercase letter a, for append) to enter insert mode from command mode; press Esc to end insert mode, and return to command mode.

Line mode
Use line mode to enter line oriented commands. In command mode, type a colon ( : ). Your cursor moves to the bottom of the screen, by a colon prompt. Type a line mode command, then press Enter. Any sensible command from the UNIX line editor ex will work, and a few are good to know about. These commands are indicated by a colon in front of the command. Each time you use a line mode command, you must type a colon to enter line mode, then type the command by the colon prompt at the bottom of the screen, then press Enter when you finish typing the command.

 

Starting vi
vi filename start editing filename, create it if necessary
Saving the file you're working on and/or leaving vi:
:wq write the file to disk and quit
:q! quit without saving any changes
:w! newfile write all lines from the entire current file into the file 'newfile', overwriting any existing newfile
:n,m w! newfile write the lines from n to m, inclusive, into the file newfile, overwriting any existing newfile
Moving the Cursor
h one space to the left (also try left arrow)
j one line down (also try down arrow)
k one line up (also try up arrow)
l one space to the right (also try right arrow)
$ end of current line
^ beginning of current line
Enter beginning first word on the next line
G end of file
:n line n
w beginning of next word
e end of next word
b beginning of previous word
Ctrl-b one page up
Ctrl-f one page down
% the matching (, ), [, ], {, or }
(Press % with your cursor on one of these characters to move your cursor its mate.)
Searching for Text
/string search down for string
?string search up for string
n repeat last search from present position
Inserting Text
a append starting right of cursor
A append at the end of the current line
i insert starting left of cursor
I insert at beginning of the current line
o open line below cursor, then enter insert mode
O open line above cursor, then enter insert mode
:r newfile add the contents of the file newfile starting below the current line
Deleting Text
x delete single character; 8x deletes 8 characters
dw delete word; 5dw deletes 5 words
dd delete line; 7dd deletes 7 lines
cw change word - deletes word, leaves you in insert mode
cc change line -- delete line and start insert mode
s change character -- delete character and start insert mode
D delete from cursor to end of line
C change from cursor to end of line -- delete and start insert mode
u undo last change
U undo all changes to current line
J join current line with line that follows
Cutting and Pasting
xp transpose two characters (two commands, x followed by p)
yy yank one line into a general buffer (5yy to yank 5 lines)
"ayy yank into the buffer named a
P put the general buffer back before the current line
"aP put from buffer a before current line
p put the general buffer back after the current line
"ap put from buffer a after the current line
Miscellaneous Commands
Ctrl-g show line number of current line
Ctrl-l redraw the entire display
:!sh fork a shell; type Ctrl-d to get back to vi
. repeat last text change command at current cursor position
~ Changes case of current character

The .exrc file. It store you default setting when vi initialises. It is located in the login directory. These setting are by witched on by :set <command>.

The setting are switched off by :set no<command>

autoindent (ai) In insert mode, indents each line to the same level as the line above or below. Use with shiftwidth option.
autoprint (ap) Displays changes after each editor command. (For global replacement, displays last replacement.)
autowrite (aw) Automatically writes (saves) file if changed before opening another file with :n or before giving UNIX command with :!.
beautify (bf) Ignores all control characters during input (except tab, newline, or formfeed).
directory (dir) Names directory in which ex stores buffer files. (Directory must be writable.)
errorbells (eb) Sounds bell when an error occurs.
exrc (ex) Allows the execution of .exrc files that reside outside the user's home directory. (System V only.)
hardtabs (ht) Defines boundaries for terminal hardware tabs.
ignorecase (ic) Disregards case during a search.
lisp Inserts indents in appropriate lisp format. ( ), { }, [[, and ]] are modified to have meaning for lisp.
list Prints tabs as ^I; marks ends of lines with $.
magic Wildcard characters . (dot), * (asterisk), and [] (brackets) have special meaning in patterns.
number (nu) Displays line numbers on left of screen during editing session.
open Allows entry to open or visual mode from ex.
prompt Displays the ex prompt (:) when vi's Q command is given.
readonly (ro) Any writes (saves) of a file will fail unless you use ! after the write (works with w, ZZ, or autowrite).
shell (sh) Pathname of shell used for shell escape (:!) and shell command (:sh). Default value is derived from shell environment, which varies on different systems.
shiftwidth (sw) Defines number of spaces in backward (^D) tabs when using the autoindent option.
showmatch In vi, when ) or } is entered, cursor moves briefly to matching ( or {. (If match is not on the screen, rings the error message bell.) Very useful for programming.
showmode In insert mode, displays a message on the prompt line indicating the type of insert you are making. For example, "Open Mode," or "Append Mode." (System V only.)
tabstop (ts) Defines number of spaces that a [TAB] indents during editing session. (Printer still uses system tab of 8.)
term Sets terminal type.
ttytype Sets terminal type.
warn Displays the warning message, "No write since last change."
wrapscan (ws) Searches wrap around either end of file.
wrapmargin (wm) Defines right margin. If greater than zero, automatically inserts carriage returns to break lines.
writeany (wa) Allows saving to any file.
Others Please consult manual pages for others.