Switch between header and implementation files

2 Points

Lifepillar Lifepillar

6 years ago

A tags file must have been previously created with ctags passing --extra=+f.

The dictionary maps the suffix of the current file to a list of candidate suffixes for alternate files.

The snippet below supports C/C++, but you may extend it to other file types: just update the dictionary accordingly.

 nnoremap <leader>a :<c-u>tjump /^<c-r>=expand("%:t:r")<cr>\.\(<c-r>=join(get(
        \ {
        \ 'c':   ['h'],
        \ 'cpp': ['h','hpp'],
        \ 'h':   ['c','cpp'],
        \ 'hpp': ['cpp']
        \ },
        \  expand("%:e"), ['UNKNOWN EXTENSION']), '\\\|')<cr>\)$<cr>


Lifepillar 6 years ago

Tip: adding 'vim': ['vim']to the dictionary allows you to easily switch between the different .vim files of a given plugin (say, from plugin/foo.vim to autoload/foo.vim to syntax/foo.vim).