9.4. vi Commands

 < Day Day Up > 

vi supplies a large set of single-key commands when in command mode. vim supplies additional multikey commands.

9.4.1. Movement Commands

Some versions of vi do not recognize extended keyboard keys (e.g., arrow keys, Page Up, Page Down, Home, Insert, and Delete); some do. All, however, recognize the keys in this section. Many users of vi prefer to use these keys, as it helps them keep their fingers on the home row of the keyboard. A number preceding a command repeats the movement. Movement commands are also used after an operator. The operator works on the text that is moved.

9.4.1.1. Character

h, j, k, l

Left, down, up, right (, , , ).

Spacebar

Right.

BACKSPACE

Left.

CTRL-H

Left.


9.4.1.2. Text

w, b

Forward, backward by "word" (letters, numbers, and underscore make up words).

W, B

Forward, backward by "WORD" (only whitespace separates items).

e

End of word.

E

End of WORD.

ge

End of previous word. {vim}

gE

End of previous WORD. {vim}

), (

Beginning of next, current sentence.

}, {

Beginning of next, current paragraph.

]], [[

Beginning of next, current section.

][, [ ]

End of next, current section. {vim}


9.4.1.3. Lines

Long lines in a file may show up on the screen as multiple lines. (They wrap around from one screen line to the next.) While most commands work on the lines as defined in the file, a few commands work on lines as they appear on the screen. The vim option wrap allows you to control how long lines are displayed.

0, $

First, last position of current line.

^, _

First nonblank character of current line.

+, -

First nonblank character of next, previous line.

ENTER

First nonblank character of next line.

num|

Column num of current line.

g0, g$

First, last position of screen line. {vim}

g^

First nonblank character of screen line. {vim}

gm

Middle of screen line. {vim}

gk, gj

Move up, down one screen line. {vim}

H

Top line of screen (Home position).

M

Middle line of screen.

L

Last line of screen.

numH

num lines after top line.

numL

num lines before last line.


9.4.1.4. Screens

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 screen.

z ENTER

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).

CTRL-R

vi: Redraw screen (without scrolling).

 

vim: Redo last undone change.


9.4.1.5. Searches

/pattern

Search forward for pattern. End with ENTER.

/pattern/+num

Go to line num after pattern.

?pattern

Search backward for pattern. End with ENTER.

?pattern?-num

Go to line num before pattern.

:noh

Suspend search highlighting until next search. {vim}.

n

Repeat previous search.

N

Repeat search in opposite direction.

/

Repeat previous search forward.

?

Repeat previous search backward.

*

Search forward for word under cursor. Matches only exact words. {vim}

#

Search backward for word under cursor. Matches only exact words. {vim}

g*

Search backward for word under cursor. Matches the characters of this word when embedded in a longer word. {vim}

g#

Search backward for word under cursor. Matches the characters of this word when embedded in a longer word. {vim}

%

Find match of current parenthesis, brace, or bracket.

fx

Move cursor forward to x on current line.

Fx

Move cursor backward to x on current line.

tx

Move cursor forward to character before x in current line.

Tx

Move cursor backward to character after x in current line.

,

Reverse search direction of last f, F, t, or T.

;

Repeat last f, F, t, or T.


9.4.1.6. Line numbering

CTRL-G

Display current line number.

gg

Move to first line in file. {vim}

numG

Move to line number num.

G

Move to last line in file.

:num

Move to line number num.


9.4.1.7. Marks

mx

Place mark x at current position.

'x

(backquote) Move cursor to mark x.

'x

(apostrophe) Move to start of line containing x.

''

(backquotes) Return to position before most recent jump.

''

(apostrophes) Like preceding, but return to start of line.

'"

(apostrophe quote) Move to position when last editing the file. {vim}

'[, ']

(backquote bracket) Move to beginning/end of previous text operation. {vim}

'[, ']

(apostrophe bracket) Like preceding, but return to start of line where operation occurred. {vim}

'.

(backquote period) Move to last change in file. {vim}

'.

(apostrophe period) Like preceding, but return to start of line. {vim}

'0

Position where you last exited vim. {vim}

:marks

List active marks. {vim}


9.4.2. Insert Commands

a

Append after cursor.

A

Append to end of line.

c

Begin change operation.

C

Change to end of line.

gI

Insert at beginning of line. {vim}

i

Insert before cursor.

I

Insert at beginning of line.

o

Open a line below cursor.

O

Open a line above cursor.

R

Begin overwriting text.

s

Substitute a character.

S

Substitute entire line.

