Skip to main content
1 of 4
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k

A cursory glance at the various Vim help files does not reveal whether this is possible without resorting to using a Vim script. If you use :e filename in Vim and filename does not exist, the editor will open a new buffer of that name and will save the buffer to that name when you use :w. This is usually what you would want to do.

To require that a filename exists when starting Vim, you can overload the vim command with a shell function:

vim () {
    if [ -e "$1" ] || [ -z "$1" ]; then
        command vim ${1:+"$1"}
    else
        printf 'No such file or directory: %s\n' "$1" >&2
        return 1
    fi
}

This would allow you to

  1. only edit an existing file, and
  2. start Vim with no file on the command line (this would enable you to create a new file with :e filename in Vim).

The two tests, [ -e "$1" ] and [ -z "$1" ], detects whether the first argument to the function exists as a filename, or whether it's empty.

The parameter expansion ${1:+"$1"} will expand to the given filename, quoted, or to nothing if there was no filename given.

command vim ensures that we don't call our function recursively.

Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k