vi: wicked cool stuff
The 10 verses of chapter 1 are:
1:0 moving
1:1 marks
1:2 changing
1:3 cut and paste
1:4 buffers
1:5 oops
1:6 finding
1:7 again
1:8 colon revisited
1:9 wisdom
LESSON 1
1:0 The basic movement commands, h, j, k, and
l, will get you anywhere
you want to go. Here are some other commands that will help you
move around quicker:
- w
- move to the beginning of the next word
- e
- move to the end of the next word
- b
- move backwards to the beginning of the previous word
- W,E,B
- same as w,e, and b, but move to next white space
- H,M,L
- move to home, middle, and last line of screen
- }
- move to beginning of next paragraph. { moves backwards
- 0
- move to beginning of line
- $
- move to end of line
- G
- go to end of file
- 1G
- go to top of file
Many of these commands, when preceded with a number, will execute that
command a number of times. 10G goes to the 10th
line of a file, 15w
moves 15 words forward, 5k moves 5 lines up.
1:1 You can stick up to 26 invisible bookmarks in your file to help you
navigate around in it. The m command, followed by any letter,
creates a mark which you can return to by using either the '
(single quote) or ` (back quote) command. Here are some examples
of things you can do with marks.
- mc
- create a mark called c
- 'c
- return to the line you have previously marked c
- `c
- return to the line and column marked c
- ''
- return to last line before a search or goto
- ``
- same as '' but returns to column and line
- d'a
- delete from current cursor position to mark a
- y'a
- yank from current cursor position to mark a
Alas, when you quit or edit a new file, all the marks are lost.
1:2 You can replace existing text with new text in many various ways.
All of these commands leave you in insert mode, except for the
r command, which leaves you in command mode. Commands that
change more than one character change things from the current
character position wherever you specify.
- rX
- replace current character with X
- R
- overlay characters with new text
- s
- replace current character
- S
- replace current line
- cc
- replace current line
- cw
- change word
- cW
- change up to next white space
- c'a
- change to mark a
- c/bozo
- change to the first occurrence of bozo
- c}
- change to next blank line
- c/^$
- change to next blank line (explained in 1:6)
- c1G
- change to top of file
- c<ret>
- change current and next line (<ret> means return!)
- c3k
- change current and three previous lines
- 3cw
- change next three words
- c0
- change to start of line
- c$
- change to end of line
1:3 You can cut and paste using command mode or colon commands.
The command mode is simplest: simply delete some text, position
the cursor at the new location, then put it back (see 0:5 for
help on those commands). The same procedure may be followed for
words (dw, 10dw, 5db, etc.) If you want
to copy the text, leaving
the original part intact, yank it, then put it as above.
- yy
- yanks one line
- Y
- yanks one line
- 5yy
- yanks five lines
- 7yw
- yanks seven words
- y'a
- yank to mark a
- y/chain
- yank to the next occurrence of the text chain
Another way to move or copy text uses marks (see 1:1). If you set
a mark at the top (mark x), bottom (mark y), and
destination (mark z), the command
will move the text between marks x and y after z.
To copy the text without deleting it, use
The ., ^, and $ characters have special
meanings in colon commands that use marks.
- :'a,'bm.
- move text between marks a and b after the current line
- :^,'am'b
- move text between the top of the file and a after b
- :'a,$co^
- copy text between mark a and the end of the file to the top of the file
1:4 You can use up to 26 named buffers (a through z)
to store text that
you have yanked or deleted. They are accessed using the " command.
Marks (which also use the letters from a to z) can be used in
conjunction with text buffers.
- "ayy
- yank the current line into buffer a
- "wd/dime
- delete and copy into buffer w text from
the current line to the next occurrence of the word dime
- "Wyy
- yank the current line and append it to whatever text
is already in buffer w.
- "d10yy
- yank next ten lines into buffer d
- "rd'r
- delete text from the current line up to mark
r and store it in buffer r. Notice that although the mark
and buffer share the same letter, they do not interfere
with each other.
- "aP
- insert contents of buffer a above
current line. A lower-case p inserts buffer a after
the current line.
Text which is stored in a buffer may be put into another file as
long as you stay in the same vi session. For example, to add the
first ten lines of file1 to the end of fileb, edit file1, then
yank ten lines into buffer a (using "a10yy), then edit fileb using
the colon command :efileb, then put the text from buffer a at the
end of the file by typing G"ap.
1:5 The u command undoes the last edit command
you executed. If you have
performed several edits on one line and have not moved the cursor
off of the line, you can restore it using the U command. There are
ten numbered delete buffers which hold the ten most recently deleted
blocks of text that are at least one full line long. Use the command
"1P to insert the most recently deleted block of text above the current
line. Use the . (dot) command (see 1:7) to insert successive blocks
of deleted text.
1:6 The search commands (/ and ?) are
very flexible, allowing you to
search for patterns as well as exact works. These search patterns
(called regular expressions) use special symbols (called metacharacters)
to match different patterns:
- ^blimp
- this expression matches blimp at
the beginning of a line
- blob$
- matches blob at the end of a line
- ^blue$
- a line with nothing but the word blue
on the line
- ^$
- an empty line
- .
- any character
- *
- zero or more occurences of the previous character
or metacharacter
- bab*
- matches baby and babble, but not
bake
- ba.*
- matches ba followed by zero
or more of any character,
such as babble, bake, and ba, but not bye
- [A-M]
- specifies the range of characters A
through M
- [^0-9]
- matches characters that are not numerals
- [aeiou]
- matches vowels
- ^[^ab]c
- lines starting with neither a nor b,
which have c for the second letter
After using a search command (/ or ?),
you can find the next occurrence
of the same pattern using the following commands.
- n
- find next occurrence of the pattern in
the same direction in which the search was proceeding (forwards for /,
backwards for ?)
- N
- same as n command, but in the opposite direction
Other search commands are:
- fe
- move cursor on top of the next e character
- te
- move cursor on top of the character before next
e character
- F.
- move cursor backwards to the previous "."
character. Metacharacters have no effect with the f and t
commands
- TT
- move cursor backwards to just after the previous
T character
- ;
- repeat last f, t, F,
or T command
- ,
- repeat last f, t, F, or
T command in opposite direction
- }
- search to next paragraph (next blank line)
- ]]
- search to next section. Paragraph and
section definitions are based upon the nroff interpretation of what
paragraph and sections are.
1:7 You can repeat your last editing command with the "."
command. Changes,
deletions, and insertions work with the "." command. Searches do not
work with the "." command.
1:8 Here are some other colon commands you can use:
- :n
- edit next file in the argument list
- :n!
- edit next file and abandon any edits
- :args
- list the arguments that were on the command line
- :rew
- re-edit the first file in the argument list
- :!cmd
- execute cmd in a subshell and then return to vi
- :!%
- % expands to current filename, so if you were
editing a shell script, this command would execute it
- :e#
- toggle between the current file and the last file edited
- :e!
- abandon changes on this file and re-edit it
- :g
- global ex commands; to delete all blank
lines, try
g/^$/d Learning the ex editor is left as an excercise
for the reader.
- :%s
- global line editing; to prepend all lines with "> ", try
%s/^/> /. %s is the same a 1,$s.
1:9 Always remember: it doesn't really matter if
you pronounce it "vee-i" or "vi". It works the same. Every time.
Here endeth the lesson
vi evangelical series /
corby@intuit.com
January 1995 (updated April 6, 1998)