I would like to have the behavior where I can run :Vex<CR> when pressing <Tab> in normal mode:

nnoremap <Tab> :Vex<CR>

... to open the netrw file tree window and then I would like to close this window when I press <Tab> again in any other window in this tab. Is there some way how I can get the window ID when opening the netrw window and then close it by its ID in the same tab?

  • 1
    Don’t map <Tab>! It’s the same keycode as <C-I>, which by default is a useful navigation command
    – D. Ben Knoble
    Commented Mar 18, 2020 at 15:25

2 Answers 2



Lexplore bug mentioned below is fixed (also see the github comment), so you can now use a single mapping to toggle the netrw window:

nnoremap <Tab> :Lexplore<CR>

However, see the comment in the code, repeated invoking of Lexplore is not the intended use.

Original answer:

There is a Lexplore command to toggle a netrw window on the left-hand side. However, there is a bug that executing it multiple times will generate a lot of empty buffers.

For now, I think stimulate's answer is quite good. I created my single-function version based on that answer (I am not a vim expert, so not sure if this is better):

nnoremap <Tab> :call ToggleNetRW()<CR>
function ToggleNetRW()
    if exists("g:netrw_buffer") && bufexists(g:netrw_buffer)
        exe "bd".g:netrw_buffer | unlet g:netrw_buffer
        Vexplore | let g:netrw_buffer=bufnr("%")
  • 1
    I added the && bufexists(g:netrw_buffer) part to check for buffer existence, so that errors are avoided in case the netrw buffer was closed in another way, i.e. not via this function. Commented Apr 22, 2020 at 22:45

The best I could come up with for now. It does not respect the current tab page yet, but maybe this could be achieved with using some kind of map data structure

nnoremap <Tab> :call OpenNetRW()<CR>

let g:netrw_buffer=0

function OpenNetRW()
  let g:netrw_buffer=bufnr("%")
  nnoremap <Tab> :call CloseNetRW()<CR>

function CloseNetRW()
  exe "bd".g:netrw_buffer
  let g:netrw_buffer=0
  nnoremap <Tab> :call OpenNetRW()<CR>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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