ESC

Terminate insert mode.


The following commands work in insert mode.

BACKSPACE

Delete previous character.

DELETE

Delete current character.

TAB

Insert a tab.

CTRL-A

Repeat last insertion. {vim}

CTRL-D

Shift line left to previous shift width. {vim}

CTRL-E

Insert character found just below cursor. {vim}

CTRL-H

Delete previous character (same as Backspace).

CTRL-I

Insert a tab.

CTRL-K

Begin insertion of multi-keystroke character.

CTRL-N

Insert next completion of the pattern to the left of the cursor. {vim}

CTRL-P

Insert previous completion of the pattern to the left of the cursor. {vim}

CTRL-T

Shift line right to next shift width. {vim}

CTRL-U

Delete current line.

CTRL-V

Insert next character verbatim.

CTRL-W

Delete previous word.

CTRL-Y

Insert character found just above cursor. {vim}

CTRL-[

(ESCAPE) Terminate insert mode.


Some of the control characters listed in the previous table are set by stty. Your terminal settings may differ.

9.4.3. Edit Commands

Recall that c, d, and y are the basic editing operators.

9.4.3.1. Changing and deleting text

The following table is not exhaustive, but illustrates the most common operations.

cw

Change word.

cc

Change line.

c$

Change text from current position to end-of-line.

C

Same as c$.

dd

Delete current line.

numdd

Delete num lines.

d$

Delete text from current position to end-of-line.

D

Same as d$.

dw

Delete a word.

d}

Delete up to next paragraph.

d^

Delete back to beginning of line.

d/pat

Delete up to first occurrence of pattern.

dn

Delete up to next occurrence of pattern.

dfx

Delete up to and including x on current line.

dtx

Delete up to (but not including) x on current line.

dL

Delete up to last line on screen.

dG

Delete to end of file.

gqap

Reformat current paragraph to textwidth. {vim}

g~w

Switch case of word. {vim}

guw

Change word to lowercase. {vim}

gUw

Change word to uppercase. {vim}

p

Insert last deleted or yanked text after cursor.

gp

Same as p, but leave cursor at end of inserted text. {vim}

]p

Same as p, but match current indention. {vim}

[p

Same as P, but match current indention. {vim}

P

Insert last deleted or yanked text before cursor.

gP

Same as P, but leave cursor at end of inserted text. {vim}

rx

Replace character with x.

Rtext

Replace with new text (overwrite), beginning at cursor. ESCAPE ends replace mode.

s

Substitute character.

4s

Substitute four characters.

S

Substitute entire line.

u

Undo last change.

CTRL-R

Redo last change. {vim}

U

Restore current line.

x

Delete current cursor position.

X

Delete back one character.

5X

Delete previous five characters.

.

Repeat last change.

~

Reverse case and move cursor right.

CTRL-A

Increment number under cursor. {vim}

CTRL-X

Decrement number under cursor. {vim}


9.4.3.2. Copying and moving

Register names are the letters a-z. Uppercase names append text to the corresponding register.

Y

Copy current line.

yy

Copy current line.

"xyy

Copy current line to register x.

ye

Copy text to end of word.

yw

Like ye, but include the whitespace after the word.

y$

Copy rest of line.

"xdd

Delete current line into register x.

"xd

Delete into register x.

"xp

Put contents of register x.

y]]

Copy up to next section heading.

J

Join current line to next line.

gJ

Same as J, but without inserting a space. {vim}

:j

Same as J.

:j!

Same as gJ.


9.4.4. Saving and Exiting

Writing a file means overwriting the file with the current text.

ZZ

Quit vi , writing the file only if changes were made.

:x

Same as ZZ.

:wq

Write file and quit.

:w

Write file.

:w file

Save copy to file.

:n,mw file

Write lines n to m to new file.

:n,mw >> file

Append lines n to m to existing file.

:w!

Write file (overriding protection).

:w! file

Overwrite file with current text.

:w %.new

Write current buffer named file as file.new.

:q

Quit vi (fails if changes were made).

:q!

Quit vi (discarding edits).

Q

Quit vi and invoke ex.

:vi

Return to vi after Q command.

%

Replaced with current filename in editing commands.

#

Replaced with alternate filename in editing commands.


9.4.5. Accessing Multiple Files

:e file

Edit another file; current file becomes alternate.

:e!

Return to version of current file at time of last write.

:e + file

Begin editing at end of file.

:e +num file

Open file at line num.

:e #

Open to previous position in alternate file.

:ta tag

Edit file at location tag.

