Practical Vim< /a> vit Practical Vim< /a> Note that the styling for a Visual selection is the... development I thought I knew Vim, but Practical" name="description"/>
Tải bản đầy đủ - 0 (trang)
Tip 105. Browse the Quickfix List

Tip 105. Browse the Quickfix List

Tải bản đầy đủ - 0trang

Browse the Quickfix List




Jump to next item


Jump to previous item


Jump to first item


Jump to last item


Jump to first item in next file


Jump to last item in previous file

:cc N

Jump to nth item


Open the quickfix window


Close the quickfix window

• 263

Table 28—Commandsfor Navigating the Quickfix List

Meet the Location List

For every command that populates the quickfix list, there’s a variant that places the

results in a location list instead. While :make, :grep, and :vimgrep use the quickfix list,

:lmake, :lgrep, and :lvimgrep use the location list. So what’s the difference? At any given

moment, there can be only one quickfix list, but we can create as many location lists

as we want.

Suppose that we’ve followed the steps in Tip 107, on page 265, so that running :make

in a JavaScript file passes the contents of the file through JSLint. Now let’s say that

we’ve got two different JavaScript files open in split windows. We run :lmake to compile

the contents of the active window, which saves any error messages to the location

list. Then we switch to the other window and run :lmake again. Rather than overwriting

the existing location list, Vim creates a new one. Now we have two location lists, each

containing errors for a different JavaScript file.

Any commands that interact with a location list (:lnext, :lprev, and so on) will act on

the list that is bound to the currently active window. Compare this with the quickfix

list, which is available globally throughout Vim: no matter which tab page or window

is active, when you run :copen, the quickfix window will show the same list.

Basic Quickfix Motions

We can iterate forward and backward through the items in the quickfix list

with the :cnext and :cprevious commands. If we want to skip to the start or end

of the list, we can do so with the :cfirst and :clast commands. We’ll use these

four commands a lot, so it’s a good idea to map them to something easier to

reach. See Create Mappings to Quickly Traverse Vim's Lists, on page 79, for a


report erratum • discuss

Chapter 17. Compile Code and Navigate Errors with the Quickfix List

• 264

Quickfix Fast Forward/Rewind

Both :cnext and :cprev can be prepended with a count. So instead of stepping

through every item in the quickfix list one by one, we could skip through

them five at a time by running this:


Suppose that we’re browsing through the quickfix list and we come upon a

file with dozens of matches, none of which are of much interest to us. In this

scenario, rather than stepping through the results one at a time (or even ten

at a time), it would be handy if we could just skip all of the results in that file

by jumping to the first record in the next file. That’s what the :cnfile command

does. As you’d expect, the :cpfile does the same in reverse, jumping to the last

quickfix record in the previous file.

Use the Quickfix Window

We can open a window containing the contents of the quickfix list by running

:copen. In some ways, this window behaves like a regular Vim buffer. We can

scroll up and down with the k and j keys, and we can even use Vim’s search

feature on the contents of the quickfix list.

The quickfix window has special behavior of its own. If we position our cursor

on it and press the key, then that file will be opened with our cursor

positioned on the line containing the match. The file usually opens in the

window directly above the quickfix window, but if the file is already open in

a window in the current tab page, then that buffer will be reused.

Note that each line in the quickfix window corresponds to a record in the

quickfix list. If we run :cnext, then the cursor position will move down one line

in the quickfix window, even when that window is not active. Conversely, if

we use the quickfix window to jump to an item in the quickfix list, then the

next time we run :cnext we’ll go to the item after the one we selected in the

quickfix window. Selecting an item from the quickfix window is much like

running the :cc [nr] command but with an intuitive visual interface.

We can close the quickfix window as usual by running :q when the window

is active. But we can also close it by running :cclose when any other window

is active.

report erratum • discuss

Recall Results from a Previous Quickfix List

• 265

Tip 106

Recall Results from a Previous Quickfix List

When we update the quickfix list, Vim doesn’t overwrite the previous contents.

It saves the results of older quickfix lists, allowing us to refer back to them.

We can recall an older version of the quickfix list (Vim holds onto the last ten

lists) by running the :colder command (sadly, there is no :warmer command).

To revert from an old quickfix list back to a newer one, we run :cnewer. Note

that both :colder and :cnewer commands can accept a count, which causes the

respective command to be run the specified number of times.

If we open the quickfix window after running :colder or :cnewer, its status line

will indicate the command that was run to generate that particular list.

You can think of the :colder and :cnewer commands as being like undo and redo

for the quickfix list. That means it’s cheap for us to try out commands that

repopulate the quickfix list because we can always run :colder to revert to the

previous list. Also, instead of repeating a :make or :grep command, we could

pull up the results from the last time it was executed (unless we’ve changed

any files). This can be a real time saver, especially if the command takes a

long time to run.

Tip 107

Customize the External Compiler

Vim’s :make command isn’t limited to calling the external make program; it can

execute any compilers available on your machine. (Note that Vim’s definition

of “compiler” is looser than what you may be used to; see ‘:compiler’ and ‘:make’

Are Not Just for Compiled Languages, on page 268.) In this tip, we’ll set up the

:make command so that it passes a JavaScript file through JSLint and then uses

the output to populate the quickfix list.

report erratum • discuss

Chapter 17. Compile Code and Navigate Errors with the Quickfix List

• 266

First, we’ll configure Vim so that running :make calls nodelint,1 a commandline interface to JSLint.2 It depends on Node.js and can be installed using

NPM simply by running this:3

$ npm install nodelint -g

As a test case, we’ll use this JavaScript implementation of FizzBuzz:


var i;

for (i=1; i <= 100; i++) {

if(i % 15 == 0) {


} else if(i % 5 == 0) {


} else if(i % 3 == 0) {


} else {




Configure ‘:make’ to Invoke Nodelint

The ‘makeprg’ setting allows us to specify the program that will be called when

we run :make (see :h 'makeprg' ). We can instruct Vim to run nodelint as follows:

:setlocal makeprg=NODE_DISABLE_COLORS=1\ nodelint\ %

The % symbol is expanded to the path for the current file. So if we were editing

a file called ~/quickfix/fizzbuzz.js, then running :make inside Vim would be equivalent to running this in the shell:


$ nodelint ~/quickfix/fizzbuzz.js

~/quickfix/fizzbuzz.js, line 2, character 22:

for (i=1; i <= 100; i++) {

~/quickfix/fizzbuzz.js, line 3, character 15:

if(i % 15 == 0) {

~/quickfix/fizzbuzz.js, line 5, character 21:

} else if(i % 5 == 0) {

~/quickfix/fizzbuzz.js, line 7, character 21:

} else if(i % 3 == 0) {

~/quickfix/fizzbuzz.js, line 12, character 2:


5 errors




Unexpected '++'.

Expected '===' ...

Expected '===' ...

Expected '===' ...

Unexpected ';'. and, respectively.

report erratum • discuss

Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tip 105. Browse the Quickfix List

Tải bản đầy đủ ngay(0 tr)