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 | Go to top

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]

Tabs and buffers | Go to top

Key bindingAction
:tabnewOpen 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
:bnNext buffer
:bpPrevious buffer
:lsList buffers
:b[n]Open buffer [n]
:bdClose buffer
ctrl-w wSwitch viewport

Editing and Navigation | Go to top

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 bindingAction
$Go to end of 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.
ywCopy 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-rRedo
[[Go to top of file
]]Go to bottom of file
:[n],[m]dDelete from line [n] to line [m]
[n],[m]ddDelete from line [n] to line [n]+[m]
ctrl-fPage up (forward)
ctrl-bPage down (backward)
ctrl-u1/2 page up
ctrl-d1/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]
qStop 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)
"+yYank to clipboard so that you can paste into an external application (operations other than yank can also be performed)
"+pPaste from clipboard (also see "+y above)
"[char]yYank to buffer named [char] (operations other than yank can also be performed)
"[char]pPaste from buffer named [char] (also see "[char]y above)
:.,$ s/[old]/[new]/gReplace 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]/gReplace 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.

Command history | Go to top

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 | Go to top

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 combinations to complete the word:

ctrl-x-oComplete the word by taking context into account. This is intelligent completion, aka omni completion.
ctrl-nComplete the word using the first match of available words. Continue hitting ctrl-n to scroll through the possibilities.
ctrl-pComplete the word using the last match of available words. Continue hitting ctrl-n to scroll through the possibilities.

Switches | Go to top

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.

To disable text width, set it to 0:

:set textwidth=0

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

:set list

rc files | Go to top

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

Ruby on Rails | Go to top

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.

Diff and merge | Go to top

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

References | Go to top

Last modified: 03/06/2010 Tags: (none)

Go to top

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