1

I have figured out how to open a file browser in a split, using the following in vimrc:

let g:netrw_liststyle = 3
let g:netrw_banner = 0
let g:netrw_browse_split = 4
let g:netrw_winsize = 25
let g:netrw_altv = 1
augroup ProjectDrawer
  autocmd!
  autocmd VimEnter * :Vexplore
augroup END

This is nice since I can then work with multiple files without having to exit and reopen vim all the time.

The issue is when I want to edit a specific file. Then I do not need or want the file browser.

So my question is: is there a way to wrap this in an if statement which would only open netrw in a split if the argument to vi is not a file:

  1. vi path/to/folder/ - Include
  2. vi path/to/folder - Include
  3. vi - Include
  4. vi path/to/folder/my_file - Skip
5
  • 1
    Doesn't Vim already open netrw by default if you open a path to a directory? It does that for me anyway, without having to set anything up. Commented Aug 22, 2022 at 10:19
  • 1
    See :help v:argv.
    – romainl
    Commented Aug 22, 2022 at 11:37
  • @MartinTournoij My commands opens it in a left side pane, kinda like VSCode. So not the same. Commented Aug 23, 2022 at 8:55
  • Ah right; I edited the question a little bit to make that clearer. Commented Aug 23, 2022 at 15:10
  • @MartinTournoij But Christopher also wants netrw opened if no arguments are specified. I think your title edit obscures this.
    – Rich
    Commented Aug 24, 2022 at 8:41

1 Answer 1

0

I would propose the following code:

let s:hasfolder=1
let g:foldertoopen = '.'

function s:parse_arguments()
  let i = 1
  while i < len(v:argv)
    if v:argv[i][0] == '-'
      let i = i + 2
    else
      let path = v:argv[i]
      if path[len(path)-1] == '\' || path[len(path)-1] == '/'
        let path = path[:-2]
      endif
      if finddir(escape(fnamemodify(path, ':t'), ' '), escape(fnamemodify(path, ':h'), ' ')) != ''
        let s:hasfolder = 1
        let g:foldertoopen = v:argv[i]
        break
      else
        let s:hasfolder = 0
      endif
      let i = i + 1
    endif
  endwhile
endfunction

call s:parse_arguments()

let g:netrw_liststyle = 3
let g:netrw_banner = 0
let g:netrw_browse_split = 4
let g:netrw_winsize = 25
let g:netrw_altv = 1

function! InitBrowser()
  if g:foldertoopen != ''
    enew
    exe 'Vexplore ' . g:foldertoopen
    let g:foldertoopen = ''
  endif
endfunction

if s:hasfolder
  augroup ProjectDrawer
    autocmd!
     autocmd VimEnter * :call InitBrowser()
  augroup END
endif
8
  • 1
    You can break the while loop when you find a folder.
    – D. Ben Knoble
    Commented Aug 23, 2022 at 13:35
  • @Ben, You are right, I have been lazy ;-) Commented Aug 23, 2022 at 16:06
  • Just tried it but it doesn't seem to work. I don't know enough about coding in vimrc (Vimscript it's called, right?) to debug it. Could be just a small thing. But now I have something to start with at least. Thx! Commented Aug 23, 2022 at 18:19
  • Thanks for the feedback. It works for me. On which OS are you using Vim? Which version of Vim have you? You can insert echom trace to understand how the code is executed. Commented Aug 23, 2022 at 19:55
  • 1
    Yes, thanks! I've marked it as accepted answer. Commented Aug 24, 2022 at 12:14

Your Answer

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

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