Vim split if not open

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

Is there a way to reassign the split command so that it checks if the file is open before splitting, and if so, focus on it?

+6
source share
3 answers

You cannot reassign an existing split command as far as I know, but you can achieve the same effect by writing a new split function and then using the command mode abbreviation ( cabbrev ),

Here's a / mapping function 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 on the current tab, and hidden buffers are ignored. (However, it should not be too difficult to add more cases to improve this functionality.)

+9
source

An alternative would be to use :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. 

You can also use :sb to switch buffers. See vim: move to buffer?

For more information:

 :h :drop :h :sb :h 'swb' 
+3
source

This is one of the features of searchInRuntime .

:GSplit and :GVSplit can be renamed, they support the completion of the file name, and they will ask which file should be opened if they are somewhat consistent with the pattern in &path .

0
source

Source: https://habr.com/ru/post/892429/


All Articles