sponsor Vim development Vim logo Vim Book Ad

ag-search : search with ag/grep in background, save/load searches on quickfix

 script karma  Rating 0/0, Downloaded by 314  Comments, bugs, improvements  Vim wiki

created by
Javier Puigdevall
script type
ag-search allows you to launch ag (silver searcher) or grep commands in background and display the results on quickfix window.

Launch :Agh for abridged help and examples.

ag-search is a plugin to:
- Launch a search in background (Ags path pattern [options]).
- Concatenate searches (:Ags pattern1,pattern2,pattern3 path [options]).
- Concatenate searches. Remove not matching pattern2 (:Ags pattern1,-pattern2 path [options]).
- Launch again a previous search (:AgS)
- Save/load/open/delete saved search results (:Agsv, :Agl, :Agd, :AgD, :Ago).
- Automatically save last N searches performed.
- Retrieve search information of the last searches performed (:Agi, :Agic).
- Create custom mappings and abbreviations to launch user searches more easily.
- Force search with context (:Agc, :Agc 4, :Agc 2 4).
- Fold/unfold the context on the quickfix window (:Agf)

This plugin uses jobs.vim to launch the search in backgraund.
Use command :Jobsl to show information about your searches running in backgraund.
Use command :Jobsk to kill any searche running in backgraund.

Default mappings:
<leader>f : search word under cursor (or visual selection) in current buffer.
<leader>d : search word under cursor (or visual selection) in current buffer file's directory.
<leader>p : search word under cursor (or visual selection) in current buffer file's parent directory.
<leader>w : search word under cursor (or visual selection) in working directory.

Default abbreviations:
_agf : search in current buffer.
_agd : search in current buffer's directory.
_agp : search in current buffer's previous directory.
_agw : search in working directory.

The doc page is still pending meanwhile you can check :Agh for an abridged command help and examples:

Any feedback will be welcome.

Ags command options:
-ID=path1,path2         : ignore (comma separed) directories.
-IF=pattern1,pattern2 : ignore (comma separed) patterns.
-RP=pattern               : ask user to replace on the provided path any words on g:AgSearch_defaultReplacePatterns (Defaults: _DIR_, _DIR1_, _DIR2..., _FILE_).

Customizations you can add to your .vimrc file.

" Fold/unfold the context on quickfix window:
nnoremap <Leader>f     :Agf<CR>

Create mapping to search on a folder projects, ask user to replace _DIR_ with the directory name or use wildcard * to search all directories:
nnoremap <Leader>as :Ags /home/jp/projects/_DIR_/source/ <C-R>="-s ".expand('<cword>')<CR>

Create command :Agss, map <leader>as and abbreviation _ags to search on directory sources, inside directory projets, and ask user to replace _DIR_ with a directory name (project name) or wildcard (like: *) to search all directories (projects):
" Remove previous user commands:
call agsearch#AddUserCmdMapAbbrev( { 'reset':1 } )
" Add new user commands:
call agsearch#AddUserCmdMapAbbrev( { 'default':"s", 'path':'/home/jp/projects/_DIR_/source/', 'help':"Search source dir on projects" } )
call agsearch#AddUserCmdMapAbbrev( { 'default':"c", 'path':'./home/jp/config/myconfig/', 'help':"Search config dir" } )

install details
Minimum version: Vim 7.0+
Recomended version: Vim 8.0+

Install vimball:
download agsearch_0.0.3.vmb
vim agsearch_0.0.3.vmb
:so %

rate this script Life Changing Helpful Unfulfilling 
script versions (upload new version)

Click on the package to download.

package script version date Vim version user release notes
agsearch_0.0.3.vmb 0.0.3 2022-01-28 7.0 Javier Puigdevall - New: use path __FILES__ to search in all files open on current vim session.
   Use __FILES:pattern__ to search only files matching pattern.
   Use __FILES:--pattern__ to search only files not matching pattern.
- New: AgF command, _agF abbreviation, <leader>aF map. to search all files
  open on current vim session.
- Remove: Agb command (_agb abbreviation, <leader>ab mapping) removed.
agsearch_0.0.2.vmb 0.0.2 2022-01-11 7.0 Javier Puigdevall Fix: auto save files rotation. Not deleting old files.
agsearch_0.0.1.vmb 0.0.1 2021-12-23 7.0 Javier Puigdevall Initial upload
ip used for rating:

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 the maillist. Help Bram help Uganda.