:n

Edit next file in the list of files.

:n!

Force next file.

:n files

Specify new list of files.

:rewind

Edit first file in the list.

CTRL-G

Show current file and line number.

:args

Display list of files to be edited.

:prev

Edit previous file in the list of files.


9.4.6. Window Commands (vim)

The following table lists common commands for controlling windows in vim. See also the split, vsplit, and resize commands in the later section "Alphabetical Summary of ex Commands." For brevity, control characters are marked in the following list by ^.

:new

Open a new window.

:new file

Open file in a new window.

:sp [file]

Split the current window. With file, edit that file in the new window.

:sv [file]

Same as :sp, but make new window read-only.

:sn [file]

Edit next file in file list in new window.

:vsp [file]

Like :sp, but split vertically instead of horizontally.

:clo

Close current window.

:hid

Hide current window, unless it is the only visible window.

:on

Make current window the only visible one.

:res num

Resize window to num lines.

:wa

Write all changed buffers to their files.

:qa

Close all buffers and exit.

^W s

Same as :sp.

^W n

Same as :new.

^W ^

Open new window with alternate (previously edited) file.

^W c

Same as :clo.

^W o

Same as :only.

^W j, ^W k

Move cursor to next/previous window.

^W p

Move cursor to previous window.

^W h, ^W l

Move cursor to window on left/right.

^W t, ^W b

Move cursor to window on top/bottom of screen.

^W K, ^W B

Move current window to top/bottom of screen.

^W H, ^W L

Move current window to far left/right of screen.

^W r, ^W R

Rotate windows down/up.

^W +, ^W -

Increase/decrease current window size.

^W =

Make all windows same height.


9.4.7. Interacting with the System

:r file

Read in contents of file after cursor.

:r !command

Read in output from command after current line.

:numr !command

Like above, but place after line num (0 for top of file).

:!command

Run command, then return.

!motion command

Send the text covered by motion to Unix command; replace with output.

:n,m! command

Send lines n-m to command; replace with output.

num!!command

Send num lines to Unix command; replace with output.

:!!

Repeat last system command.

:sh

Create subshell; return to editor with EOF.

CTRL-Z

Suspend editor, resume with fg.

:so file

Read and execute ex commands from file.


9.4.8. Macros

:ab in out

Use in as abbreviation for out in insert mode.

:unab in

Remove abbreviation for in.

:ab

List abbreviations.

:map string sequence

Map characters string as sequence of commands. Use #1, #2, etc., for the function keys.

:unmap string

Remove map for characters string.

:map

List character strings that are mapped.

:map! string sequence

Map characters string to input mode sequence.

:unmap! string

Remove input mode map (you may need to quote the characters with CTRL-V).

:map!

List character strings that are mapped for input mode.

qx

Record typed characters into register specified by letter x. If letter is uppercase, append to register. {vim}

q

Stop recording. {vim}

@x

Execute the register specified by letter x. Use @@ to repeat the last @ command.


In vi, the following characters are unused in command mode and can be mapped as user-defined commands:


Letters

g K q V v


Control keys

^A ^K ^O ^W ^X ^_ ^\


Symbols

_ * \ = #

The = is used by vi if Lisp mode is set. Different versions of vi may use some of these characters, so test them before using.


vim does not use ^K, ^_, _, or \.

9.4.9. Miscellaneous Commands

<

Shift text described by following motion command left by one shiftwidth. {vim}

>

Shift text described by following motion command right by one shiftwidth. {vim}

<<

Shift line left one shift width (default is eight spaces).

>>

Shift line right one shift width (default is eight spaces).

>}

Shift right to end of paragraph.

<%

Shift left until matching parenthesis, brace, or bracket. (Cursor must be on the matching symbol.)

==

Indent line in C-style, or using program specified in equalprg option. {vim}

g

Start many multiple character commands in vim.

K

Look up word under cursor in manpages (or program defined in keywordprg). {vim}

^O

Return to previous jump. {vim}

q

Record keystrokes. {vim}

^Q

Same as ^V. {vim} (On some terminals, resume data flow.)

^T

Return to the previous location in the tag stack. (Solaris vi and vim)

^]

Perform a tag lookup on the text under the cursor.

^\

Enter ex line-editing mode.

^^

(Caret key with CTRL key pressed) Return to previously edited file.


     < Day Day Up > 


    Unix in a Nutshell
    Unix in a Nutshell, Fourth Edition
    ISBN: 0596100299
    EAN: 2147483647
    Year: 2005
    Pages: 201

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net