Learning the vi Editor

Learning the vi EditorSearch this book
Previous: III. AppendixesAppendix ANext: B. ex Commands
 

A. Quick Reference

This appendix lists vi commands and ex commands according to their use.


Table A.1: Movement Commands
CommandFunction
Character
h,j,k,lLeft, down, up, right (<-, -v, -^, ->).
Text
w,W,b,BForward, backward by word.
e,EEnd of word.
),(Beginning of next, previous sentence.
},{Beginning of next, previous paragraph.
]],[[Beginning of next, previous section.
Lines
[RETURN]First non-blank character of next line.
0, $First, last position of current line.
^First non-blank character of current line.
+, -First non-blank character of next, previous line.
n|Column n of current line.
HTop line of screen.
MMiddle line of screen.
LLast line of screen.
nHn (number) of lines after top line.
nLn (number) of lines before last line.
Scrolling
[CTRL-F], [CTRL-B]Scroll forward, backward one screen.
[CTRL-D], [CTRL-U]Scroll down, up one-half screen.
[CTRL-E], [CTRL-Y]Show one more line at bottom, top of window.
z [RETURN]Reposition line with cursor: to top of screen.
z.Reposition line with cursor: to middle of screen.
z-Reposition line with cursor: to bottom of screen.
[CTRL-L]Redraw screen (without scrolling).
Searches
/patternSearch forward for pattern.
?patternSearch backward for pattern.
n, NRepeat last search in same, opposite direction.
/, ?Repeat previous search forward, backward.
fxSearch forward for character x in current line.
FxSearch backward for character x in current line.
txSearch forward to character before x in current line.
TxSearch backward to character after x in current line.
;Repeat previous current-line search.
,Repeat previous current-line search in opposite direction.
Line number
[CTRL-G]Display current line number.
nGMove to line number n.
GMove to last line in file.
:nMove to line n in file.
Marking position
mxMark current position as x.
``xMove cursor to mark x.
``Return to previous mark or context.
'xMove to beginning of line containing mark x.
''Return to beginning of line containing previous mark.

Table A.2: Editing Commands
CommandFunction
Insert
i, aInsert text before, after cursor.
I, AInsert text before beginning, after end of line.
o, OOpen new line for text below, above cursor.
Change
rReplace character.
cwChange word.
ccChange current line.
cmotion

Change text between the cursor and the target of motion.

CChange to end of line.
RType over (overwrite) characters.
sSubstitute: delete character and insert new text.
SSubstitute: delete current line and insert new text.
Delete, move
xDelete character under cursor.
XDelete character before cursor.
dwDelete word.
ddDelete current line.
dmotion

Delete text between the cursor and the target of motion.

DDelete to end of line.
p, PPut deleted text after, before cursor.
"np

Put text from delete buffer number n after cursor (for last nine deletions).

Yank
ywYank (copy) word.
yyYank current line.
"ayy

Yank current line into named buffer a (a-z). Uppercase names append text.

ymotion

Yank text between the cursor and the target of motion.

p, PPut yanked text after, before cursor.
"aPPut text from buffer a before cursor (a-z).
Other commands
.Repeat last edit command.
u, UUndo last edit; restore current line.
JJoin two lines.
ex edit commands
:dDelete lines.
:mMove lines.
:co or :tCopy lines.
:.,$dDelete from current line to end of file.
:30,60m0Move lines 30 through 60 to top of file.
:.,/pattern/co$

Copy from current line through line containing pattern to end of file.


Table A.3: Exit Commands
CommandFunction
ZZWrite (save) the file if modified, and quit file.
:xWrite (save) the file if modified, and quit file.
:wqWrite (save) the file unconditionally, and quit file.
:wWrite (save) file.
:w!Write (save) file, overriding protection.
:30,60w newfileWrite from line 30 through line 60 as newfile.
:30,60w>> file

Write from line 30 through line 60 and append to file.

:w %.newWrite current buffer named file as file.new.
:qQuit file.
:q!Quit file, overriding protection.
QQuit vi and invoke ex.
:e file2Edit file2 without leaving vi.
:r newfile

Read contents of newfile into current file.

:nEdit next file.
:e!Return to version of current file at time of last write (save).
:e #Edit alternate file.
:viInvoke vi editor from ex.
:Invoke one ex command from vi editor.
%Current filename (substitutes into ex command line).
#Alternate filename (substitutes into ex command line).

Table A.4: Solaris vi Command Mode Tag Commands
CommandFunction
^]

Look up the location of the identifier under the cursor in the tags file, and move to that location. If tag stacking is enabled, the current location is automatically pushed onto the tag stack.

^T

Return to the previous location in the tag stack, i.e., pop off one element.


Table A.5: Command-Line Options
CommandFunction
vi file

Invoke vi editor on file.

vi file1 file2Invoke vi editor on files sequentially.
view file

Invoke vi editor on file in read-only mode.

vi -R fileInvoke vi editor on file in read-only mode.
vi -r file

Recover file and recent edits after a crash.

vi -t tag

Look up tag and start editing at its definition.

vi -w n

Set the window size to n; useful over a slow connection.

vi + fileOpen file at last line.
vi +n file

Open file directly at line number n.

vi -c command file

Open file, execute command, which is usually a search command or line number (POSIX).

vi +/pattern file

Open file directly at pattern.

ex fileInvoke ex editor on file.
ex - file < script

Invoke ex editor on file, taking commands from script; suppress informative messages and prompts.

ex -s file < script

Invoke ex editor on file, taking commands from script; suppress informative messages and prompts (POSIX).


Table A.6: Other ex Commands
CommandFunction
Abbreviations[1]
:map x sequence

Define keystroke x as a command sequence. x can be multiple characters.

:map! x sequence

Define x as command sequence for insert mode.

:unmap xDisable the map x.
:unmap! xDisable the insert mode map x.
:ab abbr phrase

Abbreviate phrase as abbr; when abbr is typed in insert mode, it expands to full words or phrases.

:unab abbrDisable abbreviation abbr.
Customizing environment:[1]
:set optionActivate option.
:set option=value

Assign value to option.

:set nooptionDeactivate option.
:setDisplay options set by user.
:set all

Display list of all current option settings, both default and those set by the user.

:set option?Display value of option.
Accessing UNIX
:shInvoke shell.
^DReturn to editor from shell.
:! commandGive UNIX command.
:n,m! command

Filter lines n to m through UNIX command.

:r !command

Read output of UNIX command into current file.

[1] In .exrc files, omit the colon at the start of ex commands.


Previous: III. AppendixesLearning the vi EditorNext: B. ex Commands
III. AppendixesBook IndexB. ex Commands

The UNIX CD Bookshelf NavigationThe UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System