Effective Use of VIM - Part 5

Wednesday, January 7, 2009

Effective Use of VIM - Part 5

VIM Editor
It has been some while since Part 4, and here is Part 5 of the series on various Tips & Tricks for using VIM Effectively... fully packed with fun...

BTW, Vim 7.2 has been released, get it here... http://www.vim.org/download.php

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

== PART 5 ==

More Make, Grep

We have seen how to use make, grep from inside VIM in Part 3.

Here are some more options...

:copen - Open the list of locations in a new split window. You can go to an item, by highlighting it and pressing enter. It will be opened in the main split.
CTRL + w, UP/DOWN/RIGHT/LEFT will move focus to corresponding window.
CTRL + w, w will move between the windows, in order.

:cclose - Close the list window.

:cc - See Current. If you go to a particular item and move around the file, :cc will take you back to the location of the (current) item.

:cl - See List. Show a list of the items, which can be used to go to an item using :cn[number] which is relative, just a quick calculation will do.

Basic Usage of VIM

Most us haven't ever seen this help...

usage: vim [arguments] [file ..] edit specified file(s)
or: vim [arguments] - read text from stdin
or: vim [arguments] -t tag edit file where tag is defined
or: vim [arguments] -q [errorfile] edit file with first error

Reading from stdin - this one is very useful. You can just type whatever you want. You can stop reading from stdin by CTRL + d.

Let's Jump

These are various keys used to jump around inside VIM...

I thought the keys on the keyboard will be of interest to you.

`` (2 Acutes) - jumps between start and current cursor location.
( - jumps to start of file.
) - jumps to end of file.
`. (Acute and Dot) - moves the cursor to the line and column where the last edit was made.
'. - moves the cursor to the line where the last edit was made.
'" - moves the cursor to the last position of the cursor when you exited the previous session.

:jumps - shows the jumplist. List of files modified in order.
CTRL + o - moves the cursor to the last jump.
CTRL + i - moves the cursor to the previous jump.

H - moves the cursor to the top of the screen or viewport (viewport means what all you see in a screen at a time).
M - moves the cursor to the middle of the screen or viewport.
L - moves the cursor to the bottom of the screen or viewport.


Mark is another way of jumping around in VIM. Like bookmarks inside HTML pages.

We need to set marks for important locations in the file. After that we can jump to that locations much easily.

mx - tells Vim to add a mark called x. x can be any character.
`x (Acute and x) - tells Vim to return to the line and column for mark x.
'x - tells Vim to return to the beginning of the line where mark x is set.
:marks - shows all marks set.
:marks x - shows the mark named x.

Working with Multiple Files again?

You already know how to open multiple files in buffers from Part 2.

Here is more on multiple files...

vim -o [filelist] - Will open the files in multiple horizontal splits.

vim -O [filelist] - Same as above, but use vertical splits.

vim -p [filelist] - Opens the files in multiple tabs.

:tabnew [filename] OR :tabe [filename] - opens the file in a new tab.

:tabnext/tabprev/tablast/tabfirst - moves around multiple tabs.

For effective use of tabs: check this.

:wa - Save all open files.

:qa - Quit all the open files in the VIM.

Directory traversal

You can browse through the filesystem from within VIM.

vim [dir] - will open the filelist inside VIM.

Then we can use following keys for particular operations.

ENTER - open the file at cursor / go to subdir.
D - delete file at cursor.
R - rename file at cursor.
x - execute file at cursor.
- - go to previous directory.

Block Select Mode

One really great thing I liked about MS Word is its block select mode. When we drag while pressing ALT, it will select all text inside that rectangle. Very handy tool.

We have block select mode in VIM too...

CTRL + v - start Block Select Mode. Selection rectangle is drawn using UP, DOWN, RIGHT, LEFT.

You will have a pivot point around which the rectangle can be moved.

Miscellaneous Tips...

d + UP - delete current line and previous line
d + DOWN – delete current line and next line
y + UP – yank current line and previous line
y + DOWN – yank current line and next line

:CTRL + f - will open the history of VIM commands in a split. Can execute by highlighting and pressing ENTER.

P - paste above / left of the cursor.
p - paste below / right of the cursor.

D - deletes till the end of the line. d$ also does the same.

:shell - Open a shell. Returns to VIM once you close the shell.

Start typing a VIM command after : and pressing UP will match it with previous commands starting with that string.

. (Dot) - will perform the previous action like d, y etc. If you used 5dd for deleting five lines. Pressing . will do that action again and delete 5 more lines.

When using search and replace...

We can replace a particular string pattern with another using s.

Syntax - :[location]s/[source]/[destination]/[g][c]
[location] - where to search. 10,20 will search from line 10 to 20. % will search whole file.
[source] is replaced by [destination]
g - replaces globally
c - asks for confirmation

The [source] and [destination] can have control characters for special purposes. One particular is &. & anywhere in the [destination] will be replaced by [source].

:%s/hello/& world/g - will replace 'hello' with 'hello world'

To be continued...

Like it? Subscribe via RSS

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

See also... » Adding File Banner to Multiple Files

» Replace Tab using Sed

» 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


Bonus said...

Nice article, two things to note though. ( and ) move to the beginning and end of the sentence, respectively, not beginning and end of file.
Also `` and '' don't jump between current location and start of file, rather they jump between current location and the previous absolute jump (like if you do 10gg). If there's no absolute jump then I think the beginning of the file is used.

Pento said...

Nice article, but

( [count] sentences backward. |exclusive| motion.
) [count] sentences forward. |exclusive| motion.

instead of
( - jumps to start of file.
) - jumps to end of file.

Anonymous said...

Incredibly useful for a fresh vim with no custom .vimrc is :mk which writes the current settings out to .vimrc automatically. Customise while in vim, then write out rather than editing the rc file manually.


PS. The captcha on this blog is terrible. I had to use IE to get this posted.

atoztoa said...

Hi Strawp,

Thanks for the info... it is a life saver... usually i set each time i need some settings and forget to add to .vimrc...

BTW, sorry for the captcha, it is Google :)

Anonymous said...

Did you mention cut and paste?

atoztoa said...


This series assumes the reader uses Vim or knows what Vim is. So, I have omitted the most common actions.

Give "vimtutor" and VIM itself takes you through a tour of its working.

Post a Comment