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:

  1. Put cursor on the "v" -> (very interesting)
  2. Use yi( or yi).
  3. Move your cursor to a new line and hit p to paste 'very interesting'

Visual Mode:

  1. Put cursor on the "v" -> (very interesting)
  2. Use vi(y or vi)y.
  3. 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

  1. Put your cursor on the top line
  2. Use shift+v to enter visual mode
  3. Press 2j or press j two times to go down two lines
  4. (Or use v2j in one swift ninja-move!)
  5. Press y to yank or x to cut
  6. Move your cursor and use p to paste after the cursor or P 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

  1. Put cursor on the "I" -> "I really enjoy the company of animals."
  2. Use di"

Visual Mode

  1. Put cursor on the "I" -> "I really enjoy the company of animals."
  2. 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), use ya( instead of yi(

Delete a sentence or paragraph

Normal Mode

  1. Put cursor in the beginning of a sentence or paragraph
  2. Use dis for a sentence, or dip for a paragraph

Visual Mode

  1. Put cursor in the beginning of a sentence or paragraph
  2. Use visd for a sentence, or vipd 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

second tab

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.

second tab

Movin' the cursor aboot.

  1. w to skip ahead a word (cursor at beginning of word)
  2. b to go back a word (cursor at beginning of word)
  3. e to skip ahead a word (cursor at end of word)
  4. ge to go back a word (cursor at end of word)
  5. $ to go to the end of a line
  6. ^ to go to the beginning of a line

Deletin' all over the place.

  1. If you're at line 10 and need to delete line 20, type :20d
  2. If you're at line 10 and need to delete lines 20-25, type :20,15d
  3. If you want to move your cursor back to its original place, use ctrl+o

Some other tricks:

  1. dw to delete a word. d3w to delete three words.
  2. cw to delete word and put cursor in its place ("replace word")

Duplicate Duplicate

  1. :t. Duplicate current line right below cursor
  2. :t 17 Duplicate current line to below line 17
  3. Replace t with m to "move" the line instead of "duplicate"