ARTICLE

Unlock the Power of VIM
Contributed by Girish Venkatachalam in Applications on 2007-03-20 12:07:24

vi editor is something that UNIX newbies often like to criticize. Until they learn it well and understand why vi is vi and not something else.It is a marvellous creation of Bill Joy and one cannot but think of it without a feeling of magic and spookiness.

It has the same demonic characteristics of other UNIX concepts like daemons and pipes. It has a steep learning curve but it is worth the effort since the power and versatility of vi cannot be matched.

Vim stands for Vi IMproved and was developed by Bram Moolenaar. That took vi to the next level making it run even on Microsoft Windows. Many people like to differentiate between vi, nvi and vim. There are plenty of subtle differences and vim today is so feature rich and powerful that one can argue that there is a certain feature bloat.

Whatever it is, there is no editor I know that can highlight syntax of various file formats like vim. Syntax highlighting can be a boon for serious programmers and network administrators who spend countless hours working and are given to carelessness after a sleepless night. Vim would clearly show simple errors and typos even before you write the file to disk.

The online documentation is so comprehensive and user friendly that you don't have to go out of vim or google for figuring out the way to achieve common tasks. At the same time there is a lot to learn and most of the features stay unused. However if you learn to use vim well you do not need to learn any other editor since you can use it for every single editing purpose.

I type my e-mails in vim, I am writing this document in vim, I code using vim (but of course) and nowadays I benefit by its spell checking capability.

It has got a powerful plugin and scripting capability using which you can use third party add ons. You can find plenty of them at www.vim.org. You can very easily add key mappings for executing arbitrary commands.

For instance, one thing I often need is the ability to delete from the current line till the end of the file. This need often arises while sending replies to e-mails. Hence I have this mapping in my .vimrc.

nmap   :,$d

You can get really creative and use input and output filtering using vim's shell I/O mechanism.

There is support for tabs starting version 7.0 and you can open multiple files in separate tabs using the command.

$vim -p file1.txt file2.txt ...

You can open multiple tabs and switch between tabs using the "gt" hotkey sequence. You can open a new tab from inside of vim using

:tabnew file.txt

Inside each tab you can open multiple files as usual using the traditional vi mechanism.

But vi is an editor and editor alone. It does not try to be everything to everybody. Sometimes I feel that is what makes vim charming.

You can enable spell checking with this.

:se spell spelllang=en_us 

Vim has a powerful folding feature which is not often talked about. You can create folds and remove them just like you would fold paper or clothes.

For instance if you wish to fold the first 10 lines of a file, just typethis.

:1,10fold

You can open folds by typing "zo" at a fold. Most fold operations start with the 'z' key.

For more details type

:help fold

from inside vim.

One important detail needs to be told about vim help documentation navigation. You see certain words and phrases highlighted with a turquoise color. Those are links and you can "click that link" by pressing the Ctrl-] key combination. This is the standard way of going to function definitions whilst using c tags.

Of course I merge source files with vim or vimdiff to be precise. Differences are highlighted so well and in massive software projects highlighting the diff output can be inevitable. This often occurs when using a revision control system like cvs or svn.

Vim can format paragraphs using the '=' operator. You can right justify or left justify by selecting the paragraph either by using the 'v' operator or colon and line range. After that you type a colon command like this.

:1,10le:1,10ce:1,10ri

for left justified, centered and right justified text.

For counting the number of words in a file type the"g Ctrl-g" key combo.

You can write simple vim scripts using the builtin functions or create your own. A function is created by using the :function .

Note that command names and function names have to begin with an Uppercase letter.

Happy vimming!


 
Discussion(s)
What?
Written by nigel on 2007-03-21 17:15:56
Type emails in vim? Get with the times, dude.
Discuss! Reply!

Don't judge something you don't know abo
Written by Matthias Fassl on 2007-03-21 17:43:56
Quote:

Type emails in vim? Get with the times, dude.




Clearly you have not yet experienced the power of vi(m)
Discuss! Reply!

lol
Written by Noobie Noobson on 2007-03-21 19:05:20
Quote:

