1

I am trying to write a function to cause vim to open the relative header/source file in a split window.

What I have works (see below) apart from the file it opens in the split doesn't have syntax highlighting.

function! SplitOther()
    let s:fname = expand("%:p:r")

    if expand("%:e") == "h"
        set splitright
        exe "vsplit" fnameescape(s:fname . ".cpp")
    elseif expand("%:e") == "cpp"
        set nosplitright
        exe "vsplit" fnameescape(s:fname . ".h")
    endif
endfunction

autocmd! BufReadPost *.h,*.cpp call SplitOther()

I have tried appending syntax on to the command (just before the endfunction) but that doesn't seem to want to work.

I think it happens when the file isn't in a buffer before splitting? I'm not 100% sure though.

Edit

I change my function to have allow the definition of file pairs, I'm not sure if it will change my question at all so here's the extended version:

function! SplitOther()
    let s:pairs = [ [ "h", "cpp" ], [ "vert", "frag" ] ]
    let s:fname = expand("%:p:r")

    for [s:left, s:right] in s:pairs
        if expand("%:e") == s:left
            set splitright
            exe "vsplit" fnameescape(s:fname . "." . s:right)
        elseif expand("%:e") == s:right
            set nosplitright
            exe "vsplit" fnameescape(s:fname . "." . s:left)
        endif
    endfor
endfunction

autocmd! BufReadPost * call SplitOther()
Community
  • 1
  • 1
Lerp
  • 2,957
  • 3
  • 24
  • 43
  • It's working without `autocmd!`, i.e. using a map or something. Does it have to be automatic? I use `le` for this. – timss May 15 '13 at 13:57
  • The automation was more to make life easier – Lerp May 15 '13 at 13:59
  • Aye, I can understand that, although I prefer to not have something like this being forced automatically, thus using an easy bind instead. Anyway, there's nothing wrong with your function, it's because of `autocmd`. – timss May 15 '13 at 14:01
  • I suspect I'm using the wrong event or something, I'll experiement – Lerp May 15 '13 at 14:02
  • You could take a look at [this related question](http://stackoverflow.com/q/10513583/1076493). I didn't get it to work with this use of `autocmd`, but it might give you an idea. – timss May 15 '13 at 14:05
  • If I do `:windo filetype detect` on a file pair opened by this command it fixes it so I tried adding `exe "window" "filetype detect"` at the end of the function but that doesn't work?? – Lerp May 15 '13 at 14:39
  • I don't know about automation, but you may want to look here about switching to header files: http://vim.wikia.com/wiki/Easily_switch_between_source_and_header_file – Peter Rincker May 15 '13 at 15:19

2 Answers2

2

Got it!

When the file was being loaded into the vsplit it's filetype wasn't being set.

I noted that when vsplit is called the new split grabs focus and that is the window that does not have syntax highlighting so you can simply add exe "filetype" "detect" at the end of the function to tell vim to detect the filetype of the current window.

The result:

function! SplitOther()
    let s:pairs = [ [ "h", "cpp" ], [ "vert", "frag" ] ]
    let s:fname = expand("%:p:r")

    for [s:left, s:right] in s:pairs
        if expand("%:e") == s:left
            set splitright
            exe "vsplit" fnameescape(s:fname . "." . s:right) 
            break
        elseif expand("%:e") == s:right
            set nosplitright
            exe "vsplit" fnameescape(s:fname . "." . s:left)
            break
        endif
    endfor

    exe "filetype" "detect"
endfunction 

autocmd! BufRead * call SplitOther()
Lerp
  • 2,957
  • 3
  • 24
  • 43
1

The problem is that filetype detection is triggered by an autocmd, but by default, autocommands do not nest (cp. :help autocmd-nested).

Also, by using :autocmd! with a bang, you're clearing all other such global autocmds; this might affect other customizations or plugins! You should define your own scope via :augroup, then it's safe. Taken together:

augroup MyAltSplitPlugin
    autocmd! BufReadPost * nested call SplitOther()
augroup END
Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324
  • I do have the autocommand in a group I just had quite a few commands in that augroup so I omitted it. I'll read up on the nested, thanks! – Lerp May 16 '13 at 10:48