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
- Tabs and buffers
- Editing and navigation
- Information display
- Command history
- Completion and intelligent completion with Omni-completion
- Find and replace
- Switches
- rc files
- Running external commands
- Ruby on Rails
- Diff and merge
- How to
- References
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]
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 |
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). |
gu | Change selection to lowercase. |
gU | Change selection to uppercase (caps / capital letters). |
Information display
Key binding | Action |
---|---|
g ctrl-g | Show information related to the current cursor position - column, line, word count, byte. |
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
Vim/gvim uses regular expressions aka regexp for its find and replace functionality.
On single line
Replace all occurrences of [old]
with [new]
on the current line:
:s/[old]/[new]/g
Within selected area
Replace all occurrences of [old]
with [new]
in the currently selected area (the '<,'>
is added automatically by virtue of having something selected, but I don’t know what this means - the command will replace ever occurrence on the line if you don’t specify \%V
):
:'<,'>s/\%V[old]/[new]/g
Replace all occurrences of [old]
with [new]
within 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.
Reference: Find and replace using regular expressions
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.
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
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
Running external commands
You can run an external [command] with the following:
:![command]
Use % to substitute in the filename. E.g. word count using the wc
program is :!wc %
.
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.
Diff and merge
You can use vim as a diff and merge tool. See Diff and merge using vim (or gvim)
How to
Miscellaneous how tos…
Prevent Vim stalling / running out of memory
If you’re performing a big operation on a large file (g)vim can easily run out of memory due storing undo history. To prevent this, turn off the undo history:
:set ul=-1
ul stands for ‘undo levels’.
Word count
Select some text and type the following get info about the selection at the bottom of gvim (column, line, word count, byte):
g ctrl-g
To run the linux wc
program:
:!wc %
The % is substituted with the filename.
References
- Vim online
- Vim documentation: options
- Efficient Editing With Vim”>Efficient Editing With Vim
- Graphical vi-vim Cheat Sheet and Tutorial
- VIM Useful Commands
- Omni completion. Vim documentation: Version 7
- Tip #14: Highlighting all the search pattern matches : vim online
- HowtoUseVimWithRails
- rails.vim
- Linux.com | Vim tips: Using tabs
- Linux.com | Vim tips
- Best of VIM Tips, gVIM’s Key Features
- C editing with VIM HOWTO: 4. Auto-Completing Words
- Tip #312: Copy, Cut, and Paste : vim online
- Vim documentation: gui_x11
- Vim Regular Expressions 101
- December 2002 Linux Productivity Magazine: VI and Vim
- How can I get (g)Vim to display the character count of the current file?
- Switching case of characters
- 8 Essential Vim Editor Navigation Fundamentals
- Search and replace in visual selection
- Word count - Vim Tips Wiki
- Vim runs out of memory during a find and replace