3

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

~/workspace/myproject/include/myproject/core/utils/coolutil.h

and decide I now need to make a small change to

~/workspace/myproject/src/myproject/core/utils/coolutil.cpp.

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.

1

2 Answers 2

1

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")
    endif
endfunction

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.

3

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

2
  • 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, 2011 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 Jul 22, 2011 at 12:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .