Vim在保存时调用命令

Vim在保存时调用命令,vim,Vim,我正在尝试在保存时调用自动格式化插件 这是我的自动命令: autocmd BufWrite *.css,*.html,*.js,*.py :Autoformat<CR> autocmd BufWrite*.css、*.html、*.js、*.py:Autoformat 当我保存时,如果我手动调用:Autoformat,则自动格式化程序将运行 我做错了什么?根据我的经验,你有时需要将它围成一个augroup augroup autoFormat autocmd BufWri

我正在尝试在保存时调用自动格式化插件

这是我的自动命令:

autocmd BufWrite *.css,*.html,*.js,*.py :Autoformat<CR>
autocmd BufWrite*.css、*.html、*.js、*.py:Autoformat
当我保存时,如果我手动调用:Autoformat,则自动格式化程序将运行


我做错了什么?

根据我的经验,你有时需要将它围成一个
augroup

augroup autoFormat
    autocmd BufWrite *.css,*.html,*.js,*.py :Autoformat<CR>
augroup END
augroup自动格式化
autocmd BufWrite*.css、*.html、*.js、*.py:Autoformat
螺旋端

我不知道为什么,但它对我很有效!从技术上讲,只有
autocmd
可以独立工作,但有时不能。另外,在GitHub页面上,它说要使用
:Autoformat
,也许可以尝试一下。

出于某种原因,我不得不放弃回车符,改为BufWritePre(虽然CR是主要问题,但BufWritePre只是确保在写入缓冲区之前而不是之后对其进行更改,以便保存):


为什么,我不知道?

您已经找到了解决方案-以下是解释:

用于映射,其工作方式类似于记录的键入键序列,因此您需要使用
启动命令行模式,并使用
结束。autocmd接受Ex命令,因此
被视为(无效)参数。您也不需要
,但这并没有害处

:help BufWrite
所示,这是
BufWritePre
的同义词

因此,这是推荐的形式:

autocmd BufWritePre *.css,*.html,*.js,*.py Autoformat

这个剧本很神秘。也许它会执行Autoformat的更改,但在使用BufWrite之后,它会将旧的缓冲区写入文件
  BufWrite or BufWritePre     Before writing the whole buffer to a file.
autocmd BufWritePre *.css,*.html,*.js,*.py Autoformat