From charlesreid1

Vim is a very handy, full-featured, lightweight text editor available on all Unix systems. Vim and Emacs are two of the most common text editors available on Unix systems.

See: Vim wikipedia article

Vim/Common Actions





You can find plenty of documentation available for Vim here:

You can also type :help from Vim, which will open Vim help, and you can navigate from there.

This documentation is also available online here:



The .vimrc file initializes your Vim environment whenever you start up Vim. This is where you specify, e.g., default color schemes, turn on autoindent, etc.

Handy link - with this capital O + escapekeys fiasco:


Searching in Vim utilizes regular expressions.


To search for an expression, type:


Find and Replace

To find and replace an expression, type:


where the g means "global", i.e. replace all instances of "peanutbutter" with "jelly" (instead of just the first instance).

Using Regular Expressions

You can also utilize regular expressions in either finding or finding and replacing. For example, you can search for a 2-digit number by using:




Vim has a really handy feature that allows you to divide the screen of the text editor into multiple sections, called windows.

:sp - split the current window into two parts, horizontally - "top" and "bottom"

:vsp - split the current window into two parts, vertically - "left" and "right"

To open multiple files and split them into their own windows, use the -o and -O command-line options.

The following will open these 3 files split horizontally:

$ vim -o file1 file2 file3

and this will open the three files split vertically:

$ vim -O file1 file2 file3


Like windows, tabs provide a way to handle several files at once. However, windows allow you to open and look at multiple files at once. Tabs, on the other hand, like browser tabs, allow you to look at one file at a time but still have other files open, for easy switching back and forth between open files.

Tab commands include:

:tabnew - creates a new tab and switches to it

:tabnext - go to the next tab

:tabn - ditto

:tabprevious - go to the previous tab

:tabp - ditto

:tabfirst - switch to first tab (good if you have lots of tabs open)

:tabfir - ditto

:tablast - switch to last tab

:tabf something* - find tabs with open files whose names match "something*"

:tabs - list all open tabs and what files they are open to

Interfacing with the Shell

Vim Ex Mode

This link on Stack Overflow is the source of information for this, and it has been battle-tested:

led me to learn about how to use vim from the command line - without actually opening vim. This is very handy for doing those tasks that vim is particularly good at. Especially when I know the right escape characters for some awful regular expression, and it works great in vim, but it chokes the system sed on a fish bone `` somewhere.

Search/Replace in Single File From Command Line

For a single file, can run a vim-like search:

$ ex -s +%s/foo/bar/g -cwq file.txt
$ ex -s '%s/foo/bar/g' -cwq file.txt

Search/Replace in Multiple Files From Command Line

$ ex -s +'bufdo!%s/foo/bar/g' -cxa *.*

A more complicated example:

ex -s +'bufdo!%s/\(visualization..[jc]s\)/c3\/\1/g' -cxa *.*

Search/Replace in Multiple Subdirectories

To find replace recursively:

$ ex -s +'bufdo!%s/foo/bar/g' -cxa **/*.*

Keyboard Shortcuts


The leader key gives you a namespace for keyboard shortcuts. The leader key can be set to whatever you want but by default it is backslash \\. To make life easier, you can remap it to a closer key, like comma.

Different Kinds of Mapping

To create key mappings, edit your ~/.vimrc.

There are a few different ways to remap a key in your .vimrc:

  • map - recursive mapping; this will take the expression on the right side of the expression (what the keyboard shortcut is supposed to do) and substitute any actions that those keys are a shortcut for
  • noremap - non-recursive mapping; this does the same thing as the map above, except that it doesn't replace the right side of the assignment with further (recursive) keyboard shortcuts
  • nmap - normal mode recursive mapping; works like map, but only applies when in normal mode
  • nnoremap - normal mode non-recursive mapping
  • vmap - visual mode recursive mapping; works like map, but only applies when in visual mode
  • vnoremap - visual mode non-recursive mapping

Map example

Suppose my .vimrc contains:

:map j gg
:map Q j

First, this will remap the shortcut key (j) to jump to the top of the document (gg).

Second, this will remap the shortcut key (Q) to another key (j), and because this is a recursive mapping, it will substitute the (j) shortcut just defined as (gg).

So you end up with keybindings of (j) to (gg) and (Q) to (gg).

No Recursive Map Example

Suppose my .vimrc contains:

:map j gg
:noremap W j

First, this will remap the shortcut key (j) to jump to the top of the document (gg).

Second, the keyboard shortcut key (W) will be remapped to the key (j). This is not a recursive mapping, so (W) maps to a literal (j), not to (gg).

Getting Help on Mapping

Get help on specific mapping commands:

:help :map
:help :noremap

Get help on mapping modes in general:

:help :map-modes
:help recursive_mapping

Vim Configuration

Replacing source with runtime

Normally you can use the :source command to read commands from a file. You can load commands from a file as:

source ~/.vim/mappings.vim

However, this is not really sustainable for complicated vim configurations. Instead, you can use :runtime to load files that work with the file layout of the runtime path of vim.

If you say runtime and a name, it will look in all runtime path directories and will load the first one it finds.

If you say runtime! and a name, it will look in all runtime path directories and it will load every one that it finds.

You can also do pattern matching.

runtime syntax/c.vim
runtime! syntax/c.vim
runtime! */maps.vim
runtime! **/maps.vim

The leading ~/.vim should be left out.

The double asterisk can look as many directories deep as you need.