Vi Cheat Sheet

ADVERTISEMENT

vi Cheat Sheet
regex Cheat Sheet continued
Edit mode:
Logic
Legend
Example
Sample Match
|
OR operand
22|33
33
i
insert at cursor position
()
Capturing group
A(nt|pple)
Apple
a
insert after cursor position
\1
Contents of Group 1
r(\w)g\1x
regex
A
append at end of line
\2
Contents of Group 2
(\d\d)\+(\d\d)=\2\+\1 12+65=65+12
o
insert at new line after cursor
(?:
Non-capturing group
A(?:nt|pple)
Apple
O
insert at newline before cursor
Character Legend
Example
Sample Match
[ESC]
leave edit mode
\t
Tab
T\t\w{2}
T
ab
Command mode
\r
Return character
see below
\n
New line character
see below
:w!
write file to disk
\r\n
New line in Windows
AB\r\nCD
AB
:q!
quit
CD
:wq!
write file to disk and quit
Quantifier Legend
Example
Sample Match
ZZ
"
u
undo
+
The + (one or more) is "greedy"
dd
delete current line
\d+
12345
C
clear-edit the current line from cursor
?
Makes quantifiers "lazy" \d+?
1 in 12345
D
delete the current line from cursor
*
The * (zero or more) is "greedy"
cw
change the current word from cursor
A*
AAA
dw
delete the current word from cursor
?
Makes quantifiers "lazy" A*?
empty in AAA
{2,4}
Two to four times, "greedy"
Text replacement
\w{2,4}
abcd
?
Makes quantifiers "lazy" \w{2,4}?
ab in abcd
[NUM][COMMAND]
repeat COMMAND NUM times
Character Legend
Example
Sample Match
5dd
delete five lines
7C
clear the next 7 lines from cursor
[stuff]
One of the characters in the brackets
[AEIOU]
One uppercase vowel
Y
copy current line
-
Range indicator
[a-z]
One lowercase letter
P
paste text after the current line
[stuff]
One of the characters in the brackets
p
paste text prepending the current line
[AB1-5w-z]
One of either:
v <cursor-select> characer-based visual selection
A,B,1,2,3,4,5,w,x,y,z
use "y" to copy selected text
[stuff]
One of the characters in the brackets
V <cursor-select> line-based visual copy
[A-Z]+
GREAT
use "y" to copy selected text
[^x]
One character that is not x
:%s/\s\+$//
delete trailing spaces at the end of each line
[^a-z]{3}
A1!
:%s/^\s\+
delete all leading spaces at the beginning of each line
[\d\D]
One character that is a digit or a non-digit
:%s/\s\+/ /g
replace multiple whitespaces (or tabs) by one whitespace
[\d\D]+
Any characters,
including new line
Anchor
Legend
Example
Sample Match
^
Beginning of line (but means "not" inside [^brackets])
^abc .*
abc (line start)
$
End of line
.*? the end$
this is the end
\A
Beginning of string
\Aabc[\d\D]*
abc (string...
...start)
regex Cheat Sheet
\Z
End of string
[\d\D]*the end\Z
this is...
...the end
Character Legend
Example
Sample Match
\b
Word boundary
Bob.*\bcat\b
Bob ate the cat
\d
One digit
file_\d\d
file_25
\B
Not a word boundary
Bob.*\Bcat\B.*
Bobcats
\w
One "word character": letter, underscore or digit
\w-\w\w\w
A-b_1
Character Legend
Example
Sample Match
\s
One white space character (e.g.: a tab)
[:alpha:] Letters
[8[:alpha:]]+
WellDone88
ab\s\s\sc
ab
c
[:alnum:] Letters and numbers
[[:alnum:]]{10}
ABCDE12345
\D
One character that is not a digit
[:punct:] Punctuation marks
[[:punct:]]+
?!.,:;
\D\D\D
ABC
\W
One character that is not a word character
Modifier Legend
Example
Sample Match
\W\W\W\W\W
*-+=)
(?i)
Case-insensitive
(?i)Monday
monDAY
\S
One character that is not a space
(?s)
The dot (.) matches new line characters
\S\S\S\S
Yoyo
(\r\n) (?s)ABC.*to Z
ABC
to Z
Quantifier Legend
Example
Sample Match
(?m)
Treats the string as multiple lines, so that ^ and $ can match
+
One or more
Version \w-\w+
Version A-b1_1
in several places
(?m)1\r\n^2$\r\n^3$
1
{3}
Exactly three times
\D{3}
ABC
2
{2,4}
Two to four times
\d{2,4}
156
3
{3,}
Three or more times
\w{3,}
regex_tutorial
(?x)
Comment mode (aka whitespace mode)
*
Zero or more times
A*B*C*
AAACC
(?x) # this is a
abc d
?
Once or none
plurals?
plural
# comment
abc # on multiple
Character Legend
Example
Sample Match
# lines
.
Any character except new line
[ ]d # spaces must be
a.c
abc
# in brackets
.
Any character except new line
.*
whatever, man.
Lookaround Legend
Example
Sample Match
\.
A period (special character: needs to be escaped by a \)
01234 in 0123456789
(?=
Positive lookahead
(?=\d{10})\d{5}
a\.c
a.c
(?<=
Positive lookbehind
(?<=\d)cat
cat in 1cat
\
Escapes a special character \.\*\+\? \$\^\/\\
.*+? $^/\
(?!
Negative lookahead
(?!theatre)the\w+
theme
\
Escapes a special character \[\{\(\)\}\]
[{()}]
(?<!
Negative lookbehind
\w{3}(?<!mon)ster
Munster

ADVERTISEMENT

00 votes

Related Articles

Related forms

Related Categories

Parent category: Education
Go