Welcome to Linux Forums! With a comprehensive Linux Forum, information on various types of Linux software and many Linux Reviews articles, we have all the knowledge you need a click away, or accessible via our knowledgeable members.
Find the answer to your Linux question:
Site Navigation
Linux Forums
Linux Articles
Product Showcase
Linux Downloads
Linux Hosting
Free Magazines
Job Board
IRC Chat
RSS Feeds
Free Publications


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!

 
Rate This Article: poor excellent
 
Comments about this article
What?
writen by: nigel on 2007-03-21 17:15:56
Type emails in vim? Get with the times, dude.
RE: What? written by nigel:
Don't judge something you don't know abo
writen by: Matthias Fassl on 2007-03-21 17:43:56
RE: Don't judge something you don't know abo written by Matthias Fassl:
macro not needed
writen by: ky13 on 2007-03-21 17:46:06
Pressing 'dG' will delete from the current line to the end of the file.
RE: macro not needed written by ky13:
more about 'd'
writen 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.
RE: more about 'd' written by ky13:
Some more tips...
writen 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
RE: Some more tips... written by Eric:
lol
writen by: Noobie Noobson on 2007-03-21 19:05:20
RE: lol written by Noobie Noobson:
Macros
writen 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.
RE: Macros written by Brian:
The Problem with VI(M)
writen 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).
RE: The Problem with VI(M) written by The problem with VI(M):
RE: The Problem with VI(M)
writen by: dragosis1 on 2010-11-22 16:13:38
No need to get hyper about editor wars...first of all I think it depends on what you use computers for. Also, as a general rule, if you didn't take the time to understand a tool (such as vi(m)), there is no reason to bash it. I have been using it for several years, and I would say an advanced editor is something you will never regret learning (at least for programmers). Sure, vim is strange in the beginning, and switching between normal/insert/visual modes may seem like an artificial tedium. But the point is that after some time all of this mechanics will be transferred into the so-called "finger memory", where things "just happen", without the need to think about low-level stuff. This process can take several months (faster for touch-typists - another highly-recommended skill). Of course, I am not saying vim is the only way to go. If emacs or joe works better for you go for it. But trying, for example, to write a non-trivial program in Notepad is like saying all doctors should perform surgery with a kitchen knife, just because it is a tool that can be easily understood :).
Reply to dragosis1:
Hah
writen by: Jesse on 2007-03-21 21:45:11
RE: Hah written by Jesse:
All it takes is some investment in learn
writen by: Bill-nz on 2007-03-21 22:15:45
RE: All it takes is some investment in learn written by Bill-nz:
good article
writen 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
RE: good article written by vim vini vinci:
YEH!!
writen by: JMC on 2007-03-21 23:19:20
Vim is teh suck for emailz. I use Microsoft's Outlook Express.
RE: YEH!! written by JMC:
Wow.. you OBVIOUSLY come from a differen
writen 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.
RE: Wow.. you OBVIOUSLY come from a differen written by JMC:
Vim not really appreciated until...
writen 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.
RE: Vim not really appreciated until... written by Jeff:
Favorit
writen 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.
RE: Favorit written by Eelco:
Errgg Stupid
writen 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
RE: Errgg Stupid written by tripwire:
Vim as IDE
writen by: Eelco on 2007-03-22 05:01:51
RE: Vim as IDE written by Eelco:
moron!
writen by: vimuser on 2007-03-22 09:35:07
moron!
RE: moron! written by vimuser:
Delete to end of file
writen by: Rob on 2007-03-22 11:40:10
dG That's it. No mapping needed!
RE: Delete to end of file written by Rob:
No mention of vim most important feature
writen 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.
RE: No mention of vim most important feature written by Shamar:
It seems to run on EVERYTHING
writen 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.
RE: It seems to run on EVERYTHING written by JonP:
still new but.....
writen 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.
RE: still new but..... written by nephish:
nvi and elvis
writen by: Replaced on 2007-04-25 07:48:16
RE: nvi and elvis written by Replaced:
Ha!
writen by: Anonymous on 2007-06-08 07:40:04
RE: Ha! written by Anonymous:
The Tutorial
writen 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. :)
RE: The Tutorial written by Ken:
autocompletion in vim
writen 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!
RE: autocompletion in vim written by linjava:
filemanager not needed?!
writen by: Peter on 2008-03-11 20:53:36
RE: filemanager not needed?! written by Peter:
Nice article about vim
writen 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
RE: Nice article about vim written by Srinivas Rao:
use the mouse != better
writen 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.
RE: use the mouse != better written by blit:
No war needed.
writen by: Leandro Mattioli on 2009-01-03 13:51:13
For developers: The main point is that, even though there are good editors out there, just a few allow the user to create new functionalities depending on its needs. I agree (partially) that VI(M) and EMACS weren't made to be simple to use and to learn, they were made to be complete (or at least, "completable"). If you take a look at the numerous plugins available, you'll probably get the idea. When you transform your VI(M) into a very good EDITOR, you'll realize that it is possible to improve more, reaching a complete ENVIRONMENT. This means that, in just a single application, you can edit your code, compile, debug, generate documentation, use e-mail, play simple console games, download/upload, use version control system, manage projects, and many other actions... And yes, when you're working as a developer, it is nice to send an e-mail without leaving VIM (actually, it would be nicer if VIM alone could give us water and bread, but I don't think this is going to happen!!!). However, non-developer users (and also many developers) may prefer other editors. We must respect the right to choose, and avoid the "religious wars" (linux vs. windows, vim vs. emacs, python vs. java, gnome vs. kde, etc.). That's all! Thanks everyone.
RE: No war needed. written by Leandro Mattioli:
Crazy
writen by: karr on 2009-01-21 09:16:49
why don't you all go back to the 50s and forget about the mouse. just don't use it at all, maybe you will feel better. i think that some developers become so obssesed in their minds so they can't imagine anything that will ease their lifes. how come its easier to know 20 different commands if you only want to delete different parts of a text than to simply use a click of the mouse. almost all the developer that i know don't use VIM at all and they never felt any need for weird editors like VI. they also never wasted days even weeks on learning how to use an editor instead on focusing on the developing tasks. a good tool is when u feel impressed when you first see it and you feel you wanna learn what it does and not when you feel discussed when you first use it and then you feel obliged to learn how to use it and how to do what you used to do effortlessly in another tool. stop loosing your time on what is not important. start focusing on real challenges, not on memorising thousands of commands that u can replace by a menu or a simple click... ligthen up
RE: Crazy written by karr:
Wake up and take the red pill, micro$hit junkies!
writen by: david_p on 2009-10-20 19:00:17
I've been using Vim for a about 3 years and everyday i learn some incredible feature. It's just a world apart from anything else. No dum notepad or ultraedit can even compare (and i've used both and many more).
I guess it has to do with the way you think about editing text. Either in a logic and efficient way, or like a stupid chicken pressing those arrow keys time after time after time after time...
I understand the argument that people don't see the need for vim, because once i was like that. It was not until i got a chance to really work on unix boxes on a day by day basis that i started to appreciate the beauty of it. I can only feel sorry for those who never had the chance that i had and keep swimming happy and ignorant in the microshit bloated sea. Like anything in life, different experiences contribute to a richer vision of things. Saying that you prefer to use menus and the mouse to edit text reminds of the ostricht that sticks her head in the sand to ignore what goes on around her. Try looking for them when you're editing a python script, via ssh on a solaris box. Oh, but you don't use ssh or python right?...(but you do use ssl don't you?).
And for your information you don't have to memorize commands in vim. All you have to do is use your brain and know how to use the help system. Most commands can be combined logically to perform most tasks in an efficient way. cw=change word dw=delete word 2dw=delete 2 words...get the ideia?...
I prefer to type 13dd to delete 13 rows than to hammer that poor down arrow 13 times and finally pressing delete, but it's your choice...
Don't feel bad, just take the blue pill and everything will be allright again...
Ignorance is a bliss.......






RE: Wake up and take the red pill, micro$hit junkies! written by david_p:

Comment title: * please do not put your response text here