1

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 Mar 18 at 15:25
1

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 anwser is quite good. I created my single-function version based on that answer (I am not a vim expertise 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
    else
        Vexplore | let g:netrw_buffer=bufnr("%")
    endif
endfunction
| improve this answer | |
  • 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. – Philipp Moers Apr 22 at 22:45
0

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()
  Vexplore
  let g:netrw_buffer=bufnr("%")
  nnoremap <Tab> :call CloseNetRW()<CR>
endfunction

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

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.