Archive-name: editor-faq/vi/part2
Posting-Frequency: every fifteen days
Last-modified: 2007-06-06
Version: 1.93

5.0 - What's online at the vi archives?

There is a FAQ posted frequently which gives a list of all the files
that are online at the vi archive. It also gives a list of addresses
which mirror the vi archive. The main address is ftp://alf.uib.no/pub/vi, but it is
archived at many, many sites. The vi-archive faq lists all of them.
Please check that faq before ftping to it, as there is almost definitely
a closer and therefore faster site to you than the one in Norway.
A few mirrors are: .

The site has many, many files on vi, including a few clones. It also
has the UCB distribution of vi, and lots of useful macros. Check it
out.

6.0 - Silly vi tricks, and silly macros

This section is for silly vi tricks, and silly macros. Actually, any
interesting vi tricks and macros are acceptable, as long as they are
not too lengthy. I will add any that are suggested to me that I think
are reasonable.

6.1 - Silly vi tricks
Note: Also check out the Silly macros down below. Many macros and
tricks are interchangeable.

xp This will delete the character under the cursor, and put it
afterwards. In other words, it swaps the location of two characters.

ddp Similar to xp, but swapping lines.

yyp duplicate a line

uu Undo and redo last change. (This will take you to the last
modification to the file without changing anything.) You can also use
this to compare the changes to a line. Make the changes to the line,
press U to undo the changes to the current line, and then press u to
toggle between the two versions.

:g/.*/m0
This will reverse the order of the lines in the current file.
m0 is the ex command to move the line to line 0.

:v/./d or :g/^$/d
Removes all blank lines.

:g/^[ ]*$/d
Removes all lines that only have whitespace.

:v/./$s/$/./|'';/./-1j|$d
Replaces multiple blank lines with just one blank line.

6.2 - Silly macros
Note: means hold down control, and hit x.

Swap character and one vertically above:
map * kxjphxkP

Fold a line that's too long
map g $80|F r

Change case on most words
map v ywmnoP:s/./\~/g0"nDdd`n@n

Put `and' around the current word
map *` i`ea'

Put 'and' around the current word
map *' i'ea'

Put "and" around the current word
map *" i"ea"

Put `and' around the current word
map! `` bi`ea'

Split a line at the cursor, and put a > at the beginning of the next
line. (For quoting Usenet, etc). I had some trouble with my old
version of this under some versions, so I've redone it, and I think
that it should work.
map g may0OP`aoP:s/./ /g0i>$mb`ay$`bP'add

Insert one character
map g i$r

Format a paragraph without the fmt program. (To use, use J a few
times, then this a few times)
map K 072lBhr

Or, a little less elegantly, but without the need to use J and K
multiple times.
map K {wma}b:'a,.j:s/\(................................................ .........\)/\1/g

Make ctrl-x work as cut, ctrl-v as paste, ctrl-p as copy. You should
mark the beginning of the area as m (use mm). (ctrl-c cannot be
remapped when it is defined as the interrupt character in Unix.)
map "zd`m
map "zy`m
map "zP`m

Save a read-only file. Careful, it changes the permissions. When
done, it changes them to read/write owner, read for everyone else. Note
that this might not be the same as the original permissions.
map K :!chmod 666 %:w!
:!chmod 644 %

