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.


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.


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

  • 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? – rcv Jul 21 '11 at 17:17
  • 3
    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 clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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