[lug] Source Code Pretty-fying

Chip Atkinson chip at pupman.com
Tue Sep 14 21:52:46 MDT 2004

Here's what I use for keeping things nice tab wise in vim:

:se et
:se ai
:se wm=15
:se tabstop=2
:se shiftwidth=2

et is "expand tabs" which turns a tab into tabstop number of spaces.
This makes your code come out better on printers.
ai is "auto indent" which puts your cursor under the first character
of the line above.  Warning:  use :se noai to turn off auto indent.
It is important to do so if you do much cutting and pasting.  You end
up with a wrapped up mess if it's on.
wm is "wrap margin", the number of characters from the right edge of
the screen to break lines.  Set it to 0 to stop wrapping.
tabstop is how many characters a tab represents.
shift width is, I *believe*, the number of characters to shift a line
when using the > or < operators.

If you wish to shift a block of text between braces, use a combination
of % and >.  For example, put your cursor on one of the bounding
braces.  Then use >% and your block of text will be indented by
shiftwidth characters.   Unfortunately (for my coding style) it also
indents the line with the { and } on it.  You can easily unindent that
part though.

To use indent from within vi, use something like :1,$ !indent

Hope that helps.


On Tue, 14 Sep 2004, Tkil wrote:

> >>>>> "Matt" == Matt Thompson <thompsma at colorado.edu> writes:
> Matt> I have some C code that has been hacked on by about 4-5 people.
> Matt> Apparently, each person had their own idea on what "tab" should
> Matt> do.  I say 3 spaces, another said 2 spaces, another said the
> Matt> hard \t.  So, when I open this code in VIM, I get code that is
> Matt> wacky-indented.  You know, some lines inside like 3 if's are
> Matt> underindented, etc.
> The usual culprit is people used to windows systems, which have
> adopted a 4-spaces-per-tab-character standard.  You can set tab widths
> in vi, but I don't recall exactly how.
> As for imposing style in vi for new code, I don't know about that
> either.  (x)emacs has an editing mode that will automatically build
> brace blocks with customizable indentation and newline arrangements,
> and configures the tab key to go to an appropriate indentation level
> (with either tabs, spaces, or a mix); I suspect there are similar
> add-ins for vi.
> Seems like a place to start might be:
>    http://hermitte.free.fr/vim/c.php
> Some other comments on using modes in VIM:
>    http://www.justlinux.com/nhf/Programming/Introduction_to_C_Programming.html
> Matt> Thus, I figured there has to be some utility that will resolve
> Matt> this and impose regular indenting.  Heck, there might be
> Matt> something for VIM, but I couldn't see it.  Any help?
> As Ralf already indicated, you probably have 'indent' installed on
> your system already, and it can do much of this.
> As he also said, this incantation will have (x)emacs indent the
> current file:
>    C-x h M-x untabify RET
>    C-x h M-x indent-region RET
> ("Untabify" is the in-emacs equivalent of 'expand' at the command
> prompt...)
> The magic phrase you are looking for, though, to have google tell you
> all about these lovely toys, is "pretty printer":
>   http://www.google.com/search?q=source+code+pretty+printer
> Happy hacking,
> t.
> _______________________________________________
> Web Page:  http://lug.boulder.co.us
> Mailing List: http://lists.lug.boulder.co.us/mailman/listinfo/lug
> Join us on IRC: lug.boulder.co.us port=6667 channel=#colug

More information about the LUG mailing list