I know about :only
and Ctrl+wo.
But I want to keep NERDTree window untouched. Is there a way to achieve it?
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 communityI know about :only
and Ctrl+wo.
But I want to keep NERDTree window untouched. Is there a way to achieve it?
This solution only works with vim 8+
You can add this to your .vimrc
:
function! OnlyAndNerdtree()
let currentWindowID = win_getid()
windo if win_getid() != currentWindowID && &filetype != 'nerdtree' | close | endif
endfunction
command! Only call OnlyAndNerdtree()
The function gets the id of the current window with win_getid()
then it uses windo
do iterate through all of the windows and execute code following it.
The code test if the id of the window is different than the id of the current windo and if the filetype of its buffer is different than nerdtree
. If both of the conditions are met, the window is closed.
And the last line creates the command :Only
which calls the function.
Note win_getid()
was only introduced in Vim 8, with previous versions of Vim a workaround must be possible wit winnr()
but it would probably not be as easy as with win_getid()
especially because when a window is closed, the number of the other windows are updated.
call win_gotoid(currentWindowID)
under the windo
line to make sure that the intended window kept focus. I also added an extra check for the tagbar plugin window after the nerdtree check which is configured to be the rightmost window which may be why I needed that.
– jriggins
Dec 23 '17 at 5:54
buffer only.
https://github.com/tracyone/t-vim/blob/master/autoload/te/tools.vim#L70-L106
"buffer only
nnoremap <leader>bo :call te#tools#buf_only('', '')<cr>