I have a permanent window for file explorer enabled by netrw_altv=1 option. The problem is that when I run vim with file or directory as an argument the window with file explorer is focused. How can I use autocmd on VimEnter to automatically focus on the window with the file content (the window on the right) if file is passed as an argument with vim and leave vim focused on the windows with the file explorer (the window on the right) if directory is passed as an argument with vim?

I mean, when calling vim like that:
vim ~/.

This window____|
should be______|
focused _______|

And when calling vim with a file as an argument:
vim src/main.c

_______________| This window
_______________| should be
_______________| focused

1 Answer 1


You could try to add this autocommand to your .vimrc:

autocmd VimEnter * if (argc() > 0 && filereadable(argv()[0])) | wincmd l | endif

The idea is to check if you provided some arguments on the command line (see :h argc()) and if the first argument is an actual file (see :h filereadable() and :h argv()).

If that is the case then we use wincmd to change to the left split (see :h :wincmd).

  • 2
    I would also recommend reading this very good article about why not to use a permanent window for your file explorer.
    – statox
    Commented Dec 4, 2017 at 8:51
  • 1
    Thank you for the solution. If I could I would give you +1 for the comment above because I gave up using a permanent window for the file explorer. I realized that it takes about 10% of my screen and I use it once a 15min, so it is a loss of the screen space. Commented Dec 5, 2017 at 18:46
  • @K.Koovalsky Good for you! You are sharpening the saw that's the way to go :)
    – statox
    Commented Dec 6, 2017 at 7:46

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.