vi status line (sort of...) [Note: It's slow...]
map k
map j
map k k
map j j
map
map
map H H
map M M
map L L
map G G
map
map

center a line
map = 080i $78hd0^D:s/ / /g$p

redefine tab to insert 5 spaces instead of a tab marker
map!

switch current and last line (repeat to reverse a file)
map v Gdd''Pj

yank until end of line, run it in a shell and read in the result
map v y$opI:r!"add@a

allow vi to backspace over text that was inserted in a previous
insert mode
map! $Xs
map! $Xs

print the document to the default printer (for BSD replace lp with lpr).
map v 1G!Glpu

set # to toggle line numbers on and off:
map \o# o:se nu:se nonu-:map \o# "wp
map \d# "w2dd
map \x# "xdd@x"xpk
map # ma3L\o#\x#\d#`a:

Quote the current paragraph with '>'s.
map Z> mc{jma}kmb:'a,'bs/^ /> /'c

Allow to suspend processes within insert mode
map! :stop
Make all tabs spaces
map!

Replace all tabs with the proper amount of spaces for the given
tabstop. Removing the last two character (4) will allow you to
specify the tabstop: #t4
map #t 1G!Gpr -t -e4
map #T !}pr -t -e4}

Insert a ruler above the current line
map #r Oi....+....1....+....2....+....3....+....4....+.... 5....+....6....+....7....+....8

This is a macro that redefines itself: (more for heuristic reasons
than anything else) It inserts foo the first time it is run, and bar
all subsequent times.
map K ifoo:map K ibar

This makes end notes. It will insert a [#] at the current location
and put a [#] at the bottom of the document and let you type there.
Hitting `a will take you back to the original location once you are
done with your end note. [#] will be the number of the end note, not
the literal # character. (Pretty crazy, huh?)
map K ma1GO[0]G$?\[[0-9]*\]lyt]`aaOpI:r!echo '1 A + p' | dc0"yD@y0i[A]kkJJJmb1Gdd`bF[yf]GopA

7.0 - Alphabetical vi quick reference

... means that something needs to be specified before or after the
command, as appropriate. This is normally a cursor movement
key (h,j,k,l,w,b, etc.) or a line number.

# (where # is a number) following command n times...
: go to ex-mode
) next sentence
( previous sentence
} next paragraph
{ previous paragraph
]] next section
[[ previous section
0 beginning of line
$ end of line
^ first non-whitespace character
+ first character of next line
- first character of previous line
(spacebar) next character
(return) next line
/ search forward
? search backward
% find match of current parenthesis, brace, or bracket
, reverse direction of last f, F, t, or T
; repeat last f, F, t, or T
. repeat last command
` goto mark
' goto beginning of line with mark
`` return to previous mark or location before a search
'' go to start of line of previous mark or location before search
~ switch case of current character
" store in register
@ execute command in register
! send next to command, replace output (eg !}fmt passes the current
paragraph to the command fmt, and replaces the output with
whatever fmt returns.)
!! send line to command
>> shift line one shiftwidth to the right

<< shift line one shiftwidth to the left
>% shift until matching (, [, or { to the right

<% shift until matching (, [, or { to the left
a append after the current location
A append at the end of the line
^a unused
b beginning of previous word
B beginning of previous word, ignore punctuation
^b scroll back one screen
c change until...
C change to end of line
^c ends insert mode, unused in command mode (if defined as interrupt)
d delete until...
D delete to end of line
^d scroll down half a window, moves to previous shiftwidth in insert
mode
e end of word
E end of word, ignore punctuation
^e scroll screen down one line
f find...
F find backward...
^f scroll forward one screen
g unused
G ...Goto [defaults to end of file]
^g show status line
h left
H first line on screen
^h backspace in insert mode, left in command mode
i insert before current location
I insert before first non-whitespace character on line
^i tab in insert, unused in command
j down
J join next line with current line
^j down in command, create newline in insert
k up
K unused
^k unused
l right
L last line on screen
^l redraw screen
m mark position into register
M middle of screen
^m carriage return
n repeat last search
N repeat last search, reverse direction
^n down in command
o open line below current
O open line above current
^o unused
p put below current line
P put above current line
^p up in command
q unused
Q quit and run ex
^q unused
r replace current character
R replace characters until insert mode is left
^r redraw screen in command mode
s substitute
S substitute entire line
^s unused
t to...
T backward to...
^t moves to next shiftwidth.
u undo last change
U undo changes to current line
^u scroll up half a window
v unused
V unused
^v unused in command, quotes next character in insert
w beginning of next word
W beginning of next word, ignore punctuation
^w unused in command, in insert move back to beginning of previous
word
x delete current character
X delete previous character
^x unused
y yank...
Y yank current line
^y scroll screen up one line
z reposition screen around line (Return to top of screen, . to
middle, - to bottom)
ZZ write (only if changes have been made) and quit
^z unused

7.1 - Command mode input options ( : commands)

(Note: this is not a canonical list, just some of the more important
ones.)

:r read into current text
:r ! read output from command into current text
:nr read in at line number
:! run command, return
:sh goto shell
:so read and execute commands from
:x write (only if changes have been made) and quit
:wq write and quit
:l1,l2w write between lines l1 and l2 to . If
is not specified, assume current. If l1,l2 not
specified, assume entire file (making it :w)
:w >> append to . May use line numbers
:w! overwrite current file
:q quit
:q! quit, forget changes
:e edit without leaving vi
:e! forget changes since last write
:n edit next file
:e +n edit at line n, if no end, assume end of file
:n specify as new list of files to edit
:e# edit alternate file (if :e is used, alternate is
the original file)
:args show files to be edited
:rew rewind list of files to top
:map m n create a macro (make m do n)
:map! m n create an insert mode macro (make m do n)
:unmap m destroy macro m
:unmap! m destroy insert mode macro m
:ab <1> <2> abbreviate - replace <1> with <2> whenever typed as
a word
:unab <1> unabbreviate <1>
:cd cd to
:set