whttps://vimsheet.com/
Copy (yank), cut and paste
<N>
y
{motion}
: where {motion} is a Vim motion. For example, y
w
copies to the beginning of the next word. Other helpful yanking commands include:
y
y
orY
– yank the current line, including the newline character at the end of the liney
$
– yank to the end of the current line (but don’t yank the newline character); note that many people like to remapY
toy
$
in line with C and Dy
i
w
– yank the current word (excluding surrounding whitespace)y
a
w
– yank the current word (including leading or trailing whitespace)y
t
x
– yank from the current cursor position up to and before the character (til x)y
f
x
– yank from the current cursor position up to and including the character (find x)
Cutting can be done using d
{motion}
, including:
-
d
d
- cut the current line, including the newline character at the end of the line -
yank into clipboard:
"
+
y
Surround Text
<V>
c
[textbefore]
<
c-r
>
"
[textafter]
How do I add text before and after the visual selection?
<N>
c
i
w
[text]
EscP
: surround the word currently under the cursor with [text]
You can surround the word currently under the cursor in quotes with the following normal mode commands:
c
i
w
"
"
EscP
-
Replace
iw
with any other Vim motion/text object to surround other things with quotes*. Or""
with any other pair of characters to surround the object with different things. -
Or use
c
from visual mode to surround text that is hard to describe with a single motion. -
If you want to surround the object with a longer piece of text, such as an HTML
<p>
tag, you can use Ctrl-R instead of theP
put command:
c
i
w
<
p
>
c-R
"
<
/
p
>
Esc
See :help i_CTRL-R
for more details.
Objects smaller than a line can also be surrounded using the small delete register, and as of Vim 8.2.2189, this is repeatable via the dot command, making it easy to apply the edit quickly in several different places.
So e.g. to go from this:
one two three four
to this:
(one two) (three four)
type:
c
2
w
(
c-R
-
)
Esc
w
.
Thanks to Christian Brabandt for letting us know about the fix, and for implementing it!
However, as dicussed in the comments, if you attempt to use Ctrl-R to surround an object larger than a single line (or prior to v8.2.2189), when you repeat this with the dot command, it will enter the text from the original change command. As @user938271 explains, you can workaround this by using <c-R><c-O> or <c-R><c-P> instead of a plain <c-R> when inserting the contents of the register.
So to go from:
<li>one
<li>two
<li>three
<li>four
to:
<ul>
<li>one
<li>two
</ul>
<ul>
<li>three
<li>four
</ul>
You can type:
c
2
c
<
u
l
>
Return
c-R
c-O
"
<
/
u
l
>
Esc
j
j
.
Find and Replace
<N>
How to insert text at beginning of a multi-line selection
How to Search in Vim / Vi Find and Replace in Vim / Vi
Slash and Dot
/
+[keyword]
+Enter
: to find any keyword and jump to the first occurrence;cgn
: find the last thing we searched for, delete it and push into insert mode. Press <esc> key after finishing editing and return to normal mode;N
: jump to the next occurrence;- repeat with
.
for auto-replace
Substitute Command
Basic
The general form of substitute command:
:[range]s/{pattern}/{string}/[flags] [count]
-
The command searches each line in
[range]
for a{pattern}
, and replaces it with a{string}
.[count]
is a positive integer that multiplies the command. -
If no
[range]
and[count]
are given, only the pattern found in the current line is replaced. The current line is the line where the cursor is placed. -
For example, to search for the first occurrence of the string ‘foo’ in the current line and replace it with ‘bar’, you would use:
:s/foo/bar/
- To replace all occurrences of the search pattern in the current line, add the g flag:
:s/foo/bar/g
- If you want to search and replace the pattern in the entire file, use the percentage character % as a range. This character indicates a range from the first to the last line of the file:
:%s/foo/bar/g
- If the {string} part is omitted, it is considered as an empty string, and the matched pattern is deleted. The following command deletes all instances of the string ‘foo’ in the current line:
:s/foo//g
To confirm each substitution, use the c flag:
:s/foo/bar/gc
replace with bar (y/n/a/q/l/^E/^Y)?
Press y
to replace the match or l
to replace the match and quit. Press n to skip the match and q or Esc to quit substitution. The a option substitutes the match and all remaining occurrences of the match. To scroll the screen down, use CTRL+Y, and to scroll up, use CTRL+E.
You can also use regular expressions as a search pattern. The command bellow replaces all lines starting with ‘foo’ with ‘Vim is the best’:
:%s/^foo.\*/Vim is the best/gc
The ^ (caret) symbol matches the beginning of a line and .* matches any number of any characters.
Shifting blocks visually
<N>
>>
: indent current line<<
: unindent current line- +
.
: repeat operation
<V>
>
: indent current line<
: unindent current line- +
.
: repeat operation
<I>
- <c-T> indent current line
- <c-D> unindent current line