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

Vim/Notes

Vim/Tabs

Introduction

Documentation

You can find plenty of documentation available for Vim here: http://www.vim.org/docs.php

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: http://vimdoc.sourceforge.net/

Configuration

.vimrc

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: http://vim.wikia.com/wiki/Fix_broken_arrow_key_navigation_in_insert_mode

Searching

Searching in Vim utilizes regular expressions.

Find

To search for an expression, type:

/pattern

Find and Replace

To find and replace an expression, type:

:%s/peanutbutter/jelly/g

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:

/[0-9][0-9]


Multitasking

Windows

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


Tabs

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: http://stackoverflow.com/questions/7573368/in-place-edits-with-sed-on-os-x/29735769#29735769

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 **/*.*

References