Vim helper script

I decided to post this after reading Adriaan’s blog on licensing.

My editor of choice is vim. It has a steep learning curve, but once passed it allows speedy editing. It has more features than I have any hope of discovering, let alone mastering. I’ve more than once wished I had a simple way of doing something, then accidentally stumbled across just such a feature in vim.

All this is besides the point, however. I’m not here to convince you to use vim if you are currently happy with Kate or Emacs (or any of the dozens of other editors out there). For those that do use vim, however, kdesdk has a very handy vim script: kde-devel-vim.vim. Simply copy it (or create a link) from kdesdk/scripts/kde-devel-vim.vim to ~/.vim/plugin. Or put a line like source ~/src/KDE/kdesdk/scripts/kde-devel-vim.vim in to your ~/.vimrc.

After this you get several neat features. One of them is that spaces at the end of lines, and all tabs, are marked with a dot, so you can see excess whitespace or tabs where there should be spaces (if you are following the KDE coding style). It has a few helper functions that automatically adjust spaces around parentheses and insert braces in the correct place depending on the coding style of the module you are working on.

All this is very useful, but why did Adriaan’s post trigger this one? Well, I discovered a few more useful features it provides recently. If you type ,i, for example, it inserts include guards in the file you are editing based on the file name. ,h will switch between the header and the main cpp file, and ,p will do the same for the private header and the implementation file. And, incredibly usefully, there are similar keysequences that insert license headers.

You need to do a little bit of set-up for this. You need to create some files in your home directory. GPLHEADER should contain the text to insert for a GPL header. Such as the following:

/*
 * Copyright 2008  Name <email@domain.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA  02110-1301  USA
 */

This will be inserted at the top of the file when you type the sequence ,lg. Likewise ,ll will insert the contents of ~/LGPLHEADER, and ,lm will insert ~/MITHEADER. The script is trivial to extend to do the same for BSD headers, for instance.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: