How I can make a window automatically visible on all tabs ? Like NerdTree-tabs plugin:
:tabs
Aba 1
NERD_tree_1
main.c
Aba 2
NERD_tree_1
Makefile
Aba 3
NERD_tree_1
> readme.md
Vi and Vim Stack Exchange is a question and answer site for people using the vi and Vim families of text editors. It only takes a minute to sign up.
Sign up to join this communityHow I can make a window automatically visible on all tabs ? Like NerdTree-tabs plugin:
:tabs
Aba 1
NERD_tree_1
main.c
Aba 2
NERD_tree_1
Makefile
Aba 3
NERD_tree_1
> readme.md
First, @romainl's comment is a really good advice: you'll learn a lot by reading a real plugin code and trying to understand its behavior.
Now if you're new to vimscript and coding, it can be pretty hard. So here is a solution. (It is probably more simple and maybe not as efficient as NERDTree way to do it but it should work properly)
Add this to your .vimrc
:
let g:windowToKeep=[]
nnoremap <F2> :call SetWindowToKeep()<CR>
nnoremap <F4> :call OpenTab()<CR>
function! SetWindowToKeep()
let g:windowToKeep = [expand("%:p"), winheight(0), winwidth(0)]
endfunction
function! OpenTab()
execute "tabedit " . g:windowToKeep[0]
execute "vsplit"
execute "resize " . g:windowToKeep[1]
execute "vertical resize " . g:windowToKeep[2]
endfunction
The idea behind this code is that "make a window visible on all tabs" is actually doing the following:
To do so, this code will create a variable g:windowToKeep
which will contain the window that you want to keep.
To set this variable a mapping is done on F2: you'll have to press this key to set the window to keep on your tabs.
Then you can call the function OpenTab()
either with ex mode or by pressing F4. This function creates a new tab with a buffer you want to keep, split the tab and resize the window containing your buffer.
Note 1 You can (of course) replace <F2>
and <F4>
by your favorite mappings.
Note 2 It is possible that the function isn't exactly what you expected because your workflow can be a little different than what I imagined while creating the code. As it's a pretty simple code you should be able to tweak it to fit your needs more closely.
g:windowToKee
, not g:bufferToKeep
.
Mar 19, 2016 at 19:09