VI/Vim advanced tutorial: quick fix

Source: Internet
Author: User

The help entry for the commands used in this section:

:help quickfix
:help :make
:help 'makeprg'
:help 'errorformat'
:help 'switchbuf'
:help location-list
:help grep
:help :vimgrep
:help :grep
:help starstar-wildcard

I used to read martial arts novels and saw that the martial arts masters had never left the sword. You can also use Vim to write programs ,:-)

Vim is developed by a programmer and used by more programmers. Therefore, the support for software development is enhanced in Vim. The introduction of quickfix mode is an example. The so-called quickfix mode has nothing to do with the normal mode and the insert mode. It is just a way to speed up your development.

The main idea of quickfix mode is to save a location list and provide a series of commands to redirect to this location list.

The generation of the location list can be obtained from the compilation and output information of the compiler, or from the output information of the grep command. The cscope command described in the previous article, you can also generate location list information (: Help 'cscopequickfix').

[Compile]

In general, we often need to write code, compile, and modify compilation errors during the development process. This process will be repeated dozens of times and hundreds of times. If you open an error file based on the error information output by the compiler, find the wrong row, and then start modifying it, the efficiency will not be too low.

Using the quickfix mode of VIM can greatly speed up this process. You can start the compilation in Vim, and then Vim will automatically jump to the first error location based on the error message output by the compiler, after modification, you can use a shortcut key to jump to the next error and modify it.

To achieve this, you must first define the program used during compilation. For most projects that use makefile, Vim's default settings"Make"The requirements can already be met. If your project needs to be compiled using a special program, you need to modify'Makeprg'Option value.

Everyone has probably read the "Hello World" program during programming. Let's take this simple example to explain the usage of the quickfix mode.

The program contains the following content, which contains three small errors:

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

We can write a small MAKEFILE file for this program, but to demonstrate'Makeprg'. Instead of using makefile, we can directly set'Makeprg'Option, as follows:

:set makeprg=gcc\ -Wall\ -ohello\ hello.c 

The above command will compile the hello. c executable file named hello and open all the warnning. If the compilation command contains spaces, use'\'The space is escaped, and the preceding example uses'\'Escape space.

We have set'Makeprg'Option, enter the following command to compile:

:make 

In use": Make", VIM will automatically call'Makeprg'COMMAND defined by the option to compile and redirect the compilation output to a temporary file. When an error occurs during compilation, VIM will read the error information from the temporary file, create a quickfix list based on the information and jump to the first error.

For the program above, the cursor will stop at the third line, that is, the first error location. Vim will also prompt an error message. If you do not see the error information, enter": CC"Command, VIM will display this information more times, or simply use": CW"Command, open a quickfix window and display all error information. See:

 

Now we know where the error is. correct it and then use": CN"Command (or enter the press enter on the corresponding line of quickfix List) to the next error, and so on until all errors are corrected.

Well, it's a great deal of work. Our Hello world is now working. Taking a look at this example, it seems that quickfix does not improve the efficiency, but if your error appears in different files in different directories, it can save you a lot of time, so that you can focus on fixing bugs.

Vim can remember the latest 10 error lists at the same time, that is, you have used the last 10 times": Make"All errors encountered during command compilation are saved and can be used": Colder"And": Cnewer"Command, return to the old error list or to the updated Error List.

Frequently Used commands in quickfix mode include:

: CC: detailed error message (: Help: CC)
: CP jump to the previous error (: Help: CP)
: CN jumps to the next error (: Help: CN)
: CL: list all errors (: Help: CL)
: CW if an error list exists, open the quickfix window (: Help: CW)
: Col to the previous old Error List (: Help: COL)
: Cnew to a newer Error List (: Help: cnew)

For more commands and more detailed explanations of these commands, see the manual.

For frequently-used commands, it is best to provide more convenient use methods, defined in my vimrc:

autocmd FileType c,cpp  map <buffer> <leader><space> :w<cr>:make<cr>
nmap <leader>cn :cn<cr>
nmap <leader>cp :cp<cr>
nmap <leader>cw :cw 10<cr>

Use now", <Space>"(Press first, then press space) to compile and use", CP"And", CN"Skip to the previous and next errors, use", CW"To open a quickfix window. This is a great deal!

If you want to open an error file in a separate window, see'Switchbuf'Option value.

In vim7, each window can have its own location list, so that you can open multiple location lists at the same time, and the quickfix list has only one in Vim. You can use the location list to display the compilation error information. For more information about the commands, see the manual :": Help location-listAnd": Help: lmake".

  

[Grep]

As mentioned in CSP, a tool for programmers, CSP can be used as a fast grep program. For our software projects, CSP can be used to generate a database, which can greatly speed up searching strings. However, cssag needs to generate a database in advance. For some simple searches, it does not need to generate a database specifically. grep can be used at this time.

Grep is named "g/RE/P", "re" indicates a regular expression (RegEx), and "p" indicates printing, that is, print the rows matching the regular expression.

Vim can use both external grep programs and internal integrated grep functions.

The integrated grep command is very simple and usually used in the following format:

:vimgrep /main/gj **/*.c 

In the above example, we use the grep function integrated in VIM to search for the main string in all the C files in the current directory and Its subdirectory tree. If one line of main appears multiple times, each match is included. After finding it, the first match is not immediately redirected.

Using the grep function of internal integration is slower than external grep, because it will open each file, check it, and then close it; but the integrated grep supports Vim enhanced regular expressions, you can use it for more complex searches. It also supports the file wildcard representation of VIM extensions. See": Help starstar-Wildcard".

The results found by vimgrep will also be placed in the quickfix list. Is the quickfix list generated by executing the above command in the vim 7.0 source code directory:

 

We can use the commands in quickfix mode described above to view the matching results.

You can also use an external grep program to search for it. If your system is not using a standard grep program, you need to modify'Grepprg'Option. For more information, see the manual.

The external grep syntax is the same as that of the grep program. For more information, see the grep manual.

Whether using the internal vimgrep or the external grep, VIM allows you to place the searched results in the list of locations associated with the window. For more information ,": Help: lvimgrep"And": Help: lgrep".

In my vimrc, define the following key ing and use it to quickly find words under the cursor in the current file:

nmap <leader>lv :lv /<c-r>=expand("<cword>")<cr>/ %<cr>:lw<cr> 

  

[Reference]

  • Vim Manual
  • Vim Chinese manual

 

Yishui blog [http://easwy.com/blog/]

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.