vim and gvim

vim is a text editor, common on many *nix platforms. gvim is its GUI. All of the notes have been tested with gvim, but should also be applicable to vim.

Here’s a good intro to the basics of navigation and editing: Efficient Editing With Vim.

Index

Opening files

Open several files in tabs:

gvim -p [file1] [file2]

If you run this command again, with different files, you will get a new gvim window.

To open other files in the existing gvim window, in new tabs:

gvim --remote-tab-silent [file3] [file4]

I created a bash alias to do this. See Bash aliases.

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.

gvim --servername GVIM1 --remote-tab-silent [file3] [file4]

Go to top

Tabs and buffers

Key binding Action
:tabnew Open a new tab
:tabnew [filename] Open [filename] in a new tab
:tabf [filename] Search for [filename] in your current path and open it in a new tab
:bn Next buffer
:bp Previous buffer
:ls List buffers
:b[n] Open buffer [n]
:bd Close buffer
ctrl-w w Switch viewport

Go to top

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).
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.
:s/\%V[old]/[new]/g Find and replace. Replace all occurrences of [old] with [new] in the last selected area. First select the area then press Escape to deselect it before running the command.
:%s/[old]/[new]/g Find and replace. Replace all occurrences of [old] with [new] in the entire file.
:.,$ s/[old]/[new]/g Find and replace. 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.
:.,.+[n] s/[old]/[new]/g 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.
@: Repeat last find and replace. After you’ve done that once you can use @@ to repeat it (quicker to type).
gu Change selection to lowercase.
gU Change selection to uppercase (caps / capital letters).

Go to top

Information display

Key binding Action
g ctrl-g Show information related to the current cursor position - column, line, word count, byte.

Go to top

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].

Go to top

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.

Go to top

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

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.

To disable text width, set it to 0:

:set textwidth=0

Show invisible characters - tabs as ^I and end of line as $.

:set list

Go to top

rc files

Most vim options can be set in either /etc/vim/vimrc to apply the options to all users, or in /home/[username]/.vimrc to apply the options for the user [username].

You can add comments to the rc file using the " (double quote) character.

For example, I added the following to the end of my /etc/vim/vimrc file to set various search and formatting options.

set ignorecase
set incsearch " Highlight search terms as you type.
set hlsearch " Highlight all matched search terms.
set nolbr " No line break.
set wrap
set number

Go to top

Ruby on Rails

There are a number of helper scripts that can be used to aid development of Ruby or Ruby on Rails applications.

First, check out HowtoUseVimWithRails. Of the suggestions in this article, I only use the rails.vim so far. See my page on vim with the rails.vim plugin for further information.

Go to top

Diff and merge

You can use vim as a diff and merge tool. See Diff and merge using vim (or gvim)

Go to top

References

Go to top

Last modified: 12/10/2012 Tags: ,

This website is a personal resource. Nothing here is guaranteed correct or complete, so use at your own risk and try not to delete the Internet. -Stephan

Site Info

Privacy policy

Go to top