Installing Vim

Vim is usually pre-installed on most machines (I'm talking about real machines, not Windows computers). But you can also either install vim through a package manager, or from source.

For the configure options, I chose –enable-gui=gnome (instead of gtk2).


Recursive searching inside files:

:vimgrep /MySearchPattern/gj **/*.rb

This command searches all *.rb files in the current directory (do :pwd to see which dir you're in) and in all subdirs, for occurrences of MySearchPattern.

IMPORTANT: use :cw or :copen to open a window with the search results, or you'll be none the wiser…


  • g: all matches will be returned (instead of just one per line)
  • j: vim will not jump to the first match automatically

Alternative: EasyGrep plugin

Daniel Price's EasyGrep plugin comes in handy when you want to perform search and replace operations in multiple files (recursive search and replace).


<Leader>vr - Perform a global search search on the word under the cursor and prompt for a pattern with which to replace it.

By default, this command will look in all files (of the current directory) with the same extension as the current one.

Alternative: Grep (Just Grep)

:grep "my_keyword" -InR **/*.php
  • I: Ignore binary files
  • n: show line number
  • R: Recursive (recursive search in current directory, use pwd to show current dir)

Search for a Word Containing a Pattern

In Dutch, words may be glued together with other words to form new words. Here's a pattern which searches for words containing the word 'cursus', which means course.


This will find cursus, cursuscodes and cursusbestanden (course files). Unfortunately, w does not include 'foreign' characters - or unicode characters, so cursuscategorieën will not be found (or rather, it will be but only up till the 'ë').

The only way to circumvent this problem, is by including the special characters in the allowed range. \w is short for '[A-Za-z0-9_]', so we can expand with:


This selects the Dutch word cursuscategorieën in vim.

Search for Word at Beginning (i.e. Start) of Line

Look for a word at the very start of a line. This example is searching for php variables named $this:


^ means: the pattern is preceded by the start of a line

Here's an example which accounts for whitespace. In other words: the token is technically not at the beginning of the line, there might be whitespace before the token. The example search pattern finds all occurrences of $this at the start of the line, or preceded by indents (whitespace).

  • \s means: whitespace
  • *: zero or more occurrences of the preceding character (in the example: whitespace)

Replace Indented Word (Preserving the indents)

If you need to replace all occurrences of an indented word (i.e. word at the beginning of a line) with another word, you need to use “groupings”. Use brackets to define groupings. Example:


Here, all whitespace preceding the php variable $this is grouped. In a replacement string, you can refer to this group by using a number. Here's an example where we replace $this with $that:

:%s /\(^\s*\)\($this\)/\1\$that/g

Please note that :%s is not actually part of the regular expression, but the Ex command 'search all lines in the current file'.

Non-greedy Searching in Vim

Unlike most other regex engines (e.g. Perl), Vim does not have a non-greedy modifier (e.g. .*?), but a completely different version of the greedy match character.

  • .* means greedy match, any character
  • .\{-} means non-greedy match, any character

Please refer to :help pattern-overview (on the vim command line) for a complete explanation.

Here's an example where we use the non-greedy pattern to construct a match for multiline comments:


This search pattern can be applied to delete all multiline comments. Given this javascript code:

var i = 0;
 * First piece of comment
 * spread out over 
 * multiple lines
var j = 0;
 * Second piece
 * of comment
var k = 0;

The following vim command will wipe out all multiline comments, but leave the actual code:

:%s /\/\*\_.\{-}\*\///g

To make this (slightly) more clear, you could also use a different character for a separator, e.g. #:

:%s #\/\*\_.\{-}\*\/##g

Pasting a String to the Command Line (Ed)

  1. Select the string, e.g.:
  2. Yank the string
  3. Go to the command line
  4. Past the code from register 0:
    Ctrl-R 0

Pasting Word Under Cursor to the Command Line (Ed)

Alternatively, you can paste the word under the cursor to the command line:

Ctrl-R Ctrl-W


Show hidden files: shift-I


Show colors for hexadecimals: <Leader> - F2. Very handy for previewing colors in css stylesheets.

Align Assignments

Put '=' in one column, and assignment values as well. Use ;= command. Example:

    // Severity values, following the RFC 3164 rules:
    const  EMERGENCY = 0;
    const  ALERT = 1;
    const  CRITICAL = 2;
    const  ERROR = 3;
    const  WARNING = 4;
    const  NOTICE = 5;
    const  INFORMATIONAL = 6;
    const  DEBUG = 7;

Go to assignment block, hit ;= to convert to:

    // Severity values, following the RFC 3164 rules:
    const  EMERGENCY     = 0;
    const  ALERT         = 1;
    const  CRITICAL      = 2;
    const  ERROR         = 3;
    const  WARNING       = 4;
    const  NOTICE        = 5;
    const  INFORMATIONAL = 6;
    const  DEBUG         = 7;

Vim plugin needed: AlignAssignments.vim.

Copy Filepath to Clipboard

From stackoverflow: :let @” = expand(”%:p”) will copy the filepath of the current buffer into the unnamed register.

To copy the filepath into the system clipboard, use :let @+ = expand(”%:p”)

Just remap this command for convenience by putting this in your .vimrc file:

noremap <silent> <F6> :let @+=expand("%:p")<CR>

(Replace <F6> by any non-taken key you wish.)

Personal Tools