When I run grep "keyword" -n and get the following list of results:

a/b/c:10:    keyword
a/b/c:70:    keyword
a/b/d:50:    keyword

How can I open one of the files (say the 2nd in list) in the line it found?

I now just copy the the output using my mouse, and copy it after vim and then add + with the line number I copy. (meaning I write vim a/b/c +70 using the mouse copy to get the file name, and another mouse copy to get the line number [or I just copy it by hand, when its short enough])

Is there a way to do it with a keyboard shortcut?

  • You can try something like that: echo a/b/c:70: keyword | awk '{print $1}' | sed 's,:$,,' | sed 's,:, +,' | xargs vim && reset. Dec 11, 2018 at 12:48
  • 2
    Also, if you're interested in using Vim more efficiently, do check out the dedicated Vi and Vim Stack Exchange site.
    – muru
    Dec 11, 2018 at 12:49

3 Answers 3


Two things:

  1. Vim has some support for grep.

    If you open Vim, and do :grep keyword ..., Vim populates the quickfix list with the results, and jumps to the first file. You can then jump to the nth quickfix entry with :cc n (and other commands).

  2. You can populate the aforementioned quickfix list using grep's output:

    vim -q <(grep -n keyword ...)

    And then use the quickfix navigation commands mentioned above.

Either is simpler than playing around with grep's output manually.

As an alternative to (2), you can save grep's output to a file and use that file instead, if you think you won't necessarily open Vim after:

grep ... | tee log
vim -q log
  • Trying to use the same method on git status, to no avail :( - vim -q <(git status | grep modified) Dec 11, 2018 at 13:39
  • 3
    @CIsForCookies That won't be in the same format as grep -n (<filename>:<line>: ...). I use the fugitive plugin for Git, and then it's a matter of :Gstatus, move to desired file and press Enter.
    – muru
    Dec 11, 2018 at 13:43
  • BTW, I edited my ~/.bashrc grep alias to use colors always, and that, for some reason, broke the -q >() option... Dec 13, 2018 at 19:01
  • @ClsForCookies you can disable the colors (even if they are set in the alias as you said) using --colors=never option. Taking @muru 's example, it would be something like: vim -q <(grep --colors=never -n keyword ...)
    – Baumann
    Apr 14, 2021 at 20:56

You could make is so if there were not support for grep already as @muru answered:

:cexpr system("grep -n keyword")

It can be used with another command, git grep for example.

Also, you can open the output in a buffer and use "cbuffer" on it.

See quickfix section from the manual about it.


Pipe the output of grep to vim and use the gF family of motions to move to the file under the cursor.

grep "keyword" -rn | vim -

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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