2

I'd like :Vexplore to be toggleable. ie if netrw is open in a visible buffer, close it. If netrw is not open in a visible buffer, open it. Upon opening netrw with :Vexplore, change the width of the netrw buffer to 40 characters.

I think this is the behavior of VSCode's file explorer and maybe other gui IDEs, in case that helps explain it.

2

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
2
  • 2
    :vertical resize 40 will 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 at 20:58
  • 1
    Oh duh of course it does. Just sticking the line I mentioned below your call to Vex should do it.
    – Rich
    Feb 24 at 20:59

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.