Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Rarely will a team of programmers agree on formatting options such as tabs vs. Spaces, opening curly brackets on newlines, and other options. Is there a way to have VIM parse a file (even through an external utility such as tidy) automatically on open and write such that I edit like this:

if ( something != other ) {
    Console.WriteLine("Var: {0}", something);
} else {
    Console.WriteLine("Just some code");
}

However the file would be saved like this:

if ( something!=other )
{
    Console.WriteLine("Var: {0}", something);
}
else
{
    Console.WriteLine("Just some code");
}

This would be relevant for converting in both directions on open and on write.

share|improve this question
add comment

3 Answers

up vote 3 down vote accepted

Over the course of your career you will encounter a variety of formatting styles. My advice is to get used to it--learn to adapt your style to that of the file you're working on. Among other reasons, tools for translating from one style to the next generally don't make the round trip without introducing changes to the file, and that makes it difficult to use a tool like diff to see the substantive changes you made.

That said, Vim provides a number of hooks to execute commands as files are read and written. See

:help autocommand

For example,

au BufWrite * %s/\s\+$//

will remove trailing spaces from a buffer just before you write it to disk. The command can be any ex command including calling a function or filtering the entire buffer through an external program.

The BufWrite event is triggered just before the buffer is written. Another name for it is BufWritePre. If you want to write the buffer, then undo the changes made at the BufWritePre event, you can use the BufWritePost command to revert those changes. The BufRead (a.k.a. BufReadPost) event can be used to trigger commands that format text the way you want it when a file is read.

The * will match any file name. You can use file name patterns such as .c,.cpp to trigger your autocommands only when certain kinds of files are read or written.

As another example,

au BufRead *.[ch] normal gg=G

will re-indent any C source file you read according to your C indenting rules. See

:help =
:help C-indenting
share|improve this answer
    
T‏h‏a‏n‏k‏‏s‏‏! –  dotancohen Aug 13 '11 at 7:58
add comment

For adjusting how big an actual tab indent shows up as, you can use the tabstop directive. in your .vimrc. I have mine set to 3

set tabstop=3
share|improve this answer
    
I should have been more specific. I mean how can VIM parse (or send the file to an external script for parsing) a file on save / write to change details such as putting curly brackets on newlines. –  dotancohen Aug 10 '11 at 20:51
add comment

Check out this plugin: https://github.com/Chiel92/vim-autoformat.

Seems like it's exactly what you're looking for. It formats your source file with one button press through external formatters.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.