Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vimgrep pattern and immediately open quickfix in split mode

Tags:

vim

Not sure if this has been ask, if so I can't find it.

I want to run vimgrep to search for a pattern. I've been doing this. And when it find a match, it seems to open up the first file right away.

This is not what I want. I want the quickfix window to be open in horizontal pane split for me to navigate through the matches. This is my command in vimrc

command! -nargs=1 Ngrep vimgrep "<args>" **/*.md

like image 554
kirikoumath Avatar asked Aug 18 '16 04:08

kirikoumath


People also ask

What is Vimgrep?

vimgrep is Vim's built-in command for searching across multiple files. It's not so fast as external tools like ack and git-grep, but it has its uses. vimgrep uses Vim's built-in regex engine, so you can reuse the patterns that work with Vim's standard search command.

What is Vim quickfix?

The quickfix and location lists provide a powerful way to navigate in Vim especially in the context of searching a file or project, and when inspecting errors. Essentially, they are lists of file locations with a built-in set of commands for navigating between them, and they can be populated in a variety of ways.


1 Answers

Add this snippet to your vimrc to tell Vim to automatically open the location/quickfix window after :make, :grep, :lvimgrep and friends if there are valid locations/errors:

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

If you don't want Vim to jump to the first match, use the j flag:

:vimgrep /foo/j **/*.md

as explained in :help :vimgrep.

like image 189
romainl Avatar answered Nov 06 '22 11:11

romainl