tags:

views:

88

answers:

2

I'd like to run indent on each .c and .h file I open in vim. I've looked at setting equalprg, but I'd rather have it done for me when I open the buffer, so I tried:

autocmd BufReadPost *.[ch] '[,']!indent

This works fine if the file has no syntax errors, but spews error messages from indent into the file if I'm missing a closing brace, say. I haven't done anything with plugins before. Am I better off writing an ftplugin? I don't mind silent failure on errors, but the file should be left pristine in that case. Any hints greatly appreciated.

A: 

How about (untested):

autocmd BufReadPost *.[ch] gg=G
Aaron
The problem is that I want to run GNU indent, not the built in indent. I have lots of preferences configured that vim's indent doesn't handle.
Leonard
+1  A: 

If you want to use GNU indent. It sounds like you want something like:

autocmd BufReadPost *.[ch] silent !indent "%"
set autoread

(Also untested) :-)

Benj
This results in an oddity: The file is formatted, but the vim buffer is empty. I get the message:W11: Warning: File "web_server.c" has changed since editing startedSee ":help W11" for more info.[O]K, (L)oad File: I choose "L" and get a blank buffer. :-(Setting autoread just automates that process. :-(
Leonard
But this works:autocmd BufReadPost *.[ch] silent !indent "%" set autoread
Leonard
Ok, edited accordingly
Benj
The comment didn't let me format it, but "set autoread" should be on a separate line.
Leonard
Ok, well pointed out :-)
Benj