1

Dsecription
I am using :g/regex to do global search and it always list --More-- So I need to click Enter or Space to see the full results, it takes too much time.

I found one way to achieve my goal is to redir the result into a file then split the file
nnormap mm :redir! > output.txt \|silent! execute 'g/regex' \| redir END \|vsplit output.txt
This cmd really worked but it need to create file and if I do mutiple different :g/regex it will overide the previous results.

So I continue to improve and modify the cmd as:
:redir @a |silent! execute 'g/regex' | redir END |vsplit enew| put a
Remove the output.txt instead of the reg @a but still need to specify different reg to store my search results.

Question:
I want to achieve an easy and efficently way to put the different serach results into different split windows
Such as do the First :g/regex1 then put the results into split window1 then do the Second :g/regex2 put the results into the split window2
Is there any way to achieve and how can I modify the cmd?

1 Answer 1

2
  • You don't have to use a different register for each search because you put the current content of register a in each new buffer.
  • :vsplit enew splits the current window and creates a enew buffer. I don't think that's what you want. Use :vnew instead.
  • You should probably use scratch buffers, see :help special-buffers.

FWIW, I have been using a slightly more sophisticated variant of the same idea for years:

:Redir g/regexp

Note that it is written in a way that gives me a single sacrificial window but it could be simplified to use several windows. Here it is, with all the parts unrelated to your use case removed:

function! Redir(cmd)
    " redirect to a variable so registers are not polluted
    redir => output

    " execute given Ex comand
    execute a:cmd
    redir END

    " turn output into a list for use with :help setline()
    let output = split(output, "\n")

    " split a new window
    vnew

    " make it a scratch window
    setlocal buftype=nofile bufhidden=wipe nobuflisted noswapfile

    " dump output of given command in the buffer
    call setline(1, output)
endfunction

command! -nargs=1 -complete=command -range Redir silent call Redir(<q-args>)

If you like the idea, make sure to read the comments under the gist.

Another possibility would simply be to use :help :grep or :help :vimgrep to populate the :help quickfix list with the relevant lines. You didn't explain what you did with those searches but the quickfix lets you navigate to specific entries, jump between quickfix lists, and also do things like :cdo.

This is what I get with :vim command % | cw in my vimrc:

command

If I do a different search, say :vim nnoremap % | cw, I get a new quickfix list:

nnoremap

And I can pull up previous lists with :help :colder and friends:

:colder

Built-ins FTW!

1
  • yes, use :vnew instead :vsplit enew achieve my expectation. And your vim-scripts really helpful thanks a lot
    – M_Sea
    Nov 10, 2023 at 3:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.