pipe ripgrep search results to vim and open files at exact search location
Asked Answered
S

2

11
rg -l "searched phrase" | xargs vim

populates vim with searched terms, but I need to search for them once again, this time in vim.

How to pipe ripgrep search results to vim, so searched files will be opened at exact search location (line and column)?

Sochor answered 22/3, 2019 at 15:57 Comment(0)
C
17

According to Filling the quickfix window from stdin? you can do the following:

rg --vimgrep 'pat' | vim -q /dev/stdin

You need to supply --vimgrep to get ripgrep's output into the correct format for Vim. -q reads a file into the quickfix list.

Alternatively

Do your searching from inside Vim via :grep

Add the following to your vimrc file:

set grepprg=rg\ --vimgrep
set grepformat^=%f:%l:%c:%m

Now you can do :grep 'pattern' and it will populate the quickfix list. Redoing your search is as easy as :grep followed by some <up> presses to get back to your relevant :grep.

Quickfix commands

  • :cnext/:cprevious to navigate the quickfix forwards and backwards in the quickfix list
  • :cfirst/:clast to jump to the start and end of the quickfix list
  • Use :copen to open the quickfix window. Use <cr> to jump to an entry
  • Use :cclose to close the quickfix window
  • Use :cc to display the current error.
  • :colder/:cnewer to jump older/newer quickfix lists

I would recommend you create mappings for :cnext and :cprevious. I personally use unimpaired.vim which provides ]q & [q mappings for :cnext and :cprevious.

If you want the quickfix window to open automatically put the following in your vimrc:

augroup autoquickfix
    autocmd!
    autocmd QuickFixCmdPost [^l]* cwindow
    autocmd QuickFixCmdPost    l* lwindow
augroup END

There is a Vimcasts episode about this topic: Search multiple files with :vimgrep.

Project-wide Search and Replace

If you are using :grep/:vimgrep as way to do a project-wide search and replace then I suggest you use :cdo/:cfdo (in Vim 7.4.980+).

:grep 'foo'
:cfdo %s/foo/bar/g|w

For more help see:

:h :grep
:h 'grepprg'
:h quickfix
:h :cnext
:h :copen
:h :ccl
:h :cc
:h :colder
:h :cdo
:h :cfdo
Cutter answered 22/3, 2019 at 17:46 Comment(2)
in case someone else gets Vim: Warning: Input is not from a terminal, try vim -q <(rg --vimgrep 'pat') ... @Sochor in what way this doesn't answer? this seems like what you wanted based on your questionSkill
Second to vim -q <(rg --vimgrep ...), I like this syntax better than piping in stdin. This is also really handy for complex regexes that are difficult to work into Vim's idiosyncratic regex parsing.Ordeal
E
2

you can open vim with a command with the -c flag (see vim --help) It is easy to search with that:

rg -l "searched phrase" | xargs vim -c /searched phrase

But that will not jump to the exact position. A second -c will however assume that there is a second file open. So that we have to chain to jump behind the search:

rg -l "searched phrase" | xargs vim -c /searched phrase/norm n
Elvinelvina answered 22/3, 2019 at 17:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.