使用 cabbrev 覆盖内置命令

Override a builtin command with cabbrev

我正在尝试覆盖 vim 中的 'w' 以便它调用外部程序并过滤缓冲区而不是写入文件。互联网上有很多关于如何做到这一点的很好的例子。我尝试了 vim.wikia.com 中的一个,但 vim 总是抱怨 E488: Trailing characters。这是我的 vimrc:

中的命令
cabbrev w <c-r>=(getcmdtype()==':' && getcmdpos()==1 ? 'W' : 'w')<CR>

我对 vim 脚本不是很熟悉。我尝试从行尾删除 <CR> 但没有成功。

更新

因为我想 运行 vim 尽可能自定义,所以我 运行 它带有 -u 标志。我注意到 vim 使用该标志时的行为与 运行 不使用它时的表现不同

带有 -u 标志的扩展缩写是需要作为代码进行评估的内容。

没有标志,缩写就是它的本意(这里我根据 vim 的提示输入 cabbrev 规则)

关于 -u 标志 vim 的手册页是这样说的:

-u {vimrc} Use the commands in the file {vimrc} for initializations. All the other initializations are skipped. Use this to edit a special kind of files. It can also be used to skip all initializations by giving the name "NONE". See ":help initialization" within vim for more details.

显然当这个标志被使用时初始化从 vim /etc/vimrc 没有执行,我在那里找到了这个选项:

set nocompatible

vim 关于 compatible 选项的帮助:

This option has the effect of making Vim either more Vi-compatible, or make Vim behave in a more useful way. This is a special kind of option, because when it's set or reset, other options are also changed as a side effect. CAREFUL: Setting or resetting this option can have a lot of unexpected effects: Mappings are interpreted in another way, undo behaves differently, etc. If you set this option in your vimrc file, you should probably put it at the very start.

...

When a vimrc or gvimrc file is found while Vim is starting up, this option is switched off, and all options that have not been modified will be set to the Vim defaults. Effectively, this means that when a vimrc or gvimrc file exists, Vim will use the Vim defaults, otherwise it will use the Vi defaults. (Note: This doesn't happen for the system-wide vimrc or gvimrc file, nor for a file given with the -u argument).

set nocompatible 使问题中的 cabbrev 语法有效