Shortcuts to "grep all open buffers" (open files), and search & replace on the results or the open files themselves.
- Searching takes too long?
- Maybe you're using a huge repo or fighting a slow connection?
- Vim search/replacing confusing?
- Using an old vim or fanciest plugins have errors?
- Want a few maps named to help you memorize the plain vim commands so you don't need any plugin?
When searching and replacing files takes too long (even when using rg), this plugin helps you search through only the files you have open.
Search Opened Files / Show Quickfix Window | Mapping | Note |
---|---|---|
Search buffers for the last thing you searched for | <leader>gbufs |
/ to search in page |
Search buffers for whatever is under the cursor † | <leader>gbufc |
Additional install |
Highlight, search page, then search buffers † | <leader>gbufs |
Additional install |
Search And Replace Multiple Files, use Macro q |
Mapping | Macro | Best For |
---|---|---|---|
Search And Replace all loaded files | <leader>gbufq |
qq:%s/OLD/NEW/g<ENTER>q |
convenience - no QF needed |
Search/Replace QuickFix files, full page | <leader>bigq |
qq:%s/OLD/NEW/g<ENTER>q |
big files/lots of results |
Search/Replace QuickFix files, line-by-line | <leader>finish |
qq:s//NEW/g<ENTER>q |
previewing a lot |
- load files in buffers
- load files in a quickfix list (optional)
- make a macro labelled
q
(egqq:%s/OLD/NEW/gENTERq
) \gbufq
to run the macro on all the files
There's more workflows, but these 3 are the most common I've found.
In this video:
- Load a bunch of files into vim buffers using pattern:
vim $( find . )
(rg is recommended instead of find) - Search for
get_
, don't find it in first file. - Search all open buffers using
\gbufs
. Results are displayed in the QuickFix screen. - Record macro
qq:s//set_/gjq
qq
- record macro namedq
:
- vim command modes//set_/g
- it's empty, so search for last search term (get_
), replace withset_
, globally for each lineq
- stop macro recording
n
around,@q
, then@@
to try the macro on different pages.\finish
to run the macro on all results found.
- Installation
- Prerequisites
- Learn real vim
- Long Instructions for Workflow -
finish
- More Workflows
- Simple search and view results -
gbufs
- A lot of searching/replacing of big files -
bigq
- Simple search and view results -
- Recommended Plugins instead of gbufs
- Recommended Plugins/Additions complimentary to gbufs
Plug 'marcopolo4k/vim-gbufs'
nnoremap <leader>gbufs :call BufdoVimgrepaddCopen()
nnoremap <leader>gbufq :MacroRplceBuffersWithQEntireFile <cr>
nnoremap <leader>bigq :MacroReplaceQuickFixWithQEntireFile<cr>
nnoremap <leader>finish :MacroRplceQckFxWithQOneLine <cr>
Plug 'marcopolo4k/vim-gbufs'
" ************** Gbufs - Searching **************
"
" Search all open buffers -- for the last thing you search for --
" Maybe your last search has word boundaries, or not. Check with '/↑'
nnoremap <leader>gbufs :call BufdoVimgrepaddCopen()
" 2nd option: Search and then open them all in horizontal window panes
nnoremap <leader>gbufa :call BufdoVimgrepaddCopenAndOpenThemAll()
" 3rd option: Type out a specific search term by command line
" :Gbufs SEARCH_TERM
" to learn real vim, this helps to memorize
nnoremap <leader>bufdovimgrep :call BufdoVimgrepaddCopen()
" Search all open buffers -- for what's under the cursor --
" No word boundaries default, depending on your config
" Requires either VSetSearch or SearchParty †‡
" Loads search result files into Quickfix List
" Does NOT save the search in last search reg
nnoremap <leader>gbufc :call GrepBuffersForWordOnCursor("<C-R><C-W>")<CR>
" *** Gbufs - Search And Replace with Macro Q ***
" Multiple Files at a time
"
" Replace in all buffers (does NOT use QuickFix list)
" [g]rep [buf]fers and replace with full-page macro [q]
nnoremap <leader>gbufq :MacroRplceBuffersWithQEntireFile <cr>
" to learn real vim, this helps to memorize
nnoremap <leader>bufdoq :MacroRplceBuffersWithQEntireFile<cr>
" cfdo - Macro Replace with Q - The QuickFix List - entire file at a time
" Runs fastest on large files
" since QF list should have less files than Buffer List
" Macro requires '%' to search entire file at a time
" qq:%s/OLD/NEW/gENTERq
"
nnoremap <leader>bigq :MacroReplaceQuickFixWithQEntireFile<cr>
" to learn real vim, this helps to memorize
nnoremap <leader>cfdoq :MacroReplaceQuickFixWithQEntireFile<cr>
" cdo - Macro Replace with Q - The QuickFix List - one line at a time
" macro q does NOT need the % in ':%s/TERM/REPLACE/g'
" macro q DOES need the TERM in ':s/TERM/REPLACE/g'
" This makes for the easiest previewing using '/'
"
" if already previewed a bunch of changes, just need to [finish]
nnoremap <leader>finish :MacroRplceQckFxWithQOneLine <cr>
" to learn real vim, this helps to memorize
nnoremap <leader>cdoq :MacroRplceQckFxWithQOneLine <cr>
†‡ EITHER Install Plugin OR add a function to ~/.vimrc
, choose one of these two:
VSetSearch
, see this stack convo- Plug dahu/SearchParty (choose option 2 for the global var)
- To do multi-file search/replacing in vim, you need to understand buffers, and preferably the quickfix window.
- See those highly recommended additions at the end of the installation section, those might almost be considered prereqs.
If you want to stop using this plugin, you can start with these mappings to help memorize the commands. It's just a lot of typing, and what do we think of unnecessary typing?
Search And Replace Multiple Files | Mapping | Macro | Best For |
---|---|---|---|
Search And Replace all loaded files | <leader>bufdoq |
qq:%s/OLD/NEW/g<ENTER>q |
Convenience - the QuickFix |
:bufdo execute "normal! @q" | w |
(AKA gbufq) | list is not needed | |
Search/Replace QuickFix files, full page | <leader>cfdoq |
qq:%s/OLD/NEW/g<ENTER>q |
Big Files - because it runs only |
:cfdo execute "normal! @q" | w |
(AKA bigq) | once per file found in QF list | |
Search/Replace QuickFix files, line-by-line | <leader>cdoq |
qq:s//NEW/g<ENTER>q |
Previewing a lot - use n and |
:cdo execute "normal! @q" | w |
(AKA finish) | @@ to test before doing all files |
|
Argdo runs on the original open file list | no macro yet | author of gbufs is lazy and | ? |
:argdo execute "normal! @q" | w |
hasn't needed this one yet |
-
Load files in buffers.
Choose one of:
- Inside vim,
:grep SEARCHTERM
populates the quickfix list. Downside is that it may take some time (seconds of your life!), especially if you don't use ripgrep. - Before starting vim, shell tools help load files. These do NOT populate the quickfix list. Couple examples:
- a repo search:
vim $( rg -l SEARCHTERM )
- all the files in current folder:
vim $( \ls )
- a repo search:
- Inside vim,
-
Populate the quickfix list (skip this step if you already loaded the exact files you want with
:grep SEARCHTERM
)Choose one of:
:Gbufs SEARCHTERM
- search with
/
, then\gbufs
† - cursor on word, then
\gbufc
‡
-
Make a macro labelled
q
(qq:s/OLD/NEW/g<ENTER>q
) cdo doesn't need a%
b/c it'll search each line individually -
The effects can be seen, file by file if desired, with vim commands:
@q
:n
/
etc- Search on a single buffer with
/
, hitn
a few times to look around. - Unless it exists, make macro
q
, likeqq:s//NEW/g<ENTER>j
- OLD is left empty since the last search is used by default.
- j goes to next line, in the interest of step v.
- Run
@q
to replace the line you're looking at. n
to find the next result,@@
to run macro on that.:n
to see the next page. Or better, use quickfix list to see all results. Repeat steps i & iii.- Maybe look around your buffer list if there's any file that might be different.
- Search on a single buffer with
-
When happy, run the macro on the rest of the files:
<leader>finish
See additional installation instructions †‡
- Highlight some text (
v
, then move cursor around everything to search for) *
<leader>gbufs
-
This does NOT add word boundaries in the search.
-
pro: Allows for full punctuation, eg around variables, sets of words and symbols, etc
-
pro: Faster to run the search.
-
con: Slower to select what you want & start searching.
- Put cursor on the word to search
*
to auto-search<leader>gbufs
-
This opens the Quickfix window
-
This adds word boundaries around the word, like:
\<SEARCHTERM\>
-
pro: Faster to start searching.
-
con: Slower to run the search.
Just searching and seeing something you might want to replace is common. If you're not ready to replace anything, it's fastest to search for the word without word boundaries. I use the default \
for my leader key.
ve
visual highlight from beginning to end of word, then*
. †\gbufs
to search current buffers for the term, it opens the Quickfix window.- Use window pane navigation to go to different files and see the usage.
Sometimes files are big or there's lots of results on each page, and you need the search itself to be fast. Instead of line by line, this searches each file in the Quickfix list, just once with one macro q
(vs finish
that executes the macro on every occurrance).
- Load buffers, let's choose
vim $( rg -l SEARCHTERM )
, with optional use ofrg
to preview which files you're getting. (rg is ripgrep) - Record macro:
qq:%s/SEARCH/REPLACE/gq
/SEARCH
on the first page, then preview the change:@q
, thenn
and@@
for more on the first page.- Load Quickfix list, let's choose
\gbufs
\bigq
to search/replace once on every file in the Quickfix list.
I could add functions to specifically open or NOT open the QuickFix preview window for any of these.
- vim-esearch is way more powerful, since
gbufs
is mostly just some simple maps. But I haven't been able to getesearch
to work on anything but vim9 yet, so for me at least,gbufs
will at least be used on old vim8 systems.gbufs
might also still be preferred if you want simple maps that help you memorize the full vim commands, or just don't want larger plugins. - If you use Neovim,
gbufs
works fine, but there's so many search and replace plugins (besidesesearch
, which is supposed to work on both):
- custom window navigation mappings in general
- ripgrep
- dahu/SearchParty - practically a requirement for
gbufs
, either this or VSetSearch - vim-abolish -
:%Subvert/facility/building/g
gives a bunch of capitalization variations - Subversive - for single-page search & replace, I have a bunch of mapping for that I almost put here. (I could add available upon request)
- FZF vim plugin
- Credit for searching - fandom.com-Search_on_all_opened_buffers