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:
DJMcMayhem
  • 7,285
  • 4
  • 41
  • 61

1 Answers1

1

You don't want BufWritePre because that modifies your file before saving it. Instead, try doing BufWritePost:

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

This will still ask you to hit enter, but it doesn't prompt to load the file when I test it. If you don't want to have to hit enter after saving, you can change this to:

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

Also, note how I removed the : from your command. This is because autocmd looks for an ex command, so the : is unneeded.

DJMcMayhem
  • 7,285
  • 4
  • 41
  • 61
  • It is still prompts for Load or Ok – vishwanath patil Oct 24 '17 at 18:55
  • @vishwanathpatil Well, I"m not sure what to tell you because it doesn't prompt me when I run it locally. Sorry – DJMcMayhem Oct 24 '17 at 19:50
  • autocmd VimLeave *.[ch] exec "!indent -kr -nut %" | redraw , using VimLeave it works fine – vishwanath patil Oct 25 '17 at 05:43
  • From a [similar answer using `astyle`](https://stackoverflow.com/a/2506955/248390), I used this to format Golang code; in `~/.vim/plugin/go.vim` I added `autocmd BufWritePre *.go silent! exec "%!goimports"`; unfortunately it still move the cursor to the top of the file – bufh Aug 26 '20 at 15:15