为什么 vim returns E488:我的函数的尾随字符?

Why vim returns E488: Trailling characters to my function?

我试图在保存文件时执行 yapf 命令来格式化我的 python 文件,因此我创建了一个调用此命令的函数:

function Format_python_file()
    silent :!yapf --style="{based_on_style: pep8, indent_width: 4}" -i %
    silent :e %
endfunction

autocmd BufWritePost *.py call Format_python_file() <afile>

问题出在你的 autocmd 行,你在那里有一个尾随 <afile>

事实上,我看到的消息对此非常明确:

Error detected while processing BufWritePost Autocommands for "*.py":
E488: Trailing characters: <afile>

您应该删除 <afile>,该函数本身已经在当前缓冲区上运行,不需要任何参数或对当前文件的其他引用。


另请注意,将您的 autocmd 放入首先被清除的 augroup 中是一种很好的做法。这样,如果您重新加载源文件(vimrc 或其他),它不会创建重复的 autocmds.

设置这个 autocmd 的更简洁的方法是:

augroup python_yapf
    autocmd!
    autocmd BufWritePost *.py call Format_python_file()
augroup END