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

前端 未结 8 1324
无人共我
无人共我 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:13

    Have a look at "Grep search tools integration with Vim" and "Find in files within Vim". Basically vim provides these commands for searching files:

    :grep
    :lgrep
    :vimgrep
    :lvimgrep
    

    The articles feature more information regarding their usage.

    0 讨论(0)
  • 2021-02-02 17:14

    I highly recommend ack.vim over grep for this functionality.

    http://github.com/mileszs/ack.vim

    http://betterthangrep.com/

    0 讨论(0)
  • 2021-02-02 17:17

    I think this is what you are looking for:

    http://www.vim.org/scripts/script.php?script_id=2184

    When you open a file:line, for instance when coping and pasting from an error from your compiler (or grep output) vim tries to open a file with a colon in its name. With this little script in your plugins folder if the stuff after the colon is a number and a file exists with the name especified before the colon vim will open this file and take you to the line you wished in the first place.

    It's definitely what I was looking for.

    0 讨论(0)
  • 2021-02-02 17:19

    In this particular example:

    vim SearchToUser.java +170
    
    0 讨论(0)
  • 2021-02-02 17:31

    If you use git, results are often more meaningful when you search only in the files tracked by git. To open files at the given line which is a search result of git grep in vim you will need the fugitive plugin, then

    :copen
    :Ggrep pattern
    

    Will give you the list in a buffer and you can choose to open files from your git grep results.

    0 讨论(0)
  • 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).

    0 讨论(0)
提交回复
热议问题