1

I want use the indent linux utility to indent the current file on saving. I tried

autocmd BufWritePre *.[ch] :!indent -kr -nut %

in my .vimrc file (~/.vim).

Manually I tried that command

:!indent -kr -nut %

it works only after prompting me to load the file, as below,

See ":help W12" for more info.
[O]K, (L)oad File:
4

1 回答 1

1

您不想要BufWritePre,因为这会在保存文件之前修改您的文件。相反,尝试这样做BufWritePost

autocmd BufWritePost *.[ch] !indent -kr -nut %

这仍然会要求您按 Enter,但在我测试它时它不会提示加载文件。如果您不想在保存后按 Enter 键,可以将其更改为:

autocmd BufWritePost *.[ch] exec "!indent -kr -nut %" | redraw

另外,请注意我是如何:从您的命令中删除的。这是因为autocmd查找 ex 命令,所以:不需要。

于 2017-10-24T17:56:04.477 回答