Searching in Vim

vim
Author

Arumoy Shome

Published

May 13, 2019

Abstract
Various strategies for searching text within vim.

Customising the grep program

The grepprg and grepformat options allows us to specify a grep command and output format of out liking. I like to use ripgrep as an alternative to grep since it ignores dotfiles and binaries by default.

~/.vimrc
if executable('rg')
    set grepprg=rg\ --vimgrep\ --no-heading\ --smart-case
    set grepformat=%f:%l:%c:%m,%f:%l:%m
endif

Tiny downside which bugs me with the above two solutions is that every search takes away focus from the active splits and shows the search results in a temporary buffer before populating the quickfix list.

Finally, the vim-fugitive plugin provides a :Ggrep <pattern> <glob> command which uses git-grep under the hood. The upside is that only files tracked by git are searched.

Personally, I like to keep external dependencies to a minimum. Since vim-fugitive is pretty much a must have, I prefer a combination of modifying the grepprg to ignore files and directories specified by wildignore and :Ggrep.

Back to top