

Integrating The Silver Searcher with Vim's 'grepprg'
source link: https://blog.kiprosh.com/integrating-the-silver-searcher-with-vims-grepprg/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Integrating The Silver Searcher with Vim's 'grepprg' (without any plugins)
Vim has a command called :grep
(:help :grep
), which is used for searching plain text across the project. Internally it uses the *nix grep
utility to perform the search. However, the grep
utility is very slow, especially for larger projects, which means you might want to use something else like ag
(follow the instructions given in the official repo for installation).
Understanding :grep
When we execute let's say :grep text_to_find
in Vim, it will look at the value of the option called 'grepprg'
(:help 'grepprg'
), replace $*
with text_to_find
, and then pass it to the shell for execution. We can change the value of 'grepprg'
to use ag
instead of the default, which is grep
.
Changing 'grepprg'
Add the following to your .vimrc
file:
if executable('ag')
set grepprg=ag\ --vimgrep\ $*
set grepformat^=%f:%l:%c:%m
endif
Checking whether or not ag
is installed in the system (:help executable()
) before changing the value of 'grepprg'
will allow us to safely copy the configuration to any other system where ag
might not be available, without the fear of breaking Vim.
The --vimgrep
option passed to ag
makes it print the matches to the standard output in the %f:%l:%c:%m
format (:help errorformat
). Vim doesn't recognise this format by default, so we need to tell it explicitly. This is done using another option called 'grepformat'
(:help 'grepformat'
) which already has a couple of defaults. We just need to add our own format at the very beginning using the ^=
(:help :set^=
) assignment operator. The reason we add our format to the very beginning is because otherwise one of the default values (%f:%l:%m
) will match, resulting in Vim considering the column number and the actual match as part of %m
. We need Vim to recognise the column number separately so that it can use the information to place the cursor at the proper column while navigating the searches.
Navigating the searches
You can now use Vim's quickfix list (:help quickfix.txt
) to navigate your searches.
Recommend
-
143
The Silver Searcher A code searching tool similar to ack, with a focus on speed. Do you know C? Want to improve ag? I invite you to pair...
-
150
ag.vim Deprecated: See this comment for more info. Maybe try
-
157
helm-ag.el Introduction helm-ag.el provides interfaces of The Silver Searcher with helm. Features Support multiple sea...
-
140
ag.el Ag.el allows you to search using ag from inside Emacs. You can filter by file type, edit results inline, or find files. Ag.el tries very hard to be Do-What-I-Mean, and will make intelligent suggestions about what...
-
73
README.md _____ ____ ______ \__ \ / ___\/ ___/ / __ \_/ /_/ >___ \ (____ /\___ /____ > \//_____/ \/ Silver searcher (AG) plugin for Vim
-
90
README.md local-ehentai Local E-Hentai Gallery Index (with ~830k galleries' metadata)
-
72
In the previous article ( “Searching for a better indexOf” ) we studied various methods to search for a byte sequence in another byte s...
-
12
helm-ag.el Introduction helm-ag.el provides interfaces of The Silver Searcher with helm. Features Support multiple search tools(ag...
-
8
Introduction to Keywords via Understanding Searcher Psychology So far in this SEO for non-scumbags series (first post and index
-
7
Generate meta tags in secondsGenerate meta tags in some clicks and Rank top in search engines .You can generate Open graph, twitter and basic meta tags just in one click and a lot of adjustments in them.
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK