Vim Tricks
Work within quotes (or double quotes, or parenthesis , or brackets)
Yank or Delete text within quotes, parenthesis, brackets, etc. Here is some sample text we'll be editing:
One day a (very interesting) individual said "I really enjoy the company of animals."
They are 'great' in the same way cereal is {awesome}.
Yank (aka copy) text in paranthesis (very interesting)
:
Normal Mode:
- Put cursor on the "v" -> (
v
ery interesting) - Use
yi(
oryi)
. - Move your cursor to a new line and hit
p
to paste 'very interesting'
Visual Mode:
- Put cursor on the "v" -> (
v
ery interesting) - Use
vi(y
orvi)y
. - Move your cursor to a new line and hit
p
to paste 'very interesting'
Result:
One day a (very interesting) individual said "I really enjoy the company of animals."
They are 'great' in the same way cereal is {awesome}.
very interesting
Yank (or cut) and Paste Multiple Lines
Visual Mode
- Put your cursor on the top line
- Use
shift+v
to enter visual mode - Press
2j
or pressj
two times to go down two lines - (Or use
v2j
in one swift ninja-move!) - Press
y
to yank orx
to cut - Move your cursor and use
p
to paste after the cursor orP
to paste before the cursor
Result:
(Assuming we yank instead of cut)
One day a (very interesting) individual said "I really enjoy the company of animals."
They are 'great' in the same way cereal is {awesome}.
One day a (very interesting) individual said "I really enjoy the company of animals."
They are 'great' in the same way cereal is {awesome}.
Delete text between double quotes "I really enjoy the company of animals."
Normal Mode
- Put cursor on the "I" -> "
I
really enjoy the company of animals." - Use
di"
Visual Mode
- Put cursor on the "I" -> "
I
really enjoy the company of animals." - Use
vi"d
Result:
One day a (very interesting) individual said ""
They are 'great' in the same way cereal is {awesome}.
Note:
i
will work within our containers.a
will include the container itself. This means that in order to yank(very interesting)
instead of (very interesting
), useya(
instead ofyi(
Delete a sentence or paragraph
Normal Mode
- Put cursor in the beginning of a sentence or paragraph
- Use
dis
for a sentence, ordip
for a paragraph
Visual Mode
- Put cursor in the beginning of a sentence or paragraph
- Use
visd
for a sentence, orvipd
for a paragraph
Result:
They are 'great' in the same way cereal is {awesome}.
(Or, no more text as we deleted the whole paragraph).
Working with multiple files: Tabs
Like your favorite GUI text editor, you can use tabs in Vim. Here's how.
Start vim in your Terminal - let's open some file:
$ vim /var/www/example.com/index.php
You can open files using :e <file path>
. We're gonna go a step beyond that and open a file in a new tab using :tabe
. For instance, open a new file in a tab:
:tabe /var/www/example.com/index.php
Then you can open a new tab:
:tabe /var/www/example.com/admin.php
You'll then have 2 tabs open (or 3, if you stated Vim with a blank buffer). You can switch between tabs using "next" and "previous" commands:
# Go to the previous tab
:tabp
# Go to the next tab
:tabn
There are shortcuts for switching to the next tab - You can use gT
to go the next tab.
Movin' the cursor aboot.
-
w
to skip ahead a word (cursor at beginning of word) -
b
to go back a word (cursor at beginning of word) -
e
to skip ahead a word (cursor at end of word) -
ge
to go back a word (cursor at end of word) -
$
to go to the end of a line -
^
to go to the beginning of a line
Deletin' all over the place.
- If you're at line 10 and need to delete line 20, type
:20d
- If you're at line 10 and need to delete lines 20-25, type
:20,15d
- If you want to move your cursor back to its original place, use
ctrl+o
Some other tricks:
-
dw
to delete a word.d3w
to delete three words. -
cw
to delete word and put cursor in its place ("replace word")
Duplicate Duplicate
-
:t.
Duplicate current line right below cursor -
:t 17
Duplicate current line to below line 17 - Replace
t
withm
to "move" the line instead of "duplicate"