Effective Use of VIM - Part 1

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

» Effective Use of VIM - Part 5



== 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...

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


13 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.

Anonymous said...

As to the name: vi and vim are both forms of the Latin word vis ("power", "force").

trench said...

trench hearts vim.

If you want to have some real fun create yourself a new firefox profile (terminal: firefox -profilemanager), call it VIM, then head over to https://addons.mozilla.org/en-US/firefox/ and search for Vimperator ( https://addons.mozilla.org/en-US/firefox/addon/4891 ) and install it.

Then, close all firefox sessions, and in bash...

------

vim ffvim

i (insert)

#!/bin/sh
firefox -P VIM --no-remote &

[esc]:wq! (write & quit)

sudo chmod 755 ffvim && mv ffvim /bin/

------

Now you can browse the tubes using firefox with vim commands. Setting it up on a new profile is wonderful because it allows you to play with it until you are used to the new vim interface for firefox.

From command-line you can call your VIM Profile with simply ffvim. [in the script, "-P VIM" calls the profile you created. "--no-remote" allows multiple firefox profiles to be open. And "&" pushes the service into the background.]

As a sidenote I've also used the above method to create multiple firefox profiles to ease the strain on firefox, my addons, and my system. For example I can call, from terminal, ffsn (opens my social network profile with my social network addons)... or ffblog (my blogging-exclusive profile with blogging-exclusive addons)... etc.

Also added ffx

------

#!/bin/sh
killall firefox

------

and ffch

------

#!/bin/sh
killall firefox $1

------

The ffx kills all my profiles when and if they get out of hand. The ffch is used like "ffch ffsn(or whatever profile you want to call)" and it kills all profiles while calling the new one.

If maintained well you'll be able to speed firefox up immensely and, best: have your very own VIM-based firefox.

Of course, if you don't want multiple profiles you can just enable and disable the Vimperator addon, but if you are new to the interface that will quickly become a pain in the ass.

PS Awesome vim write-up.

trench said...

Oops.

ffch is actually:

------

#!/bin/sh
ffx && $1

------

Sorry.

:D

Post a Comment