I type really fast and sometimes accidentally save a file with the name consisting of a single ; or :. (A typo is sometimes introduced as I type the :wq command.)
Is there any way to write a macro that rejects files matching certain names from being saved?
A simple yet effective solution would be to define an auto-command
matching potentially mistyped file names, that issues a warning and
terminates saving:
Note that the
:throwcommand is necessary to make Vim stop writingthe contents of a buffer.
In order to avoid getting the
E605error because of an uncaughtexception, one can issue an error using the
:echoerrcommand runin the
tryblock—:echoerrraises its error message as an exceptionwhen called from inside a
tryconstruct (see:help :echoerr).If it is ever needed to save a file with a name matching the pattern
used in the above auto-command, one can prepend a writing command
with
:noautocmdor set theeventignoreoption accordingly (see:help :noautocmdand:help eventignorefor details), e.g.: