Indenting/formating

32 views
Skip to first unread message

Gautier DI FOLCO

unread,
Apr 24, 2012, 6:58:48 AM4/24/12
to vim...@googlegroups.com
Hi,

I recently look for re-(format|indent)ing my code and after many queries on google or in :help I didn't find a satisfactory solution.
On websites I found a solution with AStyle or indent and in :help I have to make a very big function.
Basically I want to follow de Allman Style (http://en.wikipedia.org/wiki/Indent_style#Allman_style),
maybe then I would align commas dots,  or even comments, but it's still basic.
I don't want an external tool because I work on many computers with many constraints.

For your help,
In advance,
Thanks.

Ben Fritz

unread,
Apr 24, 2012, 10:54:40 AM4/24/12
to vim...@googlegroups.com
On Tuesday, April 24, 2012 5:58:48 AM UTC-5, Gautier DI FOLCO wrote:
> Hi,
>
> I recently look for re-(format|indent)ing my code and after many queries on google or in :help I didn&#39;t find <span lang="en"><span>a satisfactory solution.
> On websites I found a solution with AStyle or indent and in :help I have to make a very big function.
>
> Basically I want to follow de Allman Style (<a href="http://en.wikipedia.org/wiki/Indent_style#Allman_style" target="_blank">http://en.wikipedia.org/wiki/<WBR>Indent_style#Allman_style</a>), </span></span><span lang="en"><span>maybe</span> <span>then</span> <span>I would</span> <span>align</span> <span>commas dots,  or</span> <span>even</span> <span>comments</span><span>,</span> <span>but it&#39;s still</span> <span>basic.
>
> I don&#39;t want an external tool because I work on many computers with many constraints.
>

If I assume correctly that you're writing code in C, just turn on filetype-specific indenting, or turn on 'cindent'. If you want to tweak the automatic indenting, update the 'cinoptions' setting.

See the following for a basic introduction for Vim's indentation features:

http://vim.wikia.com/wiki/Indenting_source_code

Gautier DI FOLCO

unread,
Apr 25, 2012, 2:43:45 AM4/25/12
to vim...@googlegroups.com
Le 24 avril 2012 16:54, Ben Fritz <fritzo...@gmail.com> a écrit :
If I assume correctly that you're writing code in C, just turn on filetype-specific indenting, or turn on 'cindent'. If you want to tweak the automatic indenting, update the 'cinoptions' setting.

See the following for a basic introduction for Vim's indentation features:

http://vim.wikia.com/wiki/Indenting_source_code

Hi,

Thank you for your answer.
I mainly writing code in C-like language but initially I want to make it work for C code.
I known this page but I would go further : the equal sign must have a space before and after, commas should be followed by a space, the keywords of sctructure control (if, switch, for, do, etc.) must be followed by a space before the parenthesis, braces should be on a dedicated line, etc.
Is it a mean to do that with ViM?

John Beckett

unread,
Apr 25, 2012, 3:26:40 AM4/25/12
to vim...@googlegroups.com
Gautier DI FOLCO wrote:
> I known this page but I would go further : the equal sign
> must have a space before and after, commas should be followed
> by a space, the keywords of sctructure control (if, switch,
> for, do, etc.) must be followed by a space before the
> parenthesis, braces should be on a dedicated line, etc.
> Is it a mean to do that with ViM?

I don't think Vim is suitable for that. You need a lint program
that gives style warnings. If you can find such a program, you
can probably make it work with Vim (displaying messages from
lint like from a compiler using the quickfix window).

John

Michael Henry

unread,
Apr 25, 2012, 5:58:23 AM4/25/12
to vim...@googlegroups.com
On 04/25/2012 03:26 AM, John Beckett wrote:
> Gautier DI FOLCO wrote:
>> I known this page but I would go further : the equal sign
>> must have a space before and after, [...]

>
> I don't think Vim is suitable for that.

For a tool outside of Vim, you might like "uncrustify":
http://uncrustify.sourceforge.net/

It has hundreds of options to match a wide variety of styles,
and it does a good job of parsing and reformatting source code.

Michael Henry

Ben Fritz

unread,
Apr 25, 2012, 11:15:13 AM4/25/12
to vim...@googlegroups.com

If you do find an external tool you like, you can invoke it from within Vim to replace the built-in format and/or indent commands.

See :help 'formatprg' and :help 'equalprg'. I'd suggest 'formatprg' for heavy-duty formatting like adding whitespace within lines or moving text between lines, and 'equalprg' for more lightweight indentation stuff.

Reply all
Reply to author
Forward
0 new messages