1

I've toggled netrw explorer window by adding the following commands found in an answer posted to this question to the vimrc file.

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

And I have autocmd calling ToggleNetrw() with the following.

augroup ProjectDrawer
    autocmd!
    autocmd VimEnter * :call ToggleNetrw()
    autocmd VimEnter * wincmd p
augroup END

But when a directory is opened in vim from the terminal (in my case: vim .) it opens the directory in the editor window.

enter image description here

And When a file is selected and opened it is displayed the toggled explorer window.

enter image description here

Is there a way to make vim open directories in the toggled netrw window? Thanks for the help.

4
  • Welcome to Vi and Vim! "I've toggled netrw explorer window using the answer posted to this question." Please indicate which commands exactly you have in your vimrc or that you executed. It's hard to guess, from a question that's not really directly related. Try to make your question stand on its own, even when you refer to other questions for additional context. Please edit your question to include the commands you used.
    – filbranden
    Jun 8, 2020 at 14:22
  • Do you invoke ToggleNetrw() at Vim startup? Do you have an autocmd calling it?
    – filbranden
    Jun 8, 2020 at 14:56
  • I have autocmd calling it. augroup ProjectDrawer autocmd! autocmd VimEnter * :call ToggleNetrw() autocmd VimEnter * wincmd p augroup END Jun 8, 2020 at 15:02
  • Please edit the question with your update. It's easier to find the information there rather than in the comments...
    – filbranden
    Jun 8, 2020 at 15:07

1 Answer 1

0

Solved by editing the part calling ToggleNetrw()

From:

augroup ProjectDrawer
    autocmd!
    autocmd VimEnter * :call ToggleNetrw()
    autocmd VimEnter * wincmd p
augroup END

To:

augroup ProjectDrawer
    autocmd!
    if argc() == 1 && isdirectory(argv()[0]) && !exists("s:std_in")
        silent exe "bwipeout " . bufnr("$")
        exe 'cd '.argv()[0]
        autocmd VimEnter * :call ToggleNetrw()
    else
        autocmd VimEnter * :call ToggleNetrw()
        autocmd VimEnter * wincmd p
    endif
augroup END

Thanks for everyone's help.

4
  • This looks like it makes vim . and_other_file and vim some_other_dir hard to actually do
    – D. Ben Knoble
    Jun 9, 2020 at 11:44
  • You're right but they were not working properly before the editing either. any suggestions? Jun 9, 2020 at 15:39
  • Dogmatically, I would say read vimcasts.org/blog/2013/01/…; practically I don’t have much to offer because I dont use this workflow
    – D. Ben Knoble
    Jun 9, 2020 at 15:41
  • vim some_other_directory worked by adding exe 'cd '.argv()[0] to the body of the first condition in the if statement. the answer has been edited. Jun 9, 2020 at 16:07

Your Answer

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

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