9

I'm reading through a large C++ code base in Vim.

Within a single file, I can do

/foo
n
n
n

Now, if I want to search through more than one file, I have to do:

:vimgrep /foo/
:cn
:cn
:cn

Now, typing :cn is so much less convenient than n. Is there a way to search through vimgrep results with n (like searches with /) instead of :cn?

Keith Pinson
  • 7,835
  • 7
  • 61
  • 104
anon
  • 41,035
  • 53
  • 197
  • 293
  • Hitting `.` usually repeats the last command. – dirkgently Feb 06 '10 at 11:04
  • 2
    @dirkgently The last edit command, I believe. Isn't there a different key for repeating the last ex command? – Keith Pinson Feb 05 '13 at 20:24
  • 2
    @Kazark Actually yes, the `@:` command repeats the last ex command. – glts Feb 05 '13 at 21:16
  • @glts Right---thanks. Which, sadly, is barely shorter than `:cn`; but one character could add up over time if you got very used to hitting the `@` key. – Keith Pinson Feb 05 '13 at 21:19
  • 1
    @Kazark Right. In some cases, however, I need to repeat the last ex command *again* (after `@:`) and then I can do `@@` which is marginally more efficient than typing `@:` again. – glts Feb 05 '13 at 21:22

3 Answers3

9

Use the Quickfix List. It will automatically be filled with found matches (no matter if you use :grep or :vimgrep). It can be navigated with the usual keys (so the key for "next" is j instead of n).

To open it use :copen.

Benjamin Bannier
  • 55,163
  • 11
  • 60
  • 80
7

This is what I have in my .vimrc exactly for this purpose:

nmap <F7> :cp^M
nmap <F8> :cn^M
Michael Krelin - hacker
  • 138,757
  • 24
  • 193
  • 173
1

Just another option: the vim-unimpaired plugin binds ]q to :cnext and [q to :cNext among many other useful keymappings.

P Varga
  • 19,174
  • 12
  • 70
  • 108