vimgrep and copen

This morning Identi.ca helped me learn something new about Vim; vimgrep. Granted, most, if not all, of these micro posts only point you in the right direction, then you need to actively go and search the knowledge out, which I did. These two pages in particular helped me out understanding how it works.

$ echo "# TODO:" > test1.py
$ echo "# TODO:" > test2.py
$ gvim

This just creates two files containing a “TODO” comment (i.e. something we wish to search for later), and starts up an instance of gvim. Then, inside gvim:

:vimgrep /TODO/gj ./*.py
:copen

This will search through all files in the current directory with the .py extension, looking for “TODO” and add the results to a list, which, if I have understood everything correctly, is called the quickfix list. :copen opens that quickfix list.

The result will be that the vim window is split in two, the upper window showing nothing (as we have yet to select one of the files listed in the window below), and the lower window listing all files matching the search.

The lower window will have focus, so just navigate to a file you wish to work with and hit enter. The file will open in the upper window, and that window will have focus now.

This is when I sat down and took the time to learn about “C^w k“, “C^w j” and “C^w o“. The first two are navigational, jumping up (k) and down (j) between the windows, while “C^w o” will close all but the window with focus. Nifty!

Tags: , , , , ,

Comments are closed.