M-x occur for vim

Emacs has a pretty great feature called “Occur” and to use it you do the ol’ M-x occur. But I’m a Vim user, and so this is the solution I’ve come up with:

nmap g/ :vimgrep /<C-R>//j %<CR>\|:cw<CR>

So my work flow is as follows:

  1. I search for what I’m trying to find with the usual /my query<CR>.

  2. If I didn’t hit what I was looking for then I hit g/ and find the line containing what I’m looking for and hit to be taken there.

If this isn’t your work flow, you could create a function that takes your query as input and uses that as your pattern instead of what you last searched for (the <C-R>/ part). Or if you’re not a fan of the QuickFix buffer, what I previously used was nmap g/ global//print, but I feel what I have now is superior.

Follow me on Twitter to keep up with what I’ve learned building my personal finance tool, Stash.

Read more posts …
Written: October 31st, 2011
Categories: vim, emacs, grep