Because error output is suppressed with the -silent suffix on the –remote-tab-silent flag, this flag can be used instead of the -p flag to open a new window when one doesn’t already exist. In other words, just use this last example and don’t bother using the first.

If you run gvim multilpe times so you have many gvim windows, you can send files to a specific window by using the –servername option. The title of the window will give the servername, e.g. GVIM, GVIM1, etc.

Editing and navigation

I have a graphical cheatsheet printed out and stuck in from of my monitor. There are loads available - just search for ‘vim cheatsheet’ in your favourite search engine (the one I use is from Graphical vi-vim Cheat Sheet and Tutorial). The list below contains some of the less commonly listed shortcuts and combinations - ones that aren’t explicitly on the cheatsheet or those that I’m most likely to forget.

From insert mode, hit escape then the key binding.

Key binding

Action

w

Go to the beginning of the next word, where a word contains alphanumeric characters and underscore.

W

Go to the beginning of the next WORD, where a WORD contains any non blank characters.

e

Go to the end of the next word (see above).

E

Go to the end of the next WORD (see above).

b

Go back to the beginning of the last word (see above).

B

Go back to the beginning of the last WORD (see above).

0

Go to the beginning of the line.

^

Go to the beginning of the first word on the line.

$

Go to the end of the line (end of the last word on the line). This includes spaces.

g_

Go to the end of the line, ignoring spaces.

f[char]

Move forward to the next occurence of [char]. A semicolon ; repeats the command.

F[char]

Move backward to the previous occurence of [char]. A semicolon ; repeats the command.

y$

Copy, i.e. yank, the text from the cursor to the end of the line. Paste with p.

yw

Copy a word.

:tabdo [action]

Perform an [action] across files in all tabs. e.g. :tabdo %s/foo/bar/g to replace foo with bar in all files.

ctrl-r

Redo

[[

Go to top of file.

]]

Go to bottom of file.

:[n],[m]d

Delete from line [n] to line [m]

[n],[m]dd

Delete from line [n] to line [n]+[m]

ctrl-f

Page up (forward).

ctrl-b

Page down (backward).

ctrl-u

1/2 page up.

ctrl-d

1/2 page down.

%

Jump from an open brace to its matching closing brace, or vice versa.

[{

Jump to the “{“ at the start of the current code block.

]}

jump to the “}” at the end of the current code block.

q[char]

Start recording macro [char]

q

Stop recording macro.

@[char]

Playback macro [char]

@@

Repeat macro playback.

m[char]

Create a mark [char]

y’[char]

Yank from mark [char] to current cursor position (operations other than yank can also be performed).

“+y

Yank to clipboard so that you can paste into an external application (operations other than yank can also be performed).

“+p

Paste from clipboard (also see “+y above).

“[char]y

Yank to buffer named [char] (operations other than yank can also be performed).

“[char]p

Paste from buffer named [char] (also see “[char]y above).

:s/[old]/[new]/g

Find and replace. Replace all occurrences of [old] with [new] on the current line. See Find and replace for more info.

@:

Repeat last find and replace. After you’ve done that once you can use @@ to repeat it (quicker to type).

Information display

Command history

You can view and navigate a list of previously entered commands.

Type q: to open the command history in a small viewport (note that you can edit the history as you would any other document). Navigate to a command and hit enter to execute it. Typing : again will place you back in the command mode, so you can execute a command like q to close the command history or [num] to quickly jump to command number [num].

Completion and intelligent completion with Omni-completion

Vim comes with a set of completion configuration scripts for a variety of languages. For Vim 7 on Gentoo Linux they are in /usr/share/vim/vim70/autoload/. These scripts allow you to start typing the beginning of a language’s word, like <ht and have Vim complete it for you, in this example by adding the ml to make <html.

Start typing the word (in insert mode) and then hit one of the following key bindings to complete the word:

Key binding

Action

ctrl-x-o

Complete the word by taking context into account. This is intelligent completion, aka omni completion.

ctrl-n

Complete the word using the first match of available words. Continue hitting ctrl-n to scroll through the possibilities.

ctrl-p

Complete the word using the last match of available words. Continue hitting ctrl-n to scroll through the possibilities.

Find and replace

On single line

Within selected area

Replace all occurrences of [old] with [new] in the last selected area. First select the area then run the command while it’s selected. It will also work on the previous selection.

:s/\%V[old]/[new]/g

Across entire file

Replace all occurrences of [old] with [new] in the entire file:

:%s/[old]/[new]/g

Between current line and end of file

Replace all occurrences of [old] with [new], between the current line and the end of file. Note that the .,$ is the range, where . is the current line and $ is the EOF.

:.,$ s/[old]/[new]/g

Between current line and another line

Replace all occurrences of [old] with [new], between the current line and the current line plus [n] lines. Note that .,.+[n] is the range, where . is the current line and .+[n] is the current line plus [n] lines. e.g. .,.+3 is the current line and the next 3 lines.

:.,.+[n] s/[old]/[new]/g |

Regexp capture and use

Use parenthesis to capture results. These have to be escaped i.e. \( to open and \) to close.

You can refer to the captured results by number, also escaped, i.e. \1, \2, etc.

E.g. to replace any occurrence of “foo” followed by a number with “bar” followed by that same number (e.g. foo123 -> bar123):

:s/foo\(\d?*\)/bar\1/g

The \(\d?*\) captures the digits and the \1 uses them in the replacement.

Switches

vim has lots of useful switches that can be activated from the editor.

Most of these switches can be disabled using the prefix ‘no’, for example :set numbers will show line numbers and :set nonumbers will hide line numbers.

Information

Show line numbers:

:set number

Search

Show the first match for the pattern, while you are still typing it:

:set incsearch

Ignore case:

:set ignorecase

Highlight all the search pattern matches in a file:

:set hlsearch

Formatting

For line wrapping:

:set wrap

The wrap option will split words. For wrapping that keeps keep words intact, also set the linebreak option when you set wrap:

:set wrap
:set lbr

For no wrap or line breaks:

:set nowrap
:set nolbr

Note that the wrap and line break options are for display only - neither will actually insert <EOL> (newline) characters into the file. If you want to wrap the lines and have <EOL>s inserted, set the textwidth option.