sponsor Vim development Vim logo Vim Book Ad

intermediate Tip #483: Using GREP for a ‘list occurrences’ and quickfix help command.

 tip karma   Rating 1/1, Viewed by 1446 

created:   June 7, 2003 9:08      complexity:   intermediate
author:   Feral <[email protected]> (Rot13ed)      as of Vim:   6.0

This is inspired by VIMTIP#391: Simple programmers TODO list using grep and quickfix
taglist.vim is VIMSCRIPT#273

This is a little tip on shortcuts to make :grep just a little bit more handy.

:GREP is simply a front end to :grep which uses the current word under the cursor and the current file.
Use this for the times when you want a ‘list occurrences’ type search. (See also :h :ilist another method)

" [Feral:158/03@07:02] Easily GREP current word in current file.
command GREP    :execute 'grep '.expand('<cword>').' '.expand('%') | :copen | :cc

Certainly nothing fancy here.

Now given that continually typing :cnext, :cprev and :cc can get a bit cumbersome to type one might consider mappings to speed up the process. When such mappings call a user function we can get somewhat fancy as illustrated below.

"[Feral:314/02@19:33] Assign some keys for handy quickfix window commands.
if has("win32")
    nnoremap <kPlus>        :call <SID>Fancy_Quickfix_Cmd(':cnext')<cr>
    nnoremap <kMinus>        :call <SID>Fancy_Quickfix_Cmd(':cprev')<cr>
    nnoremap <kMultiply>    :call <SID>Fancy_Quickfix_Cmd(':cc')<cr>
    nnoremap <c-kPlus>    :clast<CR>
    nnoremap <c-kMinus>    :cfirst<CR>
    nnoremap <m-kPlus>    :cnewer<CR>
    nnoremap <m-kMinus>    :colder<CR>
endif



" [Feral:158/03@08:02] Very simple wrapper: do quickfix cmd, center line and
"    if taglist.vim's window is open sync
function s:Fancy_Quickfix_Cmd(Cmd)
    :try
        execute a:Cmd
    :catch /^Vim(\a\+):E553:/
        :echohl ErrorMsg | echo v:exception | echohl None
    :endtry

    :norm! zz

    " If the taglist window is open then :TlistSync
    "    Tag list window name: '__Tag_List__'
    if bufwinnr('__Tag_List__') != -1
        :TlistSync
    endif
endfunction

NOTE that s:Fancy_Quickfix_Cmd() uses VIM 6.2’s new try/catch commands; For previous versions just omit the try/catch … endtry lines. The function works well enough just is not as graceful when you reach the first or last of the error list. I.e. PRE 6.2 version:
function s:Fancy_Quickfix_Cmd(Cmd)
    execute a:Cmd
    :norm! zz
    " If the taglist window is open then :TlistSync
    "    Tag list window name: '__Tag_List__'
    if bufwinnr('__Tag_List__') != -1
        :TlistSync
    endif
endfunction

Just as an asside, if you did not want to use a user command for something like this you can do something like this:

nnoremap <kPlus>        :cnext<CR> :norm! zz<cr> :TlistSync<CR>
nnoremap <kMinus>        :cprev<CR> :norm! zz<cr> :TlistSync<CR>
nnoremap <kMultiply>    :cc<CR> :norm! zz<cr>

Of course if you do not have/want the syncing with taglist.vim simplly remove :TlistSync.


Now, the advantage of having s:Fancy_Quickfix_Cmd() is that we can conditionally do something based on the command, or some other attribute. In this case we function the same for all commands (and just blindly execute them). We do check to see if the taglist window is open and if so ask it to Sync, if it is not, we don't.  One other (minor, cosmetic) advantage to this is we see the user command in the echo area and not the last command executed (as with just the pure mapping method directly above).

You may need to modify the :GREP command so that your grep has the proper flags, etc. On win32 I have my grepprg set to "set grepprg=C:\Dev\bin\grep.exe\ -niH", fwiw.

Happy VIMing!

 rate this tip  Life Changing Helpful Unfulfilling 

<<Use VIM as an organizer of Memos, bookmarks, todo, etc. | Console-like fonts for Windows GVim >>

Additional Notes

Sreny at SverGbc.Pbz (Rot13ed), July 2, 2003 3:11
by the by, another way to ‘list occurrences’ (at least for the current file)
would be a :g//:p (see :h :g and :h :p) and have the text you want to list as
your current search pattern.

Just as an aside, :cnext can be abbreviated to :cn and :cprev can be
abbreviated to :cp, of course.
If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to [email protected] after searching the archive. Help Bram help Uganda.
Sponsored by Web Concept Group Inc. SourceForge Logo