5

neovim 0.2-dev

:Lexplore opens up explorer window and entering :Lexplore again toggles it. However, if I interact with the explorer window(i.e. expand a directory), then typing :Lexplore opens up another explorer window instead of closing the existing one.

Lexplore opens up a new explorer

Is this the expected behavior? Is there any command/function that will toggle explorer even if the window had interaction?

6

I followed this script (https://www.reddit.com/r/vim/comments/6jcyfj/toggle_lexplore_properly/djdmsal/) and it works as expected.

let g:NetrwIsOpen=0

function! ToggleNetrw()
    if g:NetrwIsOpen
        let i = bufnr("$")
        while (i >= 1)
            if (getbufvar(i, "&filetype") == "netrw")
                silent exe "bwipeout " . i 
            endif
            let i-=1
        endwhile
        let g:NetrwIsOpen=0
    else
        let g:NetrwIsOpen=1
        silent Lexplore
    endif
endfunction

" Add your own mapping. For example:
noremap <silent> <C-E> :call ToggleNetrw()<CR>
1

I tried several times and it seems once you have interacted with the content, it opens a new window when :Lexplore is called again like you stated above, however, it could be a desired behavior when you just want to explore multiple directories at once, why not use :NERDTree instead if you just want to keep a single && browser?

  • 1
    Hey thank you for looking into this. netrw does everything I need for exploring and interacting with files and directories. I don't want to install a plugin only for toggling behavior. – Sayem Jan 15 '17 at 7:57
  • I wasn't able to reproduce this behavior here, tried to open and close several directories and open files, but :Lexplore simply works. What exactly have you guys done to get that? – lsrdg Jan 15 '17 at 11:42
  • Try with the latest neovim 0.2-dev or Vim 7.4 – Sayem Jan 16 '17 at 21:12
1

Please try v162j of netrw which may have fixed this problem: http://www.drchip.org/astronaut/vim/index.html#NETRW

1

Updating the version of the netrw plugin indeed fixes the problem, as stated in this answer.

On my Vim v8.0 (installed with Homebrew), I had netrw v156 (find out with :echo g:loaded_netrwPlugin), and the mentioned problem existed. You can find the newest version of netrw on Dr. Chip's website (currently v165b, updated on 13 March 2019). Installing this version fixed the problem for me (confirming what is stated in the other answer that the problem should have been fixed in v162j).

Here is how to install the newest version of the netrw plugin:

  1. Go to http://www.drchip.org/astronaut/vim/index.html#NETRW
  2. Click on the title, this should download a file named netrw.vba.gz
  3. The netrw.vba file contained in the downloaded archive is a vimball file. A vimball file is something like a self-installing plugin. Note that you don't have to unzip the .gz file, as this is done automatically in the next step.
  4. Install the plugin (as explained here or here or by :help vimball):
    1. Open the vimball file with Vim: vim netrw.vba.gz
    2. In Vim, run: :source %

The above command installs the components of the plugin to their proper locations in one of your Vim directories (most probably ~/.vim).

After the installation, you can restart Vim and test if the new version of the plugin has been correctly installed:

:echo g:loaded_netrwPlugin

This should output v165b now (or whatever version you have installed).

0

Based on alwc's answer without using global state. This way it doesn't get out of sync when Netrw was open manually first. It will still "glitch" when opening Netrw manully after it was opend with this function. But there is nothing one can do about manual commands I guess.

function! ToggleNetrw()
        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 Lexplore
    endif
endfunction
map <F3> :call ToggleNetrw() <CR>

Additionally you can set some defaults

augroup ProjectDrawer
    autocmd!
    autocmd VimEnter * :Vexplore
augroup END
let g:netrw_banner=0        " disable annoying banner
let g:netrw_winsize = 18    " width in percent
let g:netrw_browse_split=4  " open in prior window
let g:netrw_altv=1          " open splits to the right
let g:netrw_liststyle=3     " tree view
" ret g:netrw_list_hide=netrw_gitignore#Hide()
" let g:netrw_list_hide.=',\(^\|\s\s\)\zs\.\S\+'
" check: |netrw-browse-maps| for more mappings

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.