old-www/HOWTO/C-editing-with-VIM-HOWTO/quickfix.html

346 lines
5.2 KiB
HTML

<HTML
><HEAD
><TITLE
> Quickfix
</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=" Multi-file editing
"
HREF="multi.html"><LINK
REL="NEXT"
TITLE=" Copyright
"
HREF="copying.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="multi.html"
>Prev</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
></TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="copying.html"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="SECT1"
><H1
CLASS="SECT1"
><A
NAME="QUICKFIX"
>7. Quickfix</A
></H1
><P
>
When coding in C one often has a edit-compile-edit
cycle. Typically you would edit C file using some the things
I've mentioned earlier, save the file, compile the code and
go to the error(s) and start editing again. VIM helps save
the cycle time slightly using a mode called
quickfix. Basically, one has to save the compiler errors in a file
and open the file with VIM using the command
</P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="SCREEN"
> <TT
CLASS="PROMPT"
>$ </TT
><B
CLASS="COMMAND"
>vim -q compiler_error_file</B
>
</PRE
></TD
></TR
></TABLE
><P
> VIM automatically opens the file containing the error and
positions the cursor at the location of the first error.
</P
><P
> There is a shortcut to the cycle. Using the command "make", one
can automatically compile code and goto the position where
the first error occurs. To invoke the make command type the
following
</P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="SCREEN"
> <B
CLASS="COMMAND"
>:make</B
>
</PRE
></TD
></TR
></TABLE
><P
> Basically, this command calls make in a shell and goes to the
first error. However, if you are not compiling using make and
are compiling using a command such as cc, then you have to
set a variable called makeprg to the command you want invoked
when you use the make command. For eg.
<B
CLASS="COMMAND"
> :set makeprg=cc\ foo.c
</B
>
</P
><P
> After setting makeprg, you can just call the make command and
quickfix will come into play.
</P
><P
> After you have corrected the first error, the next thing to
do would be go to the next error and correct that. The
following command is used go to the next error.
<B
CLASS="COMMAND"
> :cn
</B
>
</P
><P
> To go back, you can use the command
<B
CLASS="COMMAND"
> :cN
</B
>
</P
><P
> Let me demonstrate this using an example. Consider the
following code
</P
><DIV
CLASS="FIGURE"
><A
NAME="AEN326"
></A
><P
><B
>Figure 11. Quickfile Program Listing
</B
></P
><DIV
CLASS="MEDIAOBJECT"
><P
><IMG
SRC="quickfix_prog.png"
></IMG
></P
></DIV
></DIV
><P
> As you can see there is an error on line number 5. The file
is saved as test.c and makeprg is set using the command
</P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="SCREEN"
> <B
CLASS="COMMAND"
>:set makeprg=gcc\ test.c</B
>
</PRE
></TD
></TR
></TABLE
><P
> Next the make command is invoked using the command
<B
CLASS="COMMAND"
>:make</B
>. gcc gives an error and the output
of the make command is something like this
</P
><DIV
CLASS="FIGURE"
><A
NAME="AEN338"
></A
><P
><B
>Figure 12. :make error
</B
></P
><DIV
CLASS="MEDIAOBJECT"
><P
><IMG
SRC="make_error.png"
></IMG
></P
></DIV
></DIV
><P
> On pressing <B
CLASS="KEYCAP"
>RETURN</B
>, the cursor moves to line
number 6.
</P
><P
> Now, the command <B
CLASS="COMMAND"
>:cn</B
> will move the cursor
to the line number 4.
</P
><P
> To move back to the previous error, one can use the command
<B
CLASS="COMMAND"
>:cN</B
> and the cursor will move back to the
line 6.
</P
><P
> After correcting the error on line 5 and adding "return 1;",
one can run <B
CLASS="COMMAND"
>:make</B
> again and the output
will be
</P
><DIV
CLASS="FIGURE"
><A
NAME="AEN353"
></A
><P
><B
>Figure 13. No Error
</B
></P
><DIV
CLASS="MEDIAOBJECT"
><P
><IMG
SRC="make_no_error.png"
></IMG
></P
></DIV
></DIV
><P
> That was just a small example. You can use quickfix to solve
your compile time problems and hopefully reduce the
edit-compile-edit cycle.
</P
></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="multi.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="copying.html"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Multi-file editing</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
>&nbsp;</TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Copyright</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>