Open source and header files at the same time

1 Point

Ferran Pelayo Monfort Ferran Pelayo Monfort

9 years ago

This command opens the selected source file and if there is a header for it, opens it. Also can work the other way around if you add more keys to the headers map

let g:headers = { 'c': 'h', 'cpp': 'h' }
function! OpenWithHeader(file)
    normal o
    exec "e ".a:file
    if has_key(g:headers, &ft)
        let header = expand("%:r").'.'.g:headers[&ft]
        if filereadable(header)
            exec "vs ".header
            normal h
        endif
    endif
endfunction
command! -nargs=1 -complete=file E call OpenWithHeader(<f-args>)

Rodney Fisk

Rodney Fisk 9 years ago

I'm unsure why the 'normal o' is in there. Why add a line to the current buffer? With line 2 commented, the function works perfectly. Thanks!

Ferran Pelayo Monfort

Ferran Pelayo Monfort 9 years ago

Woops it's due to the copy, it's mean to be "normal ^Wo" (where ^W is CTRL-V CTRL-W (in insert mode). The idea is to let that file (and it's header) be the only windows.

The same applies for the "normal h" in the inner most if block

Thanks for noticing!