Grep
Before :help :grep :help :make :help quickfix-window
Vim has two ways to find matches for a pattern: Internal and external. The advantage of the internal grep is that it works on all systems and uses the powerful Vim search patterns. An external grep program can be used when the Vim grep does not do what you want.
目的 We're going to create a "grep operator" and bind it to g. It will act like any other Vim operator and take a motion (like w or i{). It will perform the search immediately and open the quickfix window to show the results. It will not jump to the first result, because that can be jarring if the first result isn't what you're expecting.
Some examples of how you might end up using it:
giw: Grep for the word under the cursor.
giW: Grep for the WORD under the cursor.
gi': Grep for the contents of the single-quoted string you're currently in. viweg: Visually select a word, extend the selection to the end of the word after it, then grep for the selected text.
Exercises
Add the mapping we just created to your ~/.vimrc file.
Read :help :grep if you didn't read it before.
Read :help cword.
Read :help cnext and :help cprevious. Try them out after using your new grep mapping.
Set up mappings for :cnext and :cprevious to make it easier to quickly run through matches.
Read :help expand.
Read :help copen.
Add a height to the :copen command in the mapping we created to make sure the quickfix window is opened to whatever height you prefer.
Read :help silent.
Last updated
Was this helpful?