Vim made easy: how to get your favorite IDE features in Vim

The open source Vim text editor offers a significant amount of power and …

The Vim text editor is a favorite of many programmers and system administrators. Although it provides an exceptional amount of power and flexibility, it can be somewhat intimidating for new users. Programmers who transition to Vim from a conventional integrated development environment (IDE) tend to find the paradigm shift a bit disorienting.

I often get e-mail from readers who are looking for ways to make Vim more developer-friendly. A common complaint is that the editor doesn't come with standard IDE features out of the box and it's not always clear how to configure it to provide equivalent functionality. The secret to unlocking Vim's real magic is to take advantage of its powerful plugin system and the large ecosystem of third-party scripts that offer editor augmentations for various tasks. To get you started, I have compiled a short list of useful tips and programming plugins for Vim that will make IDE refugees feel right at home.

Although Vim is largely designed around text-based interface idioms and is most effective when used accordingly, there are graphical shells available that make the program a bit more manageable for new users. Instead of running Vim in a terminal window, you might want to try GVim, the basic GUI version. It has configurable menus and toolbars that make the program's most essential features easily accessible with a mouse. It also lets you use native file dialogs and gives you the ability to resize buffer panes by clicking and dragging. GVim is available for both Linux and Windows. The equivalent for Mac OS X is MacVim, which provides a native Cocoa user interface, including menu integration.

GVim, with the default menu and toolbar configuration

The most frequent complaint that I see from new Vim users is that the buffer list is somewhat cumbersome and there is no easy way to see at a glance what files are open. There are several plugins that can address this issue by providing a special pane with a buffer list that can be used to easily navigate between open files. My favorite is MiniBufExplorer, which displays a list in a pane at the top of the window. When the MiniBufExplorer list is active, you can hit the tab key to rotate through items and you can select one by hitting enter or double-clicking.

MiniBufExplorer plugin

Many IDEs have a code navigation sidebar that displays the structure of your programming project and allows you to jump to a specific class or method by clicking. You can get this feature in Vim by using the popular Tag List plugin. The plugin requires the Exuberant Ctags utility, which it will use to analyze your code. The plugin's sidebar, which can be activated with the :Tlist command, will show you the classes and methods in the active buffer. As you open additional files and navigate to other buffers, they will be added to the list. In GVim, you can click a method name to jump to it. To do it with the keyboard, move the text cursor to the name of the method that you want to jump to and then hit enter.

TagList plugin demonstrated with a Python program

Automatic text completion is another common IDE feature that users want to bring with them to Vim. Support for this feature was introduced in Vim 7, via the Omnicompletion system. It's programmable, which means that it can be customized to work with various programming languages. There are even completion modules available for dynamic languages, such as Python and Ruby. The proper completion configurations are generally shipped as part of the default Vim package now, so you don't even have to do anything special to make it work. To invoke the completion menu, all you have to do is hit ctrl+x followed by ctrl+o. Then you can use ctrl+n and ctrl+p to navigate through the list of possible completions. As you rotate through the completion options, Vim will will show you contextual help information with method signatures and properties in a separate scratch buffer.

Vim's omnicompletion feature demonstrated with Python and the GTK+ library

There are many other ways that you can improve your Vim experience. The Vim wiki and script repository have a very extensive collection of third-party enhancements that you can use to add additional features. There are plugins that implement snippet systems, outlining tools, project management utilities, and a large number of other features. There are also many scripts that provide specialized enhancements for particular programming languages or frameworks. For example, there is an extremely popular script that will give you improved syntax highlighting and convenient navigation features for Ruby on Rails development.

There are also some script collections for the complete beginner that will make Vim behave almost exactly like a conventional text editor with standard keyboard shortcuts and simpler menus. You can use those to get you started if you are uncomfortable with Vim's cryptic keyboard commands.

The mutability of Vim allows it to be whatever the user desires. For those who don't have the time, expertise, or inclination to build the perfect Vim configuration by hand, the endless number of third-party scripts and plugins can get you most of the way there with very little effort.

Sweet jesus. So when I read that plugins were available for vim I thought, "Hell yeah! I'll get all kinds of features and really rice out my install." Then I went through some of the motions to try and install these plugins. Yeah I think I'll pass.

Originally posted by Ancalagon:If you want the features of a modern IDE in your vim, why dont you, I dont know, use a modern IDE?

Some people are adverse to the GUI. Scares 'em away with all those intimidating buttons. Much better to memorize 9-finger key commands. :-)

On a more serious note... doing modern development in a command-line editor is rather painful, but, opinion and assholes and all that. We all have one. On the flip side, vim is pretty handy when ssh'd to points elsewhere.

I'm not sure I remember all of the VI commands I used to know but I do remember it was the bees knees when it came to doing search and replace and having multiple cut and paste buffers. What could be more natural than keying in ":s/^find/replace/g"

It was also super speedy, something that could never be said about emacs.

VIM on the command line was close enough to VI I never had to worry about the differences and it was a nice confortable tool to use. VIM in a GUI - my only question would be why? There are certainly better editor on Windows and MAC OS.

Originally posted by Ancalagon:If you want the features of a modern IDE in your vim, why dont you, I dont know, use a modern IDE?

Because a modern IDE usually has you using the mouse. As soon as you touch the mouse, you are less efficient =)

I think being able to do everything with the keyboard makes you a lot more efficient once you learn vim or emacs. You will type far less, move around far quicker, and just be a lot *faster* than you would be otherwise. I don't know about your modern IDE, but I don't think it lets you do even a fraction of the things you can do with vim (such as batching regular expressions).

The learning curve is kinda steep... but it helps if you treat it like learning anything else and learn a little bit each day. GVim definitely eases the pain, but encourages bad habits like using the mouse too (which I think is forgivable for using the menus at the top, but not so much for editing).

Originally posted by Ancalagon:If you want the features of a modern IDE in your vim, why dont you, I dont know, use a modern IDE?

Vim with plugins is a LOT less than the 800mb of Ram usage eclipse used to eat up on a good day. Buffers > tabs any day of the week, and once you get used to the movement/shortcuts it's so much nicer to have the full window for code and move around so much faster/easier.

I spent 5 years using Zend Studio/Eclipse/Wing IDE, etc. finally combined them all under vim last June and not looked back.

Having recently become much more proficient with the command-line environment, (now almost daily writing my own scripts,) I'll vouch for its continued relevance, and charming simplicity. I like the fact that almost everything I want to do can be done, almost without a thought, through muscle memory, once I do it 2 or 3 times. It is much less cluttered than having a jillion applications with a jillion different windows open, because otherwise, I'd have like 25 windows open just in the text editor, not to mention other things.

I will profess a taste for emacs over vi, just because emacs, though archaic-seeming, seems well-organized given the limitations of a terminal window, while vi always seemed like "hell, throw in something else to the command buffer." It seems to pile on the arcana for 80s-nostalgia sake. No thanks.

And I use the GUI version of neither. They're in a CLI to cut down on the clutter and the visual overhead, or to save the visual overhead for things that just don't flow in the terminal (a LOT of things!)

EDIT: And I definitely disagree with the notion that the keyboard is faster for everything, or even most things, or that "you just have to get more familiar with it." It's definitely faster for some things, but the mouse isn't just around for the loser idiots—it actually has a purpose, and fills that purpose quite well. I like the power and flexibility that I'm finding in the CLI, but it's obvious that some things could be made easier if using the mouse were an option. (I like the alt-key navigability of Windows programs, and I'm a mac guy.)

Diff between Vim and Emacs?It all depends on how you want to interact with the file you're editing. The different interfaces (commands) make sense to different people. I agree with Zhengyi - Vim is ESC and :command while Emacs is CTRL, ALT, SHIFT twister.

In Emacs, the cursor is live and you simply start typing to edit - just like MS Word. In Vim, the default is simply viewing the file and you enter a command to start editing. Press "i" to start inserting text before the cursor or "a" to append text after the cursor. Press "d" to start deleting. "u" for undo, CTRL-r to redo, and so on. I find those commands far easier to remember and execute than the finger twister CTRL sequences in Emacs.

Viewing your a list of your open files (buffers) in Vim seems quite easy to me, at least for my GVim install in IBM AIX (Vim v7.0). ":ls" shows me the buffers - just like "ls" in a shell/term window to list files. Then if I want the file in buffer 3, I just type ":b3".

I tend to use Emacs for development and vim for basic editing. They both can do the same things, but I found emacs easier to set up for cpp development than vim. My coworker's machine that I need to ssh into frequently has vim set up for development, so I use vim then. They both require several hours of using a cheat sheet before you can become faster with them than with an IDE. VIM would work better with the escape key on the home row and Emacs would work better with the mode keys on the home row. If you could hold down combinations of home row keys to set the context then they would be faster, but I don't think that keyboards allow that.

Understatement of the year. You shouldn't have to do Google searches to figure out how to get out of your text editor. Unfortunately I'm terrible at memorizing 10,000 obscure key presses to do everything under the sun so I have to use other editors. I can see the speed advantages though for those that don't have an issue with such memorization.

Understatement of the year. You shouldn't have to do Google searches to figure out how to get out of your text editor.

Ha! A popular code editor on VMS was "ls" and Unix-using morons like me would telnet into a VMS machine, type "ls" to list our files, and be stuck forever in this stupid editor. To this day I still have no idea how to exit that editor, other than killing the terminal session.

Originally posted by Trout Mask Replica:On a more serious note... doing modern development in a command-line editor is rather painful, but, opinion and assholes and all that. We all have one. On the flip side, vim is pretty handy when ssh'd to points elsewhere.

All from ssh, IMO. You can end up using vi that way a lot when it's just a quick little change or your current 'net connection makes remote desktop painful or whatever. Spend too much time doing this and you learn the keystrokes - then it's hard to use a "normal" editor without hammering on escape and leaving stray Js and Ks and colons all over the place. That's been my experience, anyway.

I do dream of the day someone manages to shoehorn vim into Visual Studio as the text editor without breaking intellisense. I'd be pretty damn happy with that as a setup.

Originally posted by Ancalagon:If you want the features of a modern IDE in your vim, why dont you, I dont know, use a modern IDE?

Some people are adverse to the GUI. Scares 'em away with all those intimidating buttons. Much better to memorize 9-finger key commands. :-)

On a more serious note... doing modern development in a command-line editor is rather painful, but, opinion and assholes and all that. We all have one. On the flip side, vim is pretty handy when ssh'd to points elsewhere.

It also keeps people from touching your computer, I find.

I work on Windows, and I much prefer using GVim on Win32 than the assinine editor that comes with Visual Studio. (Alas, as of Visual Studio.net, they broke VisVim which opened files in Vim instead of Visual Studio's editor).

It's a choice - I know IBM has a really nice tutorial on vi/vim, and Vim has its own built-in tutorial.

And I like Vim's keystrokes - a lot better than the ctrl-alt-shift-meta-bucky-cokebottle combinations you get with Emacs. Selecting text, then reformatting it or adding characters and transformations is just a few keys away. And there's probably a trillion keystrokes I can use to do what I want, but combining the basic ones you need I find gets you 99% of what you want.

My favorite was re-learning how to make use of "i" - do it right, and you can easily transform a prototype into a function body automatically by using "." to repeat the insertion. Complete with your favorite spacing and indentation.

It's a personal choice - a lot of people like the simplicity of Notepad like editors in most IDEs, others like being able to navigate around the file without touching the mouse. Fun also doing big block deletions at times - you see people selecting it per-block and backspacing to remove the block, while I just search for the separator and "dn" to delete to next selection.

If it wasn't for GVim for Windows, I really won't have one editor across every platform...

Gotcha. I favor being 1337 in my gaming - not the kind of 1337 that hurts my productivity from using antiquated or hard-to-use tools from the Cretaceous period of computing.

Horsepooie.

Vim is a highly evolved and efficient tool. Very fast*, very good at what it's designed for. If you can't wrap your head around the concept of a dual mode editor and that 'j' means 'down' and 'k' means 'up' then that is your problem.

* fast as in 'the speed at which works gets done', not 'how long it takes to refresh the screen'.

If you have a complex script on a remote machine you need to edit and you want to get it done _now_ then Vim can deliver a highly productive and all the immediate text-editing features you'd want on the fly. No setting up projects, no importing code, no integrating SCMS, no nothing.

ssh-in, edit, test it, exit, done.

In the time it takes for a person to navigate around and get a IDE started up and the code in question loaded... I am already done and heading down to the break room to get a bag of Cool Ranch Duritos and a Diet Coke.

---------------

You have to understand the target audiance is system administrators and scripters. Sure there are a lot of programmers that use it for complex coding, but the majority of people are people that simply want to get things done and don't have projects that run on for weeks at a time. Features that don't require a lot of setup, like language-sensitive syntax highlighting and autocompletion are the most valuable.

For programmers who have long-term projects they are working on then IDEs or Emacs are preferable. Those require lots of setup and time to get going and offer the most features for managing code and documentation.

And Emacs isn't just a text editor.. it's probably the first IDE that ever got very popular. Sure it pretends to be a text editor, but it's about as useful for system administration and scripting as notepad is without the devoting a great deal of time customizing it to their specific needs. In fact the only way it's very useful is if you run Emacs in 'server-mode' so that when you edit files they are loaded up into a existing program with whatever else your working on.

edit:

Oh, if your editor requires me to use either:A) the mouseorB) the arrow keys

For _anything_. Then I am not going to touch it with a twenty foot pole.

For all those wondering about why people use Vi or VIM or Emacs rather than a full IDE in X, there is one simple reason. When you are on a box with almost nothing else, you will usually have Vi. It is pretty much a universal standard on any Unix like machine and it may be the only tool you have to edit a file. At my university, learning Vi and bash is the first step in the system administration sequence and is a fundamental skill for the major.

Originally posted by lamestlamer:I tend to use Emacs for development and vim for basic editing. They both can do the same things, but I found emacs easier to set up for cpp development than vim. My coworker's machine that I need to ssh into frequently has vim set up for development, so I use vim then. They both require several hours of using a cheat sheet before you can become faster with them than with an IDE. VIM would work better with the escape key on the home row and Emacs would work better with the mode keys on the home row. If you could hold down combinations of home row keys to set the context then they would be faster, but I don't think that keyboards allow that.

If you ever wanted to undertake the Herculean task that is understanding XKB, you can make the keyboard act however you want (though, not per-program). You want to exchange Left Control and Caps Lock? There's a preset for that. Want to do the same for Escape and Caps Lock? Completely doable. There isn't any official documentation worth a damn, but there is the nice An Unreliable Guide to XKB Configuration. Of course, this only works in X.

quote:

Originally posted by Pamel:

quote:

Originally posted by TechGeek:When you are on a box with almost nothing else, you will usually have Vi.

Yeah, but having Nano installed is even more common, and infinitely more user friendly.

I told somebody a couple of days ago that I wish he died and went to hell, where his GUI-loving ass would be forced to use ed for the rest of eternity.