If you’re a C++ developer, you’ll spend a lot of time toggling back and forth between your header (
.h) files and the corresponding implementation (
.cpp) files. In object-oriented programming, classes will typically map one-to-one with the files — Widget objects will typically be split into
Widget.h holding declarations and
Widget.cpp holding the actual implementations. On the other hand, when you write templated code, it needs to go in the header file. So although the code is split up between
.cpp, they really should be viewed as a single unit. Typically, I’ll be coding in vim with multiple tabs, each tab holding a .h/.cpp pair split like so:
Opening files can be a pain, especially when your code is tucked away in nested directory structures. You potentially waste a lot of time typing in the filename you want to open. It’s not so bad if you keep your .h and .cpp files together. In this case, you can use the a.vim (version 2.18 at the time of this writing) plugin. Open up the header file. Then, a simple
:AV will open up the corresponding .cpp file in a new split pane window.
It turns out
a.vim searches a handful of locations relative to the file from which you are toggling for the corresponding alternate file. For example, I’m in
Widget.h, and to toggle to the .cpp file,
a.vim will look in the current directory as well as
../src (it’s typical for some projects that build libraries to have separate
If your project’s directory structure is a little more complicated (as in the screenshot above where you have to go between
include/CGAL/Qt), you can add a new relative path to search for the alternate file. Put the following in either your user
.vimrc or a project-specific
let g:alternateSearchPath = 'sfr:../../../src/CGAL_Qt4,sfr:../../include/CGAL/Qt'
Note that the search paths start with
sfr: and are comma-separated.
Actually, setting your own
g:alternateSearchPath will blow away the defaults, but you can patch
a.vim to just append to the default search paths in a way you usually do with the
--- a.vim 2013-12-23 13:10:42.458540027 -0800 +++ a.vim.new 2013-12-23 13:10:59.270540049 -0800 @@ -108,6 +108,8 @@ " a path in their [._]vimrc. if (!exists('g:alternateSearchPath')) let g:alternateSearchPath = 'sfr:../source,sfr:../src,sfr:../include,sfr:../inc' +else + let g:alternateSearchPath = g:alternateSearchPath . 'sfr:../source,sfr:../src,sfr:../include,sfr:../inc' endif " If this variable is true then a.vim will not alternate to a file/buffer which
Note the dot operator in this vim configuration language is used for string concatenation.