Tuesday, July 15, 2008

Effective Use of VIM - Part 1

This is for people who love VIM and also for people who hate VIM.

Some of you may be wondering what VIM is. Let me find the expansion, honestly I don't know.

Here you go:

Vim was created as an extended version of the vi editor, with many additional features designed to be helpful in editing program source code. Its full name is Vi IMproved.
- AND -
vi is a screen-oriented text editor written by Bill Joy in 1976 for an early BSD release. The name vi is derived from the shortest unambiguous abbreviation for the command visual in ex, the command in question switches the line editor ex to visual mode.

That is with the origin...

Ok, this is going to be a multipart series on various tips in using VIM Effectively.
- For VIM Experts, there may be some insights.
- For Novices, this will make you like VIM.

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



== PART 1 ==

Starting VIM

You can execute VIM by the command vim.

It is better to use VIM than vi. You can just define,

alias vi=”vim”

and put it in your .bash_profile.

There is .vimrc file which stores all the options for VIM, it is in your home directory (~). When VIM starts, it will get options from .vimrc. You can also set options while inside VIM, but they will be lost once you exit VIM.

Exiting VIM

Lot of you may not know how to exit VIM. Here are some methods...

[ When you type ESC, you will get to Normal mode. The commands starting with : (colon) are given there. ]

:q - Exits VIM, if no changes yet to be saved.
:q! - Exits VIM, discarding unsaved changes.
ZZ (Just hold SHIFT and press z twice) - Save changes and Exit.
:wq - Save changes and Exit.
:x - Save changes if any and Exit.
CTRL + w, q (While holding CTRL, press w and then q, while pressing q, CTRL is not a must) - Exits VIM, if no changes yet to be saved.

Kill VIM !!! (why would I need that?)

Multiple Windows

VIM supports multiple windows inside the VIM window.

CTRL + w, s – Split Horizontally, reopen the same file in the new window.
CTRL + w, v – Split Vertically
CTRL + w, n – Opens a Blank Window
CTRL + w, c – Close active window
CTRL + w, o – Close all windows except the active one.

[ Closing window occurs only if there is no unsaved changes ]

Match Pairs - %

% is used to match paired characters.
If the cursor is at a starting brace, pressing % will take cursor to the matching closing brace, and vice versa.
This works for () (parantheses), /**/ (block comments) etc.

This is handy, can be used extensively with other commands like d (cut), y (copy) etc.

If you need to delete an entire function block, place cursor at either one of the braces and d% will delete that full block.

Abbreviations

You can use abbreviations for the most commonly used commands snippets.

Define:

iab <key> <expansion>
<key> is the string which should be expanded to <expansion>

You can either add in .vimrc file or give while editing.

Use:

<key><SPACE> will expand to <expansion><SPACE>

You can also use <key><CTRL + ]>

Eg. : iab #i #include
- replace #i with #include

( I dont know a way to disable expanding for a particular instance only, you will have to manually edit that. )

To be continued...


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

6 comments:

Srikanth said...

Nice posts, keep it coming.

And please remove the "snap shots(tm)" that loads the hyperlinked page when you hover the mouse over a hyper link text. It's very irritating.

atoztoa said...

@srikanth:

Removed Snap Shots :)

Thanks for the feedback... keep reading...

Anonymous said...

C-o in insert mode is quite nice

Marius Gedminas said...

To prevent the abbreviation from expanding just this once, press Ctrl-V before pressing Space.

Renato Golin said...

:sp filename
splits the screen and open the file at the same time

CTRL+W + up/down/left/right
moves around split screens

:tabnew filename
creates a new tab (not buffer, not split)

:tabnext/tabprev/tablast/tabfirst
moves around tabs

CTRL+PgUp / PgDn
also moves around, like Firefox, Gnome console etc.

Post a Comment