I'm trying to override 'w' in vim so it would call an external program and filter the buffer instead of writing to a file. There are very good examples across the internet about how to do that. I tried one from vim.wikia.com, but vim always complains with E488: Trailing characters
. This is the command in my vimrc:
cabbrev w <c-r>=(getcmdtype()==':' && getcmdpos()==1 ? 'W' : 'w')<CR>
I'm not very familiar with vim script. I tried removing <CR>
from the end of the line with no luck.
UPDATE
Since I want to run vim as customized as possible I run it with the -u
flag. I noticed that vim behaves differently when using that flag compared to running it without it
With the -u
flag the expanded abbreviation is what needs to be evaluated as code.
Without the flag, the abbreviation is what it is intended to be (here I enter the cabbrev rule from vim's prompt)
Regarding the -u
flag vim's man page says this:
-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.
Apparently when this flag is used the initialization from vim /etc/vimrc is not performed and there I found this option:
set nocompatible
vim's help about compatible option:
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
makes the cabbrev syntax from the question work