Type emails in vim? Get with the times, dude.




So what do you use? Outlook Express? VIM is very powerful when it comes to email writing.
Discuss! Reply!

YEH!!
Written by JMC on 2007-03-21 23:19:20
Vim is teh suck for emailz. I use Microsoft's Outlook Express.
Discuss! Reply!

moron!
Written by vimuser on 2007-03-22 09:35:07
moron!
Discuss! Reply!

Ha!
Written by Anonymous on 2007-06-08 07:40:04
Quote:

Type emails in vim? Get with the times, dude.




You might probably be one of those who send viagra-related image attachments from Outsuck Express *evil grin*
Discuss! Reply!

Nice article about vim
Written by Srinivas Rao on 2008-03-21 05:13:43
Its nice article. VIM is like an ocean, How much ever you start using vim, you unleash more of it. Its the best text editor of current times.


http://msrinirao.blogspot.com
Discuss! Reply!

macro not needed
Written by ky13 on 2007-03-21 17:46:06
Pressing 'dG' will delete from the current line to the end of the file.
Discuss! Reply!

more about 'd'
Written by ky13 on 2007-03-21 17:48:37
Similarly, pressing 'dX', where X is a line number, will delete from the current line to that line number.
Discuss! Reply!

Some more tips...
Written by Eric on 2007-03-21 18:54:43
Nice tips. I've been using Vim for 4-5 years and I still feel like I barely harness its power.

BTW, you can also use ctrl-pg up/down (like in Firefox, etc.) to change your tabs. I think this is the default.

Also, if you can create a fold another way. Just select your area with 'v', and type 'zf'.

People who diss Vim/vi basically don't know Vim/vi or in what circumstances it has no rival.

Tally ho!
Eric
Discuss! Reply!

Macros
Written by Brian on 2007-03-21 19:20:22
One of the things that you didn't elaborate on is the power of user-defined macros. If you have to do text manipulation, macros in vim is a great alternative to writing a script in perl, python, etc. that recognizes regular expressions. In addition, vim automatically saves all of your macros until you overwrite them, letting you use the same macro for multiple files without re-entering it. Vim also lets your perform repetitions of a macro, so if you want something done 1000 times, you can write a macro for one instance and repeat it. I don't claim to be an expert and I just *really* started using macros to their potential a few weeks ago, but I would suggest that people read up on the topic because it can be a valuable asset in a programmer's toolbox.
Discuss! Reply!

The Problem with VI(M)
Written by The problem with VI(M) on 2007-03-21 21:19:58
It's bloated and not with features. Why DO I need to type out every little command? Why is the UI so hard to use? I can achieve anything VI(M) can do with text editors that can do syntax highlighting. And better yet, they work like Notepad/Wordpad. It's quick, easy, and just as powerful.

I don't need to type 5 keys just to edit a few lines (or rather move to the lines to begin to edit them). I just type one (UP, DOWN, LEFT, OR RIGHT).
Discuss! Reply!

Hah
Written by Jesse on 2007-03-21 21:45:11
Quote:

It's bloated and not with features. Why DO I need to type out every little command? Why is the UI so hard to use? I can achieve anything VI(M) can do with text editors that can do syntax highlighting. And better yet, they work like Notepad/Wordpad. It's quick, easy, and just as powerful.

I don't need to type 5 keys just to edit a few lines (or rather move to the lines to begin to edit them). I just type one (UP, DOWN, LEFT, OR RIGHT).





Lame. Are you sure that you have ever used Vim? I use the Windows version all the time and it has all the movement and copy&paste functions of those others that you mention.
Discuss! Reply!

All it takes is some investment in learn
Written by Bill-nz on 2007-03-21 22:15:45
Quote:

It's bloated and not with features. Why DO I need to type out every little command? Why is the UI so hard to use? I can achieve anything VI(M) can do with text editors that can do syntax highlighting. And better yet, they work like Notepad/Wordpad. It's quick, easy, and just as powerful.

I don't need to type 5 keys just to edit a few lines (or rather move to the lines to begin to edit them). I just type one (UP, DOWN, LEFT, OR RIGHT).





