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" -> (
very interesting) - Use
yi(oryi). - Move your cursor to a new line and hit
pto paste 'very interesting'
Visual Mode:
- Put cursor on the "v" -> (
very interesting) - Use
vi(yorvi)y. - Move your cursor to a new line and hit
pto 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+vto enter visual mode - Press
2jor pressjtwo times to go down two lines - (Or use
v2jin one swift ninja-move!) - Press
yto yank orxto cut - Move your cursor and use
pto paste after the cursor orPto 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" -> "
Ireally enjoy the company of animals." - Use
di"
Visual Mode
- Put cursor on the "I" -> "
Ireally 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:
iwill work within our containers.awill 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
disfor a sentence, ordipfor a paragraph
Visual Mode
- Put cursor in the beginning of a sentence or paragraph
- Use
visdfor a sentence, orvipdfor 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.
wto skip ahead a word (cursor at beginning of word)bto go back a word (cursor at beginning of word)eto skip ahead a word (cursor at end of word)geto 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:
dwto delete a word.d3wto delete three words.cwto delete word and put cursor in its place ("replace word")
Duplicate Duplicate
:t.Duplicate current line right below cursor:t 17Duplicate current line to below line 17- Replace
twithmto "move" the line instead of "duplicate"