更新时间:2023-10-28 09:33:46
一个简单而有效的解决方案是定义一个自动命令匹配可能输入错误的文件名,发出警告并终止保存:
A simple yet effective solution would be to define an auto-command matching potentially mistyped file names, that issues a warning and terminates saving:
:autocmd BufWritePre [:;]* throw 'Forbidden file name: ' . expand('<afile>')
注意 :throw
命令是让 Vim 停止写入所必需的缓冲区的内容.
Note that the :throw
command is necessary to make Vim stop writing
the contents of a buffer.
为了避免由于未捕获而出现 E605
错误异常,可以使用 :echoerr
命令运行发出错误在 try
块中 - :echoerr
将其错误消息作为异常引发当从 try
结构内部调用时(参见 :help :echoerr
).
In order to avoid getting the E605
error because of an uncaught
exception, one can issue an error using the :echoerr
command run
in the try
block—:echoerr
raises its error message as an exception
when called from inside a try
construct (see :help :echoerr
).
:autocmd BufWritePre [:;]*
\ try | echoerr 'Forbidden file name: ' . expand('<afile>') | endtry
如果需要保存名称与模式匹配的文件在上面的自动命令中使用,可以在前面添加一个写入命令使用 :noautocmd
或相应地设置 eventignore
选项(请参阅:help :noautocmd
和 :help eventignore
了解详情),例如:
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 :noautocmd
or set the eventignore
option accordingly (see
:help :noautocmd
and :help eventignore
for details), e.g.:
:noa w :ok.txt