Vim searching through all existing buffers

前端 未结 8 563
刺人心
刺人心 2021-02-01 13:23

When dealing with a single file, I\'m used to:

/blah
do some work
n
do some work
n
do some work

Suppose now I want to search for some pattern o

相关标签:
8条回答
  • 2021-02-01 14:07

    I don't believe it's possible to extend the 'n' functionanly across files or buffers. You could use

    :grep blah *
    

    And then do

    :cn
    

    To move to the next line with blah on it. That will switch between files but it's not quite as neat. It's more cumbersome to type the colon all the time, and it will only take you to the line, not the word.

    What I usually do is either to open the files I want to searched in tabs and then use 'n' and 'gt' to jump to next tab when I reach the end of the file, or list the files on the command line to I can skip to the next file with ':wn' when I'm done editing it.

    Hope it helps!

    0 讨论(0)
  • 2021-02-01 14:10

    We can do this using vimgrep and searching across the argslist. But first let's populate our argslist with all our buffers:

    :bufdo :args ## %
    

    Now we can search in our argslist

    :vimgrep /blah/ ##
    

    Where % == the current filepath and ## == the arglist.

    I recommend watching these vimcasts if you want to learn more: Populate the arglist, Search multiple files with vimgrep

    0 讨论(0)
  • 2021-02-01 14:13

    I would open all the buffers in a new tab using the following two commands:

    :tab sp
    :bufdo sp
    

    Then search through each file one by one and close its window when you are done (use :q or :close). Use CTRL+W_ to maximize each window as you are working in it. When you're finished and you close the last window, the tab page will close and you'll be dropped back wherever you were before you decided to do the search.

    0 讨论(0)
  • Another approach:

    :call setqflist([])           " clear quickfix list
    :silent bufdo grepadd! foo %  " edit foo in command-line history window
    :cw                           " view search results
    

    Or mapped:

    cmap bbb call setqflist([]) \| silent bufdo grepadd!  %<C-F>$hha
    
    0 讨论(0)
  • 2021-02-01 14:16

    Anytime you want to switch to another buffer try this.

    :b + <any part of file in buffer> + tab
    

    For an example. I have this in my buffer

    77 "/var/www/html/TopMenuAlertAlert.vue" line 65
    78 "/var/www/html/MainSidebar.vue" line 29
    79 "/var/www/html/FullScreenSearch.vue" line 26
    80 "/var/www/html/Menu.vue" line 93
    81 "/var/www/html/layouts/RightSidebar.vue" line 195

    As I want to change to another buffer, I probably remember some detail about the file like 'Alert'

    So I just go

    :b Alert + tab
    

    if the file given is not the one I want, I just keep on pressing tab.

    Vim will keep on giving the next file close to it.

    Once you got it. Press Enter.

    0 讨论(0)
  • 2021-02-01 14:21

    Use the bufdo command.

    :bufdo command
    

    :bufdo command is roughly equivalent to iterating over each buffer and executing command. For example, let's say you want to do a find and replace throughout all buffers:

    :bufdo! %s/FIND/REPLACE/g
    

    Or let's say we want to delete all lines of text that match the regex "SQL" from all buffers:

    :bufdo! g/SQL/del
    

    Or maybe we want to set the file encoding to UTF-8 on all the buffers:

    :bufdo! set fenc=utf-8
    

    The above can be extrapolated for Windows (:windo), Tabs (:tabdo), and arguments (:argdo). See help on :bufdo for more information.

    0 讨论(0)
提交回复
热议问题