Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim split unless open

Tags:

vim

When using split windows in VIM, sometimes I create new split of a file already opened in another split (typically with plugins that open a unit test for given file in a new split).

Is there a way how to remap split command so that it checks whether the file is already opened before splitting, and if it is, give a focus on it?

like image 789
iNecas Avatar asked Jul 10 '11 07:07

iNecas


1 Answers

You can't remap the existing split command, as far as I know, but you can achieve the same same effect by writing a new function Split and then using a command-mode abbreviation (cabbrev).

Here's a function/mapping that should do what you want.

function! MySplit( fname )
    let bufnum=bufnr(expand(a:fname))
    let winnum=bufwinnr(bufnum)
    if winnum != -1
        " Jump to existing split
        exe winnum . "wincmd w"
    else
        " Make new split as usual
        exe "split " . a:fname
    endif
endfunction


command! -nargs=1 Split :call MySplit("<args>")
cabbrev split Split

Note that this will only "check" for existing splits in the current tab, and hidden buffers are ignored. (However, it shouldn't be too difficult to add more cases to enhance this functionality.)

like image 81
Prince Goulash Avatar answered Oct 03 '22 22:10

Prince Goulash