Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm writing a library which is organized into two parallel and fairly deep src/ and include/ directories which contain my .cpp and .h files respectively. My favorite editor is vim, but I have yet to find a good way to quickly switch between source and header files quickly. For example, I may find myself editing


and decide I now need to make a small change to


Is there a convenient and ergonomic way to handle such switches quickly in (g)vim? My current approach is to keep two NERDTrees open, one for src/ and one for include/, though I feel like there must be a less cumbersome approach.

share|improve this question
up vote 1 down vote accepted

This function may do what you want. Put it in your ~/.vimrc or in a plugin. It assumes that all your .cpp and .h files are in parallel directories as in your example.

function! OpenOther()
    if expand("%:e") == "cpp"
        exe "split" fnameescape(expand("%:p:r:s?src?include?").".h")
    elseif expand("%:e") == "h"
        exe "split" fnameescape(expand("%:p:r:s?include?src?").".cpp")

nmap ,o :call OpenOther()<CR>

When you have either the .cpp or the .h file open, typing ,o will open the other in a split window.

share|improve this answer

The alternate plugin a.vim solves this. It's a must have when programming in C or in C++.

share|improve this answer
a.vim seems to only want to switch between source and header files in the same directory. How can I configure it to use my parallel source/include directory trees? – Boatzart Jul 21 '11 at 17:17
You have to set g:alternateSearchPath with the correct regexes. e.g. let g:alternateSearchPath = 'reg:#\<src\>$#include#,reg:#\<include\>$#src#' to switch between path/to/source/include and path/to/source/src – Luc Hermitte Jul 22 '11 at 12:06

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.