Quickfix list

You've already had a brush with a quickfix window in Chapter 2Advanced Editing and Navigation, but let's dig a bit deeper into it.

Vim has an additional mode that makes jumping to certain parts of files easier. Some Vim commands use it to navigate between positions in files, such as jumping to compile errors for :make or search terms for :grep or :vimgrep. Plugins such as linters (syntax checking) or test runners use the quickfix list as well.

Let's try using a quickfix list by running a :grep command to search for the animal keyword recursively (-r) in every Python file (--include="*.py"), starting in the current directory (.):

:grep -r --include="*.py" animal .

This will open the first match in a current window. To open a quickfix window and see all of the matches, execute the following:

:copen

You can see the results a horizontal split now:

You can navigate the quickfix window as usual with k and j keys to move up and down, Ctrl + and Ctrl + b to scroll by pages, and / and ? to search forward and backward. Enter will open a file with a match in the buffer you were searching from. It will also place your cursor in the desired position.

For example, if you wanted to open a match in a file, animals/sheep.py, you can navigate to the desired line by running /sheep, followed by n (next) until the cursor is at the right line, and pressing Enter. The file will be opened in the original window with the cursor located where the match is:

You can close the quickfix list with :cclose (or :bd to delete the quickfix buffer if it's in an active window).

You can also navigate the quickfix list without opening the quickfix window:

  • :cnext (or :cn) navigates to the next entry in the quickfix list
  • :cprevious (or :cp, :cN) navigates to the previous entry in the list

Lastly, you can choose to only open the quickfix window if errors (such as compile errors) are found: :cwindow (or :cw) will toggle the quickfix window only if errors are present.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.2.15