Tutorial
Vim (vi Improved)
Beyond the Basics
Vim
More Navigation
- Within current line
0, ^, $ - move to beginning of line , first nonblank character, end of line
w , b - move forward / backward one word
fchar / Fchar - move forward / backward to first occurrence of char (cursor lands on char)
- e.g.,
f; moves the cursor onto the next semicolon on the line
tchar / Tchar - move forward / backward until first occurrence of char (cursor lands on character before char)
- e.g.,
fs moves the cursor onto the characetr immediately before the next 's' on the line
- Larger movements
nG (or :n) - move to line n
G (or :$) - move to last line in file
Ctrl-F, Ctrl-B - jump forward / backward one screen
H, M L - move to top (High), Middle, bottom (Low) of screen
Basic Actions
d - delete
y - yank (copy)
Count
- specifying an integer in front of a navigation or editing command in normal mode performs the operation that number of times
3j - move down 3 lines
2fm - move cursor onto the second m after the current position on the line (move cursor forward 2 'm's)
Basic Searching
The f, F, t, and T commands operate only on the current line. To search beyond
the current line, or for patterns larger than a single character:
/string<Enter>- find first forward occurrence of string
?string<Enter> - find first backward occurrence of string
/<Enter> (or n) - find next occurrence of most recent search
?<Enter> (or n) - find next occurrence of most recent search
- You can use the up/down arrow keys after
/ or ? to move through your recent searcj strings
The Basic Structure of a Normal Mode Operation
Count Action Subject
- Omitting Count - operation is performed once
- Omitting Action - movement only
- A few Actions take no Subject (
x for example)
- Otherwise, Action is performed on the Subject Count times
- Examples:
w - move to next word; no Action, Count is 1
10j - move down 10 lines; no Action, Count is 10
dt) - delete all characters until (but not including) the next )
- doubling the letter of an action (e.g.,
dd, yy usually makes it operate on the entire line).
dd deletes the current line
yy yanks (copies) the current line
Some Command Mode Actions
- Again,
: gets you into command mode
- Many of the commands in command mode work on multiple lines; you indicate which lines by specifying a line range
upon which the command should operate.
- There are command to cut/copy/paste and substitute (replace)
- There are several commands to cut/copy/paste
Line Ranges
. - current line (line cursor is on)
$ - last line of the file
.+7 - seven lines from the current line
$-1 - next to last line in file
Visual Modes
- Allows you to see range being worked on
- Three modes:
v — character mode; selects characters
Shift-V — line mode; selects full lines
Ctl-V — block mode; selects blocks (columns) of characters
The General Form of These Commands
- <line-range> <action> other arguments
Actions
- cutting/deleting -
d
:10,15d - deletes lines 10-15
:1,$d - deletes from first to last line
- copying -
y
:13,23y - yank (make a copy of) lines 13 through 23
:.,.+3y - yank 4 lines beginning at the current line
- moving (cut/paste) -
m
:13,23m27 - move lines 13 through 23 to line 27
:.,.+3m$ - move the 4 lines beginning at the cursor to the end of the file
- transferring (copy/paste) -
t
:13,23t27 - copy lines 13 through 23 to line 27
:.,.+3mt - copy the 4 lines beginning at the cursor to the end of the file
- Note the difference between
y which simply holds onto a copy of the specified range
and t which makes a copy and places it at the destination line. Similarly d merely deletes the specified range
(holding on to it so it can be p'd later), while m places it at the destination line.
- In summary:
d is a simple cut
y is a simple copy
p is a simple paste
t is a copy/paste in a single command
m is a cut/paste in a single command