old-www/HOWTO/C-editing-with-VIM-HOWTO/auto-format.html

340 lines
5.4 KiB
HTML

<HTML
><HEAD
><TITLE
> Formating automatically
</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.57"><LINK
REL="HOME"
TITLE=" C editing with VIM HOWTO
"
HREF="index.html"><LINK
REL="PREVIOUS"
TITLE=" Auto-Completing Words
"
HREF="auto-complete.html"><LINK
REL="NEXT"
TITLE=" Multi-file editing
"
HREF="multi.html"></HEAD
><BODY
CLASS="SECT1"
BGCOLOR="#FFFFFF"
TEXT="#000000"
LINK="#0000FF"
VLINK="#840084"
ALINK="#0000FF"
><DIV
CLASS="NAVHEADER"
><TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="3"
ALIGN="center"
>C editing with VIM HOWTO</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="auto-complete.html"
>Prev</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
></TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="multi.html"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="SECT1"
><H1
CLASS="SECT1"
><A
NAME="AUTO-FORMAT"
>5. Formating automatically</A
></H1
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="AEN258"
>5.1. Restricting column width</A
></H2
><P
> One often has to restrict the column width to 80 or 75 or
whatever. One can set this quite easily by using the
command
</P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="SCREEN"
> <B
CLASS="COMMAND"
>:set textwidth=80</B
>
</PRE
></TD
></TR
></TABLE
><P
> To do this automatically just put the command in your .vimrc.
</P
><P
> In addition to textwidth you may want the text to wrap at
a certain column. Often such choices are dictated by the
terminal one is using or it could just be by choice. The
command for such a case is
</P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="SCREEN"
> <B
CLASS="COMMAND"
>:set wrapwidth=60</B
>
</PRE
></TD
></TR
></TABLE
><P
> The above command makes the text wrap at 60 columns.
</P
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="AEN268"
>5.2. Automatically indent code</A
></H2
><P
> While coding in C, one often indents inner-blocks of
code. To do this automatically while coding, VIM has an
option called cindent. To set this, just use the command
</P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="SCREEN"
> <B
CLASS="COMMAND"
>:set cindent</B
>
</PRE
></TD
></TR
></TABLE
><P
> By setting cindent, code is automatically beautified. To
set this command automatically, just add it to your .vimrc
</P
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="AEN274"
>5.3. Comments</A
></H2
><P
> VIM also allows you to auto-format comments. You can split
comments into 3 stages: The first part, the middle part
and the end part. For example your coding style
requirements may require comments to be in the following
style
</P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> /*
* This is the comment
*/
</PRE
></TD
></TR
></TABLE
><P
> In such a case the following command can be used
</P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="SCREEN"
> <B
CLASS="COMMAND"
>:set comments=sl:/*,mb:*,elx:*/</B
>
</PRE
></TD
></TR
></TABLE
><P
> Let me decipher the command for you. The commands has
three parts. The first part is sl:/*. This tells VIM that
three piece comments begin with /*. The next part tells
VIM that the middle part of the comment is *. The last
part of the command tells vim a couple of things. One that
the command should end with */ and that it should
automatically complete the comment when you hit just /.
</P
><P
> Let me give another example. Lets say your coding
guidelines are as follows
</P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
> /*
** This is the comment
*/
</PRE
></TD
></TR
></TABLE
><P
> In such a situation you can use following command for comments
</P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="SCREEN"
> <B
CLASS="COMMAND"
>:set comments=sl:/*,mb:**,elx:*</B
>
</PRE
></TD
></TR
></TABLE
><P
> to insert a comment just type /* and hit enter. The next line
will automatically contain the **. After you've finished
the comment just hit enter again and another ** will be
inserted. However to end the comment you want a */ and not
**/. VIM is quite clever here. You don't need to delete
the last * and replace it with /. Instead, just hit / and
VIM will recognise it as the end of the comment and will
automatically change the line from ** to */.
</P
><P
> For more info hit
<B
CLASS="COMMAND"
> :h comments
</B
>
</P
></DIV
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="auto-complete.html"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="multi.html"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Auto-Completing Words</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
>&nbsp;</TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Multi-file editing</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>