Chapter 1, The vi Text Editor#
-
A Brief Historical Perspective
-
Opening and Closing Files
vi filename
, and editZZ
, save file:w
, save:w new-filename
, saveAs:w!
, force write:e!
, resume:q!
, force quit:!rm junk-file
:!df
-
The only way to learn vi is to practice.
Chapter 2, Simple Editing#
-
Moving the cursor
- Single Movements:
h
,j
,k
,l
, they suck! I have the neo-keyboard layout - Numeric Arguments: 1, 2, ...
- Movement Within a Line:
0
, move tobeginning of line$
, move to end of line^
, move to first non empty char
- Move by Text Block
w,b
, move by word, forword, backword, respectively.W,B
, move by BIG word, forword, backword, respectively. "-" connected words are treated as one8G
, move to 8th lineG
, move to END of filegg
, like1G
- Move to first char of previous/next line:
-
,+
- Single Movements:
-
Simple Edits
a
, append; (not necessary, I will customize these key to other use)i
, insert;c
, change;cw, cb
, change word, forward, backward, respectivelyc2w
, change 2 words, forwardc^
, to first not empty charc0, c$
, to begin, end of line, or you usec[Home], c[End]
d
, delete;dw
, by worddb
, by word, backwarddd
, delete lined$, d[End], D
, delete to end of linedto
,dTx
, delete to(not include) "x", forward / backwardd/th
, delete until first match of "th"diw
, delete current worddi(
, delete within the current parens
s
, substitudes
(one and more char),S
(whole line).
x, X
, delete one char, forward, or backward, respectively (not necessary)xP, XP
, delete and paste backxp, Xp
, swap two chars
y
, yank;p
, put;$p
, paste to end of line0P
,^P
, paste to begin of line
-
General Form of vi Commands
cc, dd, yy
(command)(text object)
(command)(number)(text object)
(number)(command)(text object)
cc
, change whole lineC
, likec$
~
, change case
-
Misc
- Repeat your last command
.
- Undo, Redo
u
,C-r
,U
, seems work differently in evil-mode
- Numeric Arguments for Insert Commands: e.g.
4i<space><Esc>
- Repeat your last command
-
Notes
cc
,dd
,yy
, apply to lineC
,D
,S
apply from current position to end, whileY
, the whole lineC
===c$
,D
===d$
, butY
===yy
, instead ofy$
-
Overall
+------------------------------+------------------------------+------------------------------+------------------------------+ | Text object | Change | Delete | Copy | +------------------------------+------------------------------+------------------------------+------------------------------+ | One word | cw | dw | yw | +------------------------------+------------------------------+------------------------------+------------------------------+ | Two words, not counting | 2cW or c2W | 2dW or d2W | 2yW or y2W | | punctuation | | | | +------------------------------+------------------------------+------------------------------+------------------------------+ | Three words back | 3cb or c3b | 3db or d3b | 3yb or y3b | +------------------------------+------------------------------+------------------------------+------------------------------+ | One line | cc | dd | yy or Y | +------------------------------+------------------------------+------------------------------+------------------------------+ | To end of line | c$ or C | d$ or D | y$ | +------------------------------+------------------------------+------------------------------+------------------------------+ | To beginning of line | c0 | d0 | y0 | +------------------------------+------------------------------+------------------------------+------------------------------+ | Single character | r | x or X | yl or yh | +------------------------------+------------------------------+------------------------------+------------------------------+ | Five characters | 5s | 5x | 5yl | +------------------------------+------------------------------+------------------------------+------------------------------+
-
Example
r
,dd
,P
,cw
,x
With a editor you can scrooll the page, With a screen editor you can scroll the page, move the cursor, delete lines, nisret move the cursor, delete lines, insert characters, and more wrile results of characters, and more while results of your edits as you make tham. your edits as you make them. Since they allow you to make changes Screen editors are very popular. as you read through a file, much as Since they allow you to make changes you would edit a printed copy, as you read through a file, much as screen editors are very popular. you would edit a printed copy.
P
,p
,.
With a screen editor you can With a screen editor you can scroll the page. scroll the page. move the cursor. With a screen editor you can delete lines. move the cursor. With a screen editor you can delete lines.
J
With a With a screen editor you can scroll the page, move the cursor screen editor you can scroll the page, move the cursor
Chapter 3, Moving Around in a Hurry#
-
Movement by Screens
- Scroling the Screen:
C-b
,C-f
,C-u
(not work in emacs evil-mode),C-d
,C-y
,C-e
z<Enter>
,z.
,z-
, move to top/center/bottom and scroll to it (not as good as emacs'recenter-top-center
, i will drop this in my evil-mode)
- Scroling the Screen:
-
Redrawing the Screen:
C-l
(not work in my evil-mode, we haverecenter-top-center
) -
Movement Within a Screen
H
,M
,L
: to top/middle/bottom of screen2H
,2L
: 2 second line from top/bottom
-
Movement by Line
+
,<Enter>
,-
, move to first char of next/previous line.- Movement on current line:
20|
, move to column 20,^
, move to first nonblank char.
-
Movement by Text Blocks
e
,E
, end of word, (ignore punctuations)(
,)
, beginning of current/next setence{
,}
, beginning of current/next paragraph[[
,]]
, beginning of current/next section
-
Movement by Searches
/pattern
,?pattern
, search forward/backwardn
, next matchN
, next match, opposite position:set nowrapscan
, dont do loop searchd/pattern
-
Current Line Searches
fx
,Fx
, move on to previous/next 'x'tx
,Tx
, move before char 'x', previous/next;
,,
, repeat same direction, opposite direction7fx
, move to 7th matchdfx
, e.g.dfi
, delete from here to first occurance of 'i', thend;
, delete to second occurance of 'i'
-
Movement by Line Number
20G
, move to Line #20``
, move back (old position)''
, move back, but at the beginning of line
With a screen editor you can scroll the page, move1 move2 move4 move5 move6 move7 the cursor, dlete lines, insert characters, and more, while seeing the characters, ang the results of your edits as you make them.
Chapter 4, Beyond the Basics#
-
More Command Combinations
+------------+------------+-----------+------------------------------+ |Change |Delete |Copy |From cursor to... | +------------+------------+-----------+------------------------------+ |cH |dH |yH |Top of screen | +------------+------------+-----------+------------------------------+ |cL |dL |yL |Bottom of screen | +------------+------------+-----------+------------------------------+ |c+ |d+ |y+ |Next line | +------------+------------+-----------+------------------------------+ |c5| |d5| |y5| |Column 5 of current line | +------------+------------+-----------+------------------------------+ |2c) |2d) |2y) |Second sentence following | +------------+------------+-----------+------------------------------+ |c{ |d{ |y{ |Previous paragraph | +------------+------------+-----------+------------------------------+ |c/pattern |d/pattern |y/pattern |Pattern | +------------+------------+-----------+------------------------------+ |cn |dn |yn |Next pattern | +------------+------------+-----------+------------------------------+ |cG |dG |yG |End of file | +------------+------------+-----------+------------------------------+ |c13G |d13G |y13G |Line number 13 | +------------+------------+-----------+------------------------------+
-
Options When Starting vi
vi +3 filename
, open file at 3rd linevi + filename
, open file at last linevi +/pattern filename
, open file at first occurance of patternvi +R filename
, read file in read-only modevi -r filename
,ex -r filename
, Recovering a Buffer
-
Yank to Named Buffer
- `"dyy", yank to buffer "d"
- `"ayw", yank to buffer "a"
"ap
,"
dp`, put buffer "a"/"d"
-
Marking Your Place
mx
, mark current position to x'x
,''
,``
, move to last position
Chapter 5, Introducing the ex Editor#
__ _ _ _ _
/ _| ___ _ __ | | __ _| |_ ___ _ __ _ __ ___ __ _ __| (_)_ __ __ _
| |_ / _ \| '__| | |/ _` | __/ _ \ '__| | '__/ _ \/ _` |/ _` | | '_ \ / _` |
| _| (_) | | | | (_| | || __/ | | | | __/ (_| | (_| | | | | | (_| |
|_| \___/|_| |_|\__,_|\__\___|_| |_| \___|\__,_|\__,_|_|_| |_|\__, |
|___/
Chapter 6, Global Replacement#
-
Substitude
:s/old/new
, substitude first occurance of old:s/old/new/g
, many occurance:50,100s/old/new/g
,:50,100s/old/new
apply to 50~100 lines:1,$s/old/new
, apply to whole file
-
c, confirm
y
for yesn
for noEnter
, quit
-
Content-Sensitive Replacement
:%s/old/new/g
, no confirm, whole file:g/pattern/s/old/new/g
,:1,200g/pattern/s/old/new/g
:g/pattern/s/old/new/g
, same as:g/pattern/s//new/g
-
Pattern-Matching Rules
.
, single char, except a newline*
, match zero or more occurance of previous char- e.g.
a*
can matcha
,aa
,aaa
, etc - e.g.
.*
can mathc any char(s) combination
- e.g.
^, $
, when it is the first/last char of regex, treated as line beginning/ending, else treated as itself.- e.g.
^Line
match any line starts with "Line" - e.g.
something^*
, can match "something", "something^", "something^^", etc
- e.g.
[]
, e.g.[aoei]
matcha
,o
,e
,i
; and[a-z]
,;:[a-z0-9]
works as expected.:%s/\(That\) or \(this\)/\2 or \1/
~
, match last seach history. Like you seached "the" with/the
, then you can search for "then" with/~n
\
, e.g.\.
match "."\<m
,\>tion
, match words begin with 'm', end with "tion"
-
POSIX Bracket Expressions
[:alnum:]
Alphanumeric characters[:alpha:]
Alphabetic characters[:blank:]
Space and tab characters[:cntrl:]
Control characters[:digit:]
Numeric characters[:graph:]
Printable and visible (nonspace) characters[:lower:]
Lowercase characters[:print:]
Printable characters (includes whitespace)[:punct:]
Punctuation characters[:space:]
Whitespace characters[:upper:]
Uppercase characters[:xdigit:]
Hexadecimal digits
-
New Pattern of
s/old/NEW/g
\n
, n is 1..9, match (THIS)&
, mathed part~
,s/thier/their/
, thens/her/~/
is likes/her/their
\u, \l
, uppercase/lowercase char.:s/\(That\) or \(this\)/\u\2 or \l\1/
\U, \L, \e, \E
, uppercase region, or whole match:set ic
, ignore case
-
More Substitution Tricks
:s//~/
, repeat last substitution:%&g
, Repeat the last substitution everywhere:%s;/user1/tim;/home/tim;g
-
Exercise
/four
,<Enter>
,cwfive
,n
(repeat search),n
(bypass one match),.
(apply lastcwfive
)
-
Examples
- example-1
<p>something</p> <p>somethingelse</p> <a>something</a> ===> `:%300,400g/<p>/s/something/somethingelse/g` ===> <a>something</a> <a>something</a> <a>something</a> <p>something</p> <p>somethingelse</p>
- example-2
one (one) two (two) three ===> `:348,352s/.*/(&)/` ===> (three) four (four) five (five)
- example-3
That or this ===> `:%s/\(That\) or \(this\)/\2 or \1/` ===> this or That
__ _ _ _ _
/ _| ___ _ __ | | __ _| |_ ___ _ __ _ __ ___ __ _ __| (_)_ __ __ _
| |_ / _ \| '__| | |/ _` | __/ _ \ '__| | '__/ _ \/ _` |/ _` | | '_ \ / _` |
| _| (_) | | | | (_| | || __/ | | | | __/ (_| | (_| | | | | | (_| |
|_| \___/|_| |_|\__,_|\__\___|_| |_| \___|\__,_|\__,_|_|_| |_|\__, |
|___/
Chapter 7, Advanced Editing#
-
Customizing vi
- The Command
:set option
, or:set nooption
- The .exrc File
- The Command
-
Executing Unix Commands
:!cmd
:r !date
, output to file, Thu May 1 12:59:55 CST 2014:. !date
, same as:r
, but overwrite current line:r Learning-Vim.md
, this is .... Good.
-
Filtering Text Through a Command
:%!sort
, you try you die...!)cmd
, pass these sentence to cmd!!
, current line!2!
,2!!
, two lines
-
Saving Commands
- Word Abbreviation
:ab abbr phrase
:unab abbr
- Word Abbreviation
Chapter 8, Introduction to the vi Clones#
Tricks#
diw
,di"
,di{}
:!xxd
, hex editor
:!xxd -r
de
, then.
as you wantddp
, swap current and next linebye
,yiw
'.
, last edited line`.
, last edited char:changes
, list changes- /rgba/y A
10.gv
, reselects last visual selection g~~
,gUU
,guu
>>
,<<
, indent#
,*
, search word under cursor