Sometimes I accidentally tell vim to edit a directory instead of a file in it

vim directory-name           # what I typed
vim directory-name/blah.txt  # what I meant to type

Instead of immediately giving an error message, vim will open the directory in a "file editor" mode that I personally don't like:

The file browser I don't like

How can I make vim refuse to open directories instead?

One possible avenue could be by changing some settings in my .vimrc but I don't know what they would be.

Another possibility would be to create a wrapper shell alias around vim that checks with something along the lines of test -d "$1" to see if I'm trying to open a file. However, I don't know to make this alias robust so that it can tell apart which command-line arguments are flags and which are file names.

  • vim doesn't edit directories. It discovers that your argument is a directory it gives you the choice to select one of the files within that directory. You may be incompatible to vim, so choose a different editor or help yourself with a shell function that checks if your argument is a file or not. – ott-- Jul 24 '16 at 17:11

You can put the following lines in your vimrc to quit vim if any of its arguments are a directory:

for f in argv()
  if isdirectory(f)
    echomsg "vimrc: Cowardly refusing to edit directory " . f

Alternatively, if you only want to quit if all arguments are directories, you can try something like this:

let ndirs = 0
for f in argv()
  if isdirectory(f)
    let ndirs += 1
if ndirs > 0 && ndirs == argc()
  echomsg "vimrc: Cowardly refusing to edit directories"
  • I think it would be more natural to do this kind of thing as a shell script wouldn't it? – hugomg Jul 23 '16 at 20:02
  • 2
    @hugomg not really, vim code will be more portable, and vim should know its arguments while under {bash,csh,fish,ksh,tcsh,zsh,$other} you'd (ideally) need to distinguish between file arguments and options and arguments to options, which could introduce false positives. – thrig Jul 23 '16 at 20:18
  • Oh, I didn't know that argv() didn't include the flags! Before I accept this, what is that "whoops, more things to edit" case that you are working around? – hugomg Jul 23 '16 at 22:45
  • @hugomg try vim file /etc or vim /etc file instead of just the simple vim file or vim /etc – thrig Jul 24 '16 at 14:50
  • I had to make some changes to get everything to work. First of all, your original code was buggy with multiple files (ex.: vim /etc /etc). Secondly, the echomsg would only appear to the shell that invoked vim if I put the commands directly in the vimrc, outside the QuitIfDirectory function. – hugomg Jul 24 '16 at 16:45

This looks like the output of the netrw plugin.

This can be disabled by entering in your ~/.vimrc file.

let loaded_netrwPlugin = 1

Now if you try and edit a directory you'll get an error is a directory.

  • Unfortunately, that still causes vim to open a new window so its not much better than before... – hugomg Jul 23 '16 at 22:37

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.