Effective Use of VIM - Part 2

Sunday, July 20, 2008

Effective Use of VIM - Part 2

The Effective Use of VIM Series...

» Effective Use of VIM - Part 1

» » Effective Use of VIM - Part 2

» Effective Use of VIM - Part 3

» Effective Use of VIM - Part 4

» Effective Use of VIM - Part 5

As promised, here is the Part 2 of the series on various Tips & Tricks for using VIM Effectively...

== PART 2 ==


This is one feature every programmer likes in VB and Eclipse, when you start typing it will show you all the matching completions. You can just select one and go on with your coding :)

VIM also supports this feature.

CTRL + n and CTRL + p will open the autocompletion box. You can use UP and DOWN to select one option and ENTER to use that word.

VIM searches the current file for matching completions. If you have tags (will come to that shortly), then VIM will search the whole tags for matches :D

Modifiers / Locators

There are ways to point to a particular location in a file...

gg - Start of file. Can also use :0
G - End of file
[number] - Line number [number]
+[number] - [number] lines down
-[number] - [number] lines up
% - Matching pair (already explained in Part 1)


There are times when you have a large file with lot of functions. You need to compare two functions which are at different locations in the file, with some functions in between. You would like to see the two functions on the screen. This is where folds comes in :)

You can just fold (hide) a part of the file.

Create fold:


This will fold according to the modifier ([number] will fold that much lines; % will fold till matching pair ...). It will show as a banner telling how much lines are there.



Point cursor at the fold and zo will open (unfold) the fold.

Multiple Files

Use can edit multiple files in the same VIM.

:e [file] - will open the [file] in another buffer.
vim [file list] - will open vim with all the files in the list (you can use *.c)

buffers - will show the details of the buffers including the filename and line at cursor.
b[number] - will go to buffer [number] (only if there are no unsaved changes)

You can cut, copy, paste between buffers.

VIM Commands / Options

:[TAB] - will iterate through all the vim commands, just like in a shell.
:s[TAB] - will show commands starting with s.

set all - will show all the options available to you.

Command Inclusion


Just type “bang bang” and any linux commands. The command will be executed and the output is pasted at the cursor location.

!! will be expanded to :.!, . means current cursor position. If we give :%! [command], the whole file contents is send as input to the command and the output is substituted for.

To include the contents of another file into this file, just do

!! cat [filename]
Or you can use - :r [filename]

To be continued...

Like it? Subscribe via RSS

You may have found out unique ways of doing things in VIM, pls share...

See also... » 10.1 Linux Commands I Can't Live Without

» Linux Commands I Hardly Knew - Reloaded

» Linux Commands I Hardly Knew

» Unlock a PC on Windiows XP ???

ATOzTOA : Latest Headlines


Anonymous said...

G not only goes to the last line, but any line when prefix with it's number.

also, i find bufexplorer to be an invaluable plugin when working with Multiple buffers.

Anonymous said...

As of Vim 7, i find tabs to be much easier to work with than buffers. :tabe will edit a file in a new tab. gt will go between tabs.

Anonymous said...

Here are a couple of commands that I often find myself using in a tabbed environment:

:wa - Write (save) all open tabs
:wqa - Write all and then quit vim.

These are handy when you have just done a multi-tab find/replace or something similar.

Keith Pickett said...

This is a bit late, I know, but I just found this.

To open multiple files, each in its own tab from the command line:
# vim -p file1 file2 ... fileN

or # vim -p *.c

Also, from within vim: To search for a given string, an alternative to "/some term" is to put your cursor on the word and/or string and enter "*" (star).

Thanks for the post. I found lots of great info here.

Post a Comment