Vim: open files of the matches on the lines given by Grep?

前端 未结 8 1423
无人共我
无人共我 2021-02-02 16:46

I want to get automatically to the positions of the results in Vim after grepping, on command line. Is there such feature?

Files to open in Vim on the lines give

8条回答
  •  忘了有多久
    2021-02-02 17:33

    You could do this:

    % vim "+/checkWordInFile" $(grep -l checkWordInFile *)
    

    This will put in the vim command line a list of all the files that match the regex. The "+/..." option will tell vim to search from the start of each file until it finds the first line that matches the regex.

    Correction:

    The +/... option will only search the first file for the regex. To search in every file you need this:

    % vim "-c bufdo /checkWordInFile" $(grep -l checkWordInFile *)
    

    If this is something you need to do often you could write a bash function so that you only need to specify the regex once (assuming that the regex is valid for both grep and vim).

提交回复
热议问题