Vim's Quickfix mode

Source: Internet
Author: User
Often, during the development process, we often write code, compile, and modify compilation errors, a process that can be repeated hundreds of 10 of times. If you are based on the compiler output error information, open the wrong file, find the wrong line, and then start to modify, then the efficiency is too low.

Using Vim's Quickfix mode, can greatly speed up this process, you can start in vim, and then vim will be the compiler output error information, automatically jump to the first error place, let you modify; After the modification, use a shortcut, skip to the next error, and then modify, Convenient and very.

To do this, you first define the program that is used at compile time, and for most projects using Makefile, VIM's default settings "make" are already available. If your project needs to be compiled with a special program, you need to modify the value of the ' makeprg ' option.

We have probably read the "Hello World" program when we are learning programming, and we take this simple example as an example to talk about the usage of the quickfix pattern.

The contents of the program are as follows, which contains three minor errors:

<pre>
/* Hello World demo
/#include <stdio.h "
int main (int argc, char **argv)
{
    int i;
  print ("Hello world\n");
    return 0;
} 
<\pre>

we can write a small makefile file for this program, but in order to demonstrate the MAKEPRG setup method, we do not use makefile, and directly set the MAKEPRG option, as follows:

<pre>
: Set makeprg=gcc\-wall\-o\ hello\ hello.c 
</pre>

The above command compiles hello.c into the executable file named Hello and opens all warnning. If there are spaces in the compilation command, you need to use ' \ ' to escape the space, and the example above uses the ' \ ' escape space.

Once we have set the MAKEPRG option, we can compile it by entering the following command:

We modified the MAIN.C, artificially causing several errors, adding a BAOBAO_WU without any defined string in line 1019th, deleting the last bracket in line 1020th, and compiling with the following command:

: Make

Obviously compiled will report a lot of errors, when the compile finished and exit to the source interface, just the compiler reported errors have not been seen, but we can use the Quickfix window to find out the error information, with the following command to pull out the Quickfix window:

: CW

You can now look at the Quickfix window as shown in the following figure:

When using: Make, VIM automatically invokes the commands defined by the MAKEPRG option to compile and redirects the compilation output to a temporary file, and when the compilation occurs, Vim reads the error message from the temporary file and forms a Quickfix list based on that information. and jump to where the first error occurred.

For our program above, the cursor stops at the third line, the first error position, and Vim prompts for an error message. If you do not see the error message, you can enter the ": CC" command, VIM will shepherds display this information, or simply use the ": CW" command, open a quickfix window, all the error message display.

Now we know where the mistake is, fix it, and then skip to the next error by using the ": CN" Command (or entering a carriage return on the Quickfix list), and so on, until all errors are corrected.

Well, after all the hardships, our Hello world has finally worked. At first glance, it seems that Quickfix does not improve the efficiency, but if your errors appear in different files in many different directories, it can save you a lot of time so you can focus on correcting bugs.

Vim can remember the latest 10 list of errors at the same time, which means that you have been using the last 10 times: the errors that you encounter with the make command compile are saved, and you can use: Colder and: Cnewer commands, back to the old error list, or to the updated error list.

The commands that are frequently used in Quickfix mode are:: CC display verbose error message (: HELP:CC): CP jumps to previous error (: HELP:CP): CN Skip to Next error (: HELP:CN): CL Lists all errors (: HELP:CL): CW If there is a list of errors, open the Quickfix window (: HELP:CW): Co L to previous list of old errors (: Help:col): Cnew to a later list of newer errors (: help:cnew)

More commands, as well as more detailed explanations of these commands, please refer to the manual.

For frequently used commands, it is best to provide a more convenient way of using the definition in my VIMRC:

In Vim7, each window can have its own list of locations so that you can open multiple location lists at the same time, and the Quickfix list is one of the whole vim. You can use the location list to display compilation error messages, specific commands refer to the manual:: Help Location-list and: Help:lmake.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.