UPDATE: Thanks to Rick's comment this solution is now perfect for me!
The below solution isn't perfect, but it's good enough for now. I would prefer this solution to use 40 char width for the netrw buffer instead of 20% of window width. If anyone knows how I'd greatly appreciate it.
On ctrl-n toggle Vex and limit it's size to 20% of the window width. I borrowed (ie stole) from answers to Toggle explorer window.
nmap <C-n> :call VToggleNetrw()<CR>
let g:netrw_winsize=20 " percentage of window width
function! VToggleNetrw()
let i = bufnr("$")
let wasOpen = 0
while (i >= 1)
if (getbufvar(i, "&filetype") == "netrw")
silent exe "bwipeout " . i
let wasOpen = 1
endif
let i-=1
endwhile
if !wasOpen
silent Vexplore
" Added from Rich's comment. Resize netrw to 40 chars wide
:vertical resize 40
endif
endfunction
:vertical resize 40will make the current window 40 columns wide. I’m too lazy to figure out whether your current code always visits the window at some point, or if you’ll need to write code to specifically switch to it. – Rich Feb 24 '21 at 20:58