When I first started using vim, I felt the same way you did. Why are there all these arcane commands that don't make any sense?

THEN, when I actually started using them, I realized that they are what they are for a reason. For instance, the movement keys h, j, k, & l seem really odd at first, until you get used to them. They are TONS better than cursor keys in that I don't have to move my right hand to navigate the page. And when I get my cursor where I want it, I just press "i" for insert, and when I'm done just press escape to continue editing. Once you get over the "culture shock" of moving from menu'ed applications to a tool like vim, you start to appreciate the thought that went into what commands do what. If you are the type of person that reaches for his mouse all the time, vim isn't your tool. But if you take the time to learn it, vim can make you exponentially more productive.

Think of it like this: Notepad is a sub-compact car with automatic transmission. vim is a 30-gear transport. If all you ever drove was the sub-compact, you would think that the extra time needed to learn how to drive the 30-gear transport a waist of time. I mean, why is there a clutch? Why 30 gears when 3 will do? It's all about how it's used. If you need to get to write a simple text document, then notepad will be fine. If you need to manipulate huge amounts of information, you need a tool with more lifting power. If you want to see the real power of vim, watch someone who's been using it for a while and knows it, and you'll see where your notepad-type editors slow you down.
Discuss! Reply!

Wow.. you OBVIOUSLY come from a differen
Written by JMC on 2007-03-21 23:24:39
I gaurantee you that I could leave you in the dust with my Vim and your notepad. Please do not speak of things that you know absolutely nothing about.
Discuss! Reply!

good article
Written by vim vini vinci on 2007-03-21 22:46:57
any time i look at the vim documentation or find some article on the web about it, i learn something new
or at least remember some tip

vim is so complex and powerful that amazes me anytime i read its doc
Discuss! Reply!

Vim not really appreciated until...
Written by Jeff on 2007-03-22 00:40:14
you use it for awhile. I taught a unix course for about 5 years. My chapter on vi always brought a groan from at least one student who heard how difficult it was. Once I showed them examples of macros, regular expression substitutions, abbreviations, etc. they started to see that this wasn't your normal text editor.

I always added that vi is usually on every unix box you will ever come across.
Discuss! Reply!

Favorit
Written by Eelco on 2007-03-22 03:17:38
I once learned VI basics in a Unix course and had much the same thought as it being bloated and not very userfriendly. Never done anything with it until a few months ago, when I saw a colleague using it (FAST!!!). I decided to give it a try, worked my way through the tutorial. Now it really is my preferred editor, although my knowledge of it is still very little. Often I like it more than my IDE (Java), because of it's speed and not needing the mouse so much.

Not to start a flame war, but I do like Emacs as well. Both are very adequate for some serious editing.
Discuss! Reply!

Errgg Stupid
Written by tripwire on 2007-03-22 03:20:07
This is stupid, email packages exist to make life easier.Your argument for using VI for emails is like saying a IDE for a programming language sucks because you can do it in VI.Sure you can it takes 3X longer at least pluss testing items and packages etc. aren`t included.It`s like using a Sharp Stone Vs a knife to cut bread... lol
Discuss! Reply!

Vim as IDE
Written by Eelco on 2007-03-22 05:01:51
Quote:

This is stupid, email packages exist to make life easier.Your argument for using VI for emails is like saying a IDE for a programming language sucks because you can do it in VI.Sure you can it takes 3X longer at least pluss testing items and packages etc. aren`t included.It`s like using a Sharp Stone Vs a knife to cut bread... lol





Vim is very capable as IDE. Syntax highlighting, etc. comes in the standard package. I also use some plugins that give me the same functionality as the most used functions of my IDE, like compiling. For instance I use the free plugin Vim Intellisense, which makes it possible to use code completion. I can work a lot faster this way.
Discuss! Reply!

Delete to end of file
Written by Rob on 2007-03-22 11:40:10
dG

That's it. No mapping needed!
Discuss! Reply!

No mention of vim most important feature
Written by Shamar on 2007-03-23 02:20:01
Vim works even with low/crappy high-latency internet links when nothing else works, even using a mobile phone with poor coberture as link modem vim will do its job. That's really important for a system administrator.



