开发者

Vim split unless open

开发者 https://www.devze.com 2023-03-18 23:28 出处:网络
When using split windows in VIM, sometimes I cre开发者_开发百科ate 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).

When using split windows in VIM, sometimes I cre开发者_开发百科ate 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?


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.)


An alternative would be using :drop {file}.

Edit the first {file} in a window.
- If the file is already open in a window change to that
    window.
- If the file is not open in a window edit the file in the
    current window.  If the current buffer can't be abandoned,
    the window is split first.

Also using :sb to switch buffers might also be of use. See vim: move to buffer?

For more information:

:h :drop
:h :sb
:h 'swb'


That's one of the features of searchInRuntime.

:GSplit and :GVSplit can be renamed, they support filename completion, and they will ask which file to open when several match the pattern in &path.

0

精彩评论

暂无评论...
验证码 换一张
取 消