Académique Documents
Professionnel Documents
Culture Documents
1" Tips
Essential vi/vim Editor Skills
by
Jacek Artymiak
First Edition
Lublin 2008
!i"1# Tips, 1st ed. by Jacek Artymiak
Published by devGuide.net
www: http://www.devguide.net/books/vitips1
email: jacek@devguide.net
08! 10 9 8 7 6 5 4 3 2 1
ISBN: 978"83"60869"00"0
The author and the publisher disclaim any and all liability for the
use of information and programs contained in this book.
Chapter 1: Essentials## # # # 3
! PANIC!!! ! ! ! ! 5
! Canceling Commands! ! ! ! 5
! Switching Between Command Mode and
! ! Insert Mode! ! ! ! 6
! Escaping from the ex Editor Mode! ! 6
! Unscrambling the Screen!! ! ! 6
Chapter 2: Basic File Operations# # # 7
! Starting vi#1$! ! ! ! ! 9
! Starting vi#1$ and Opening a File for Editing! 10
! Starting vi#1$ and Opening Multiple Files for
! ! Editing! ! ! ! ! 11
! Switching Between Files! ! ! ! 13
! Opening a File after Starting vi#1$! ! ! 14
! Saving the Current File! ! ! ! 15
! Forcing vi#1$ to Save the Current File! ! 16
! Saving the Current File Under a Di%erent Name! 17
! Saving a Part of the Current File! ! ! 18
! Saving a Part of the Current File Under a
! ! Di%erent Name! ! ! ! 19
! Appending the Current File to Another! ! 20
! Appending a Part of the Current File to Another! 21
! Saving the Current file and Quitting vi#1$! ! 22
! Forcing vi#1$ to Save the Current File and Quit! 23
! Table of Contents "
! Entering Text! ! ! ! ! 55
! Inserting Lines! ! ! ! ! 57
! Inserting Files! ! ! ! ! 59
! Table of Contents "
! Running Commands! ! ! ! 91
! Sending vi#1$ to the Background! ! ! 92
! Shell Access! ! ! ! ! 92
Index ## # # # # # 95
List of Tables
Table 1: UNIX filename wildcards.! ! ! 12
Table 2: Motion commands.! ! ! ! 66
Table 3: Regular expressions.! ! ! ! 76
Introduction
No Unix"class system administrator or user will get far without
learning the basics of vi#1$, the widespread visual text"mode editor.
Vi#1$ is a modal editor, which means that you need to tell it to switch
between di%erent modes and commands, even when all you want to
do is change a few characters. This minor inconvenience is o%set by
the enormous flexibility of the editor and its seamless integration
with the rest of the Unix environment.
Bearing those two issues in mind, I have written a book which does
not bombard you with the old terminology, but rather uses concepts
that are familiar to users who are used to working with Microsoft
Windows or Mac OS X and who do not know much about the ‘joys’
of working on dial"up text mode terminals.
I hope this fresh new look at vi#1$ will help you learn it and become
more productive.
Jacek Artymiak
Page 1
Chapter 1
Essentials
! Chapter 1: Essentials "
PANIC!
When things go wrong and you are stuck feeling that vi#1$ has
gotten out of control, it might be a good idea #or the only way
out…$ to abandon all changes you have made to the files and start
again. To do so, use the :q! ‘panic mode.’
How$To:
Canceling Commands
When you want to abandon the command you were typing in
command mode, press Esc twice. See also the Undo/Redo section on
page 87.
Page 5
! Chapter 1: Essentials "
How you enter insert mode depends on the kind of action you want
to perform. #You can learn about the di%erent actions in Chapter 4,
Editing on page 53.$ The most important trick to master is switching
back to command mode. This is actually very simple+just press
Esc.
If you notice that the text you were working with has suddenly
disappeared and all you can see is the : command prompt, type vi
and press Enter/Return. This will get you back to vi#1$.
Page 6
Chapter 2
Basic File
Operations
! Chapter 2: Basic File Operations "
Starting vi!1"
You can start vi#1$ from the command line without telling it which
file it is supposed to open. You can provide that information later
#see page 10$.
Before
After
How$To:
1. Type vi
2. Press Enter/Return.
Page 9
! Chapter 2: Basic File Operations "
Before
After
How$To:
1. Type vi ~/book.txt
2. Press Enter/Return.
Page 10
! Chapter 2: Basic File Operations "
Before
After
How$To:
1. Type vi ~/*.txt
2. Press Enter/Return.
Page 11
! Chapter 2: Basic File Operations "
chapter0[!1234567].txt (filenames
any single character
[!abc] outside the chapter01.txt through
not on the list
chapter07.txt range)
chapter0[!0-9].txt (filenames
any single character
[!a-z] outside the chapter00.txt through
outside range
chapter09.txt range)
Page 12
! Chapter 2: Basic File Operations "
Step 1
Step 3
Step 4
Step 5
How$To:
Page 13
! Chapter 2: Basic File Operations "
After
Before
How$To:
Page 14
! Chapter 2: Basic File Operations "
After
Before
How$To:
Page 15
! Chapter 2: Basic File Operations "
After
Before
How$To:
Page 16
! Chapter 2: Basic File Operations "
After
Before
How$To:
Page 17
! Chapter 2: Basic File Operations "
If you forget !, vi#1$ will complain and refuse to write the specified
block. Rightly so, because you are replacing the whole file with a
smaller chunk and vi#1$ wants to make sure you know what you are
doing.
After
Before
How$To:
Page 18
! Chapter 2: Basic File Operations "
After
Before
How$To:
Page 19
! Chapter 2: Basic File Operations "
After
Before
How$To:
Page 20
! Chapter 2: Basic File Operations "
After
Before
How$To:
Page 21
! Chapter 2: Basic File Operations "
After
Before
How$To:
Page 22
! Chapter 2: Basic File Operations "
After
Before
How$To:
Page 23
! Chapter 2: Basic File Operations "
After
Before
How$To:
Page 24
! Chapter 2: Basic File Operations "
After
Before
How$To:
Page 25
! Chapter 2: Basic File Operations "
Page 26
Chapter 3
Cursor
Movement
! Chapter 3: Cursor Movement "
Step 1
Step 2
Step 3
Step 4
Step 5
Step 6
Move the cursor one line down: j or #
Page 29
! Chapter 3: Cursor Movement "
How$To:
You need to switch vi#1$ to command mode and then type the
number of characters or lines you want the cursor to move, followed
by h, j, k, or l.
How$To:
Page 30
! Chapter 3: Cursor Movement "
Step 1
Step 2
Step 3
Step 4
Step 5
Step 6
Step 7
Page 31
! Chapter 3: Cursor Movement "
Before
After
How$To:
Page 32
! Chapter 3: Cursor Movement "
Step 1
Step 2
Step 3
Page 33
! Chapter 3: Cursor Movement "
How$To:
Alternatively, type :x, e.g. :12 moves the cursor to line 12.
Note that the : precedes the line number.
Move the cursor x lines down: type x+, e.g. to move two
lines down, type 2+
Move the cursor x lines up: type x-, e.g. to move two
lines up, type 2-
Page 34
! Chapter 3: Cursor Movement "
Step 1
Step 2
Step 3
Step 4
Step 5
Step 6
Step 7
Page 35
! Chapter 3: Cursor Movement "
How$To:
The search will start on the line with the cursor and
continue towards the end of the current file.
Page 36
! Chapter 3: Cursor Movement "
Before
After
How$To:
Page 37
! Chapter 3: Cursor Movement "
How$To:
Page 38
! Chapter 3: Cursor Movement "
Step 1
Step 2
Step 3
Step 4
Step 5
Step 6
Step 7
Step 8
Step 9
Step 10
Step 11
Page 39
! Chapter 3: Cursor Movement "
How$To:
Page 40
! Chapter 3: Cursor Movement "
Step 1
Step 2
Step 3
Step 4
Step 5
Page 41
! Chapter 3: Cursor Movement "
Step 1
Step 2
Step 3
Step 4
Step 5
Step 6
Step 7
Page 42
! Chapter 3: Cursor Movement "
How$To:
Page 43
! Chapter 3: Cursor Movement "
How$To:
Page 44
! Chapter 3: Cursor Movement "
Step 1
Step 2
Step 3
Page 45
! Chapter 3: Cursor Movement "
Step 1
Step 2
Step 3
How$To:
Page 46
! Chapter 3: Cursor Movement "
Step 1
Step 3
Step 6
Step 8
Step 10
Page 47
! Chapter 3: Cursor Movement "
How$To:
Page 48
! Chapter 3: Cursor Movement "
Step 1
Step 2
Step 4
Step 5
Step 6
How$To:
Page 49
! Chapter 3: Cursor Movement "
Strings that you are looking for ought to be literal strings, not
regular expressions. If you want to use regular expressions in your
searches, visit page 76.
How$To:
Page 50
! Chapter 3: Cursor Movement "
Step 1
Step 2
Step 3
Page 51
Chapter 4
Editing
! Chapter 4: Editing "
Entering Text
Typing text into the current bu%er is done using insert mode. To
switch to insert mode, press i and type away. Whatever you type
will appear before the cursor. If you want to add text after the
cursor, press a.
How$To:
1. Start vi#1$: vi
2. Type i
3. Type is
4. Press Esc
5. Type a
6. Press Space
7. Type line
8. Press Esc
9. Type I
10.Type This
11. Press Space.
12. Press Esc
13. Type $
14. Press a
15. Press Space
16. Type 1.
17. The end result should be This is line 1.
Page 55
! Chapter 4: Editing "
Step 1
Step 3
Step 4
Step 5
Step 6
Step 7
Step 8
Step 9
Step 10
Step 11
Step 12
Step 13
Step 14
Step 15
Step 16
Page 56
! Chapter 4: Editing "
Inserting Lines
Adding an empty line can be done in the following way: press Esc,
press 0, press i, press Enter/Return. That’s a lot of typing. You can
do it in a much easier way:
Step 1
Step 2
Step 4
Step 5
Page 57
! Chapter 4: Editing "
How$To:
Page 58
! Chapter 4: Editing "
Inserting Files
To insert one file into another, use :r followed by the access path
and filename of the file you want to insert. It will be added to the
current file after the line with the cursor.
Step 1
Step 2
Step 4
Step 6
How$To:
Page 59
! Chapter 4: Editing "
Step 2
Step 3
Step 4
Page 60
! Chapter 4: Editing "
How$To:
Page 61
! Chapter 4: Editing "
Step 1
Step 2
Step 3
How$To:
Page 62
! Chapter 4: Editing "
Changing Text
The way vi#1$ implements text editing functionality may catch you
by surprise with its clear distinction between text entry and editing.
When you enter text, you work in insert mode, which o%ers very
limited editing functionality. You can use the arrow keys to move
back and forth, and Backspace or Del to make small changes to
any text you typed since the last switch to insert mode, but that’s
about all you can do.
Page 63
! Chapter 4: Editing "
Step 1
Step 3
Step 4
Step 5
Step 6
Step 7
Step 8
Step 9
Step 10
Step 11
Step 12
Step 13
Page 64
! Chapter 4: Editing "
How$To:
Page 65
! Chapter 4: Editing "
left h right l
up k down j
end of word
end of word e E
(blank delimited)
line n nG line n :n
previous
next sentence ) (
sentence
previous
next paragraph } {
paragraph
previous
next character x fx Fx
character x
forward to backwards to
before character tx before character Tx
x x
bottom of screen L
Page 66
! Chapter 4: Editing "
Replacing Text
When you are changing text with the ‘c’ commands, the only part of
the text that gets deleted and overwritten is the part that you
described using the motion commands listed in Table 2: Motio&
commands on page 66. That’s a lot of typing that not everyone wants
to do. Sometimes it is more convenient to switch to ‘overtype’
mode and replace characters as you type along.
How$To:
Page 67
! Chapter 4: Editing "
Step 1
Step 2
Step 3
Step 4
Step 5
Step 6
Step 7
Step 8
Step 10
Page 68
! Chapter 4: Editing "
Step 1
Step 2
Step 3
Step 4
When you change something using the s command, the old text
gets placed into the default text storage register. To avoid
overwriting it with the next change or cut you make, you can tell
vi#1$ to place the old text in one of its named registers. To do so,
precede the s command with “x, where x is a one"character register
name. For example, if you want to replace 34 characters with
something else and keep the originals in register t, type: “t34s
Page 69
! Chapter 4: Editing "
How$To:
You can precede S with the number of line you want to replace. For
example, to replace 72 lines, type 72S
Step 1
Step 3
Step 4
Whenever you change something using S, the old text gets placed
into the default text storage register. To avoid overwriting it with
the next change or cut you make, you can tell vi#1$ to place the old
text in one of its many named registers. To do so, precede the S
command with “x, where x is the one"character register name. For
example, if you want to replace two words with something else and
keep the originals in register t, type: “t72S
Page 70
! Chapter 4: Editing "
How$To:
Deleting Text
There is a multitude of commands designed to help you delete
characters, words, lines, and whole blocks of text.
Deleting Characters:
Page 71
! Chapter 4: Editing "
Step 1
Step 2
Step 3
Step 4
Step 5
Step 6
Step 7
Step 8
Step 9
Step 10
Step 11
Step 12
Step 13
Step 14
Step 15
Page 72
! Chapter 4: Editing "
How$To:
Deleting Words:
Deleting Lines:
Page 73
! Chapter 4: Editing "
Page 74
! Chapter 4: Editing "
Delete all lines that start with the given string: use
^string. For example, to delete all lines that start with the
word London, type :g/^London/d
Delete all lines that end with the given string: use
string$. For example, to delete all lines that end with the
word London, type :g/London$/d
Delete all lines that contain words that start with the
given string: use \<string. For example, to delete all
lines that contain words that start with London #e.g.
London, Londoner, Londonderry$, type :g/\<London/d
Delete all lines that contain words that end with the
given string: use string\>. For example, to delete all
lines that contain words that end with ing #e.g. running,
singing, nothing$, type :g/ing\>/d
Page 75
! Chapter 4: Editing "
/London/ matches
literal string literal string
only London
/.ondon/ matches
London, london,
any single character .
rondon, etc., but not
ondon
/.*ondon/ matches
any number of London, london,
*
repetitions LLondon, zzzzzzondon,
and even ondon
/[abc]/ matches
any character from Warsaw, Bombay,
[...]
the set Chicago, but not
London or Los Angeles
/[^abc]/ matches
any character London, Los Angeles,
[^...]
outside the set but not Warsaw,
Bombay, or Chicago
/[^London]/ matches
beginning of line ^ every line that starts
with London
/[London$]/ matches
end of line $ every line that ends
with London
/\<London/ matches
beginning of word \< every line with words
that begin with London
/London\>/ matches
end of word \> every line with words
that end with London
/[a-z]/ matches
any character from
[...-...] London, but not
range
LONDON
/[^a-z]/ matches
any character outside
[^...-...] London and LONDON,
range
but not london
[, ], \, ^, $, ., |, ?, *, +, \[ \] \\ \^ \$ \. \| \? \* \+ /\\/ matches \
(, ) \( \)
Page 76
! Chapter 4: Editing "
! :s/regex/replacement/flags
You can control the behavior of :s with two flags: g #replace all
occurrences of regex on the same line without asking to confirm$
and c #ask for permission for every match$. You can use either or
both flags in the same command.
Page 77
! Chapter 4: Editing "
Page 78
! Chapter 4: Editing "
Page 79
! Chapter 4: Editing "
vi#1$ does not o%er multi"level registers, but with sixty of them,
that’s not too much of a problem.
Copying Text
Copying text in vi#1$ is called yanking. Whatever the terminology the
principle of operation is the same.
Copying Characters:
Page 80
! Chapter 4: Editing "
Copying Words:
Page 81
! Chapter 4: Editing "
Copying Lines:
Page 82
! Chapter 4: Editing "
Copy all lines that start with the given string: use
^string. For example, to copy all lines that start with the
word London, type :g/^London/y
Copy all lines that end with the given string: use
string$. For example, to copy all lines that end with the
word London, type :g/London$/y
Copy all lines that contain words that start with the
given string: use \<string. For example, to copy all lines
that contain words that start with London #e.g. London,
Londoner, Londonderry$, type :g/\<London/y
Copy all lines that contain words that end with the
given string: use string\>. For example, to copy all lines
that contain words that end with ing #e.g. running,
singing, nothing$, type :g/ing\>/y
Page 83
! Chapter 4: Editing "
Using Registers:
Pasting Text
Cut, deleted, changed, or copied text will be stored in the default
register, unless you instruct vi#1$ otherwise. Pasting stored text is
called putting, but the principles of operation are the same.
Using Registers:
When you copy or delete chunks of text, they end up in the default
text storage register. You can change that behavior by putting them
in named registers using the “x notation, where x is a one"character
register name. For example, if you want to delete the two words
following the cursor and keep the originals in register t, type:
“td2w
Page 84
! Chapter 4: Editing "
Joining Lines
To join two or more line together, use the J command:
Join more than two lines: type the number of lines you
wish to join and then type J.
Step 1
Step 2
Step 3
How$To:
Page 85
! Chapter 4: Editing "
Changing Case
To change lowercase #a-z$ to uppercase #A-Z$ or vice versa, use the
~ #tilde$ command.
Step 1
Step 2
Page 86
! Chapter 4: Editing "
How$To:
Repeating Actions
To repeat the last action, type . #dot$. To repeat the last action any
number of times, type the number of repetitions followed by .
#dot$.
Undo / Redo
To undo the last action, switch to the command mode and type u.
There is no multi"step undo/redo history in vi#1$, but you can revert
all changes made on the current line with U. There’s a gotcha, you
can only use U, if the cursor is still on the line for which you want to
undo changes. To redo #revert the e%ects of undo$, type u or U again.
Page 87
Chapter 5
Tricks
! Chapter 5: Tricks "
Running Commands
You can execute an external command or script from vi#1$ without
a%ecting the text you are editing. The output of the command will
temporarily overwrite the contents of the vi#1$ screen, but it will not
a%ect the text itself. To execute a command, use :!command
Step 2
Step 1
How$To:
Page 91
! Chapter 5: Tricks "
Shell Access
If you need to do some work on the system command line but do
not want to leave vi#1$ use the :sh command.
How$To:
Page 92
! Chapter 5: Tricks "
Step 1
Step 2
Step 3
Step 4
Step 5
Step 6
Step 7
Page 93
Index
! Index "
A lines, 82'83
arrow keys, 29'30, 63 words, 81
Page 97
! Index "
first, 34 F
last, 34 file operations, 6
next, 34
previous, 34 filename, 11'12 , 17, 59
start of the line, 33'34 changing, 17
paragraph, wildcard, 11'12
next, 45'46 testing, 11
previous, 45'46
screen, file, 10'11, 13'24, 26, 59
bottom of, 38, 51 access path, 10, 19, 59
middle of, 38, 51 appending to, 20
top of, 38, 51 a part of, to another, 21
sentence, current, 20'22, 24'26, 59
next, 44 forcing to save and quit, 23
previous, 44 editing, 10'11
word, forced saving the current, 16
end of, 43 inserting, 59
next, 43 location, 17
previous, 43 opening, 10, 14
screen, half a opening multiple, 11
down, 38, 40 read"only, 23
up, 38, 40 recovery, 26
search match, saving, 22
next, 50 a part of, 18
previous, 50 under a di%erent name, 19
current, 15
cutting text, see deleting quitting, 22
under a di%erent name, 17
D switching between, 13
deleting, 67, 69'71, 73'75, 80, 84
characters, 71 I
lines, 70'71, 73'75 insert mode, 6, 29, 55, 58, 63, 69'70
words, 71, 73
inserting, 55, 57
display, adjusting, 51 empty lines, 57
above the current line, 57
E below the current line, 57
editing, text, 10'11, 13'14, 17, 22'24, text, 55
53, 55, 63, 67, 91
J
entering, text, 55, 63 joining, lines, 85
ex mode, 6
Page 98
! Index "
L up, 66
lines, 6, 21, 61 word,
blocks of, 21, 61 end of, 66
long, 6 next, 66
previous, 66
line, 9, 30, 36, 70
current, 60 N
empty, 9 numbers, 87
number, 36, 70 decrementing, 87
source, 30 incrementing, 87
target, 30
O
lowercase, 86 out of control, 5
M overtype mode, 67
markers, 9, 48, 60'61,63, 77, 82
~, 9 P
end of region, 63 panic, 5
motion commands, 63, 66'67, 73, 81, pasting, 80, 84
86 text,
character x, after
backward to before, 66 current line, 84
forward to before, 66 cursor, 84
next, 66 before
previous, 66 current line, 84
down, 66 cursor, 84
left, 66
line, process suspension, 92
beginning of, 66
end of, 66 processing text with external
first, 66 commands, 61'62
last, 66
&th, 66 putting, see pasting
paragraph,
next, 66
previous, 66
R
right, 66
screen, ranges, 18'19, 21, 61, 74, 77, 82
bottom of, 66 lines in,
middle of, 66 first, 18'19
top of, 66 last, 18'19
sentence,
next, 66 redo, 87
previous, 66
Page 99
! Index "
Page 100
! Index "
scripts, 91 Y
yank, see yanking
searching, 50, 77 yanking, 80
backward, 50
forward, 50
shell access, 92
T
text editor, 6
tricks, 89
U
undo, 87
history, 87
uppercase, 86
V
vi, 9'11, 22, 24'25, 92
forcing to quit without saving, 25
quitting without saving, 24
quitting, 22, 92
starting, 9'11
W
way out, 5
whitespace, 43
wrapping lines, 62
Page 101