Wednesday, May 24, 2006

C++ :: load counterpart file

Ever wondered how to load counterpart file? IOW, when you have open header file - automatically open corresponding c file.

That's what I use:

map <silent> <F7> :exec ":e ".(expand("%") =~ ".h$"
\ ? glob(substitute(expand("%"), ".h$", ".cpp", ""))
\ : substitute(expand("%"), "\\.cpp$", ".h", ""))<CR>

Hanged on <F7>. First we look if it's header file open. If header, then we replace .h with .cpp and try to ":e"dit that file. If it's not header, we presume it's cpp file and try to replace .cpp with .h and ":e"dit that file.

P.S. <silent> in :map ensures that command wouldn't be displayed during its execution.

No comments: