Of course, we wouldn't have had it any other way: we wanted to be fair, as pledged, so here is the vim article, which is a counterpart of our last one on how to make your editor the perfect programming environment. So you must have the following profile for this article to be really useful to you: you know your way around programming, so you subsequently know what you would like in an editor, and you also know your way around vim, preferably more than what we talked about in the article dedicated to it. If you read the customizing emacs article, you already have a good idea on how this article is going to be structured. If you were directed here from somewhere else, here's what we're gonna do: we'll take some popular programming language (space permitting) and show you how to tweak vim so it will became more fit for coding in that language.
Although vim is written entirely in C, there is something named vimscript that makes creating/editing settings, sort of like Elisp in emacs, although this is a loose comparison. Please remember that whatever will be talked about here is only about vim. Not BSD vi, not some vi extension for another editor, just vim. That is because although you can learn the basics on, say, nvi, the things that interest us (since you already know the basics) will only work on vim. Of course, some recent version, not older than 7.3.x. Many things will probably work on 7.x or maybe even 6.x, but there's no guarantee.
Just as before, a little advice: although this is influenced by personal preference, experience says it works; namely, install scripts/addons/color schemes directly from the source, regardless if your distro offers it as well. That's because many maintainers tend to package stuff with respect to their personal preference, which might or might not be in concordance with yours. Installing such addons is as simple as copying a file to a location, nothing more. And, for your convenience, we'll tell you how to install via your package manager anyway.
The distributions I have available to me at this point are Debian, Fedora, Gentoo and Arch. I will do a search for the 'vim' keyword on each of them and give you some tips and pointers on what you can install, then we'll go language-specific.
Generic add-ons on Linux
I will start with my Debian testing installation, which lists a few addons for various languages, including Perl, TeX, Lua, Python or Ruby. There is also an addon-manager that was created to help you ... manage the vim addons. Install what you think you need, depending on what you're working on, noting that in Debian, the syntax for such packages' names are something like "vim-$language". You can also install "vim-scripts", which packages the useful scripts provided by vim in the source tree in the runtime/ directory. These files have the .vim extension and are written in Vimscript. More on this later.
If you're using Arch, make sure you have the community repository enabled. If not, edit /etc/pacman.conf and uncomment it, then update with -Syu. The reason I'm actually suggesting this is because it seems there are a lot of interesting tools here related to vim. Some recommendations would be: vim-plugins (searching for it with pacman will list the individual contents), vim-align, vim-pastie, vim-supertab or vim-timestamp.
In Fedora's repositories (the version used is 17) offer some interesting add-ons. Some of those would be fluxbox-vim-syntax, for the Fluxbox configuration files, glusterfs-vim, vim-clustershell, vim-nerdtree or uzbl-vim. As I could understand it, improved functionality in binary form is named in the form "vim-", while syntax files are named inversely, like "-vim".
Gentoo is known, alongside Debian, to have wealthy repositories. This is to be noticed in our search and here are some interesting packages, depending on your preferences: vim-r (for R development), vimpress for managing Wordpress blogs or zencoding-vim for HTML/CSS developers. Of course, I didn't repeat the plugins that I've already described before, so don't think these are the only three addons in Gentoo. All in all, it becomes obvious that you can, just as you can with emacs, make vim an IDE and the only tool to use when programming.
I don't want to repeat the idea that I outlined last time: it all depends on the coding style you're following, whether you chose it or you had to. By now you know that vim colon commands start with a
: while in command mode, and settings reside in ~/.vimrc and can be added there by using the exact same syntax, minus the colon. So by example, if I want to temporarily set tabstop at eight spaces, I can issue
:set tabstop=8, or add this setting permanently to ~/.vimrc thusly:
echo "set tabstop=8" >> ~/.vimrc
Generally, vi(m) users like it simple. This is why colon commands are simpler, and so is using syntax highlighting. vim already has this facility for C/C++ developers, you only need to tell it about it. This is again a reflection of the Unix philosophy: you have lots of facilities, but you need to explicitly enable them. Use
syntax enable to enable syntax highlighting, or
syntax on if you want to keep your color settings. More on this and how to write your own syntax file can be found easily. Coming back a little to spacing and indentation, it's best you know about the vim wiki, which, just as the emacs equivalent, has lots of information. And I mean lots.
There is a difference between tabstop and shiftwidth: while tabstop sets the number of spaces inserted when you press the Tab key, shiftwidth sets the number of spaces to be inserted when indenting code. Also, if you want vim to insert x spaces when Tab is pressed, set expandtab, else use noexpandtab. Comments are introduced thusly:
set shiftwidth=8 "sets shift width at eight chars "and this is a comment on a line by itself
Just like in emacs, if your coding style is (or has to be) one that inserts the opening brace after the if or while or whatever, you need not alter anything.
We've found some SQL syntax file, but it seems to be focused on Oracle 9i. You can install it by just copying in ~/.vim/syntax . It's a good idea to browse the scripts section of the vim site, because you can always find a new color theme that you might like or a script you might find useful. A little search on said page got me lots of SQL results, some of them even supporting more dialects of SQL, like Sybase or MS. Since it's SQL, you can use those to write code for other RDBMS, as long as said code doesn't have some specific constructs that the plugin doesn't understand. A SQL script more oriented to the OSS database solutions can be downloaded from the scripts site as well.
I wanted to see if even more lesser-known/used languages have a corresponding script on the vim website, so I searched for Coldfusion, Cobol and Ada. That's not to bash those languages, but they're not the most popular ones, for certain. Guess what? I found a syntax script for each and every one of them. This is why I thought that continuing with other languages would be superfluous: it's all about downloading a .vim file to a location that might not be ~/vim/scripts, as some suggest ~/.vim/ftplugin or ~/.vim/indent/, and start coding. By the way, every plugin's page has a section where it tells you where to put the file in order to use it. But the location isn't mandatory, it's given by the purpose of the file, e.g. indentation vs syntax. Remember to have
filetype plugin on in your ~/.vimrc so that the editor loads the syntax file depending on the file extension (.c, .php, .html and so on). To see in what directories vim looks for plugins, type
:set runtimepath?, and if you want to add a new location, use the += operator.
This is a rule that seems obvious, but I will say it anyway: if you don't have a directory in ~/.vim that's necessary to install a .vim file in, just create it. So, create a colors directory in ~/.vim, download your preferred color schemes, noting that most of them have screenshots, and type
:colosrcheme name.vim. It's up to you to figure out how to get vim to use different schemes for different languages. If you chose a theme, say, named "kernel", add this to your ~/.vimrc:
colorscheme kernel. You don't need to use the .vim suffix.
Although different by design and philosophy, the two major editors that Gnu/Linux users have at their disposal make for customizable environments for many different tasks, besides programming. You can create your own syntax files not only for programming languages, but for configuration files as well, although that would be more of an exercise than a practical issue.