This article is part of the article series "Vim Plugins You Should Know About."
<- previous article next article ->
Vim Plugins, surround.vim

This is the fifth post in the article series "Vim Plugins You Should Know About". This time I am going to introduce you to a nifty plugin called "a.vim".

A.vim allows you to quickly switch between related source code files. For example, if you're programming in C, you can alternate between source.c and the corresponding header source.h by just typing :A.

It saves you only a few seconds every time you use it, but don't forget that these seconds can add up to hours during several weeks.

This plugin was written by Mike Sharpe.

For the introduction of this article series see part one - surround.vim.

Other bindings in a.vim

Besides the " :A " command that alternates between source files in the same buffer, a.vim also defines several other commands:

  • :AS -- alternate in a horizontal split,
  • :AV -- alternate in a vertical split, and
  • :AT -- alternate in a new tab.

The author of the plugin also defines the command " :IH ", which opens the file under cursor, but it's really unnecessary because "gf" already does that.

Extending a.vim

By default a.vim defines alternation for the following languages:

  • C -- .c <-> .h,
  • C++ -- .c / .cpp / .cxx / .cc <-> .h / .hpp,
  • lex and yacc -- .l / .lex / .lpp <-> .y / .ypp / .yacc,
  • ASP.NET -- .aspx <-> .aspx.cs / .aspx.vb

The alternation can be extended to other extensions by defining the following variable in your .vimrc:

let g:alternateExtensions_foo = "bar,baz"

This will set up alternation between .foo, .bar and .baz files.

How to install a.vim?

To get the latest version:

  • 1. Download a.vim.
  • 2. Put a.vim in ~/.vim/plugin (on Unix/Linux) or ~\vimfiles\plugin (on Windows).
  • 3. Download alternate.txt from the same page.
  • 4. Put alternate.txt in a.vim in ~/.vim/doc (on Unix/Linux) or ~/vimfiles/doc (on Windows).
  • 5. Run :helptags ~/.vim/doc (on Unix/Linux) or :helptags ~/vimfiles/doc (on Windows) to rebuild the tags file (so that you can read :help alternate.)
  • 6. Restart Vim.

I have mapped the :A command to " ,a ". You can also map it to the same combination by putting "map ,a :A<CR>" in your .vimrc file.

Have Fun!

Have fun with this plugin and until next time!

This article is part of the article series "Vim Plugins You Should Know About."
<- previous article next article ->


November 06, 2009, 00:00

:e file
will switch to another file, and
will switch back to the previous file, without any plugins. (that's even in vi, rather than just vim.)

spatz Permalink
November 06, 2009, 11:08

%:r is replaced with the currently edited filename minus the extension, so you can use :e %:r.h and :e %:r.c to switch quickly (use :tabe instead of :e to edit in a new tab). Of course those could be bound to keys or commands, but is quite short on its own.

November 07, 2009, 00:52

a.vim is really handy, I've been using it for years. I have also developed my own plugin for TDD, which you may find useful:

Passy Permalink
December 02, 2009, 21:47

I'm a python guy, so I don't really need this, but I really enjoy this series. Thanks a lot. Surround is awesome! (:

Krishna Permalink
December 09, 2009, 01:20

I use Increment.vim
a.vim (alternate)
doxygen.vim (for adding Doxygen comments for C++ programs)
ProjectExplorer (if working on Linux) - it doesn't work on windows :-(

pyrho Permalink
January 18, 2010, 16:50

I use :e %

Brent Permalink
March 07, 2010, 05:01

I was momentarily stymied by the install procedures until I determined a.vim should be put in the ~/vimfiles/plugin directory on windows. (The error I was getting was (E471: Argument required")

March 07, 2010, 08:38

Brent, oh, right. I have a mistake in the article. It's ~/vimfiles/plugin.

Tom Permalink
March 19, 2010, 09:14

What a useless plugin!

:e %<.h
:sp %<.h
:vsp %<.h

May 09, 2010, 14:26

Silly comments, this plugin *is* useful because:
- it switches back and forth between header/c file
- it does so automatically for several extension pairs (not only .c/.h), plus if you have for instance a C++ file, it looks for both a .h file and a .hpp file (or .hh, and all those conventions are indeed in use)
- plus, using the alternate file works only if you are editing two files at a time, which is *not* my case, I'm often navigating over big projects and my alternate file is rarely the current .h/.cc counterpart

So please commenters, while showing how part of these features can be accomplished without the plugin is a usefult contribution, don't bash it if you don't know what you are talking about :)

Great series btw.

Leave a new comment

(why do I need your e-mail?)

(Your twitter handle, if you have one.)

Type the word "disk_166": (just to make sure you're a human)

Please preview the comment before submitting to make sure it's OK.