I 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

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.

  • I added: 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.


"buffer only
nnoremap <leader>bo :call te#tools#buf_only('', '')<cr>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.