Discuss! Reply!

It seems to run on EVERYTHING
Written by JonP on 2007-03-23 12:04:04
Oddly enough, I first learned vi on a VAX running a POSIX shell. When we switched over to HP and HP-UX, I didn't have to change anything. When the server crashed, I was able to use vi when all other editors refused to load. When I converted to Linux, vim gave me something that my hands knew, plus various bells and whistles. Do I need them? No. Are they handy? You bet.
Discuss! Reply!

still new but.....
Written by nephish on 2007-03-23 13:15:35
i have been using vim for about three weeks. i have worked in sys admin for three years and always wondered what all the hoopla was about with vim until i needed an editor to ssh a remote server. Man, it took me all of about the first 5 sections of the tutorial to learn why this was to become my favorite editor. I hate touching the stupid mouse now.
Discuss! Reply!

nvi and elvis
Written by Replaced on 2007-04-25 07:48:16
Quote:

Oddly enough, I first learned vi on a VAX running a POSIX shell. When we switched over to HP and HP-UX, I didn't have to change anything. When the server crashed, I was able to use vi when all other editors refused to load. When I converted to Linux, vim gave me something that my hands knew, plus various bells and whistles. Do I need them? No. Are they handy? You bet.





I don't know if you wanted to say that, but vim is a really bloated version of vi. I's large and have a lot of functions, which are useless for me (why the hell it has an internal filemanager?). I use nvi and sometimes elvis if I need syntax highlighting.
Discuss! Reply!

The Tutorial
Written by Ken on 2007-09-19 04:29:23
With a default install of Vim, the first file to load if you just use the command 'vim' is a tutorial. Follow the instructions there, and it will help you learn the program. :-)

Just make sure to save your work in a separate file. :)

Discuss! Reply!

autocompletion in vim
Written by linjava on 2008-03-05 11:18:50
If you want autocompletion you can use settings like the following in your .vimrc:

set tags=$HOME/jdk_tags,$HOME/proj_tags
set cpt=.,w,b,u,t,i,k
set dictionary=~/.vimKeywords

Vim is NOT for the vast majority of folks that want to just 'get up and running'. I'm a programmer and use both vim and eclipse (as a lot of the 'shops' I work at use this so I need to integrate with a team). Now that I have vim setup for autocompletion, and can hit CTRL-} and go to any source file in the jdk source (I'm using it for java) I loathe having to use eclipse which I experience a lot of stalls on. I do what I have to do to work with the team, but I much prefer vim. Yes, it takes some getting used to. However, I strongly urge vim newbies to got through the vimtutor tutorial which takes less than an hour and really is effective at getting you up to speed with vim fast. It will take a couple of months to feel 'comfortable' with vim, but if you plan on doing long term programming as a profession it may be a worthwhile endeavor. Of course emacs is another very useful alternative; and yes, you could be a great programmer and avoid both vim and emacs altogether especially if you come from the windows world. However, if you read articles from some of the world renowned hackers, you'll usually find that they use either vi[m] or emacs. Also, if you know how to type (which I do as you can tell from this obnoxious long comment), vim is simply much faster as you don't have to ever leave the keyboard!



Discuss! Reply!

filemanager not needed?!
Written by Peter on 2008-03-11 20:53:36
Quote:

(why the hell it has an internal filemanager?).





Well... maybe because I use it this filemanager so often? :)
To open some other file in the same folder as the one being edited, just type
:tabe `dirname %`
and select the one you want.
You won't need it unless you edit multiple files at once in some medium to large project :)
Discuss! Reply!

use the mouse != better
Written by blit on 2008-03-24 11:34:15
You can't imagine how much time is spent switching from keyboard to mouse, and to the movement keys. One can think 'no big deal, a few cm. won't harm' but they do. Lots of little pauses make a big difference at the end of the day. The more you type, the more you benefit from vi. As simple as that.
Of course, designers who do a little web programming with a visual IDE won't appreciate any positive difference. But a visual IDE can't replace having to type for any non-trivial program.
Discuss! Reply!