Selecting resulting files from grep in vim

前端 未结 4 1787
轻奢々
轻奢々 2021-02-02 01:21

After I run a grep search in vim with :grep, I get a list of files. Is there a way to select one of those files and open it in a new tab at that particular line?

相关标签:
4条回答
  • 2021-02-02 01:48

    Just for completeness, as well as the :copen command, there's also :cw, which only opens the "quickfix" window if there are entries (so if your grep has no results, it won't appear).

    I think the easiest way (without defining a mapping) of making the files open in a new tab would be to do:

    :cw                " Open the quickfix window
    Ctrl-W T           " Switch the window into a new tab
    <ENTER>            " Open the file/line
    

    Alternatively, you could do:

    :cw                " Open the quick fix window
    Ctrl-W <ENTER>     " Open the file/line in a new window
    Ctrl-W T           " Move the new window to a new tab
    

    If you want to do it by default, you could probably use the BufEnter and BufLeave autocmds to create and remove a mapping when entering and leaving the quickfix window; however, this is probably not trivial.

    :help :cw
    :help :copen
    :help quickfix
    
    0 讨论(0)
  • 2021-02-02 01:50

    If you get a list of files you can browse them in a tree-like manner via

    :cn
    :colder
    

    For more information

    :help grep
    

    and scroll to the bottom of the entry

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

    I came upon this thread looking for an answer to a very similar question. The answer presented above, though correct, failed to describe a convenient way to open ALL the files in the QuickFix window at once ... into either buffers or tabs.

    There doesn't seem to be a built in command to do it, but it's trivial as a VIM plugin ... somebody has done it here http://pastebin.com/J9RwciFQ

    It's 12 lines of code (one function) ... pasted here to save you a click during your analysis. Do follow the pastebin link if you are going to try to implement this though ... my plugin is installed in pathogen directory and I modified the plugin from the original slightly (details after code).

    ~/.v/b/v/p/quickfixopenall.vim
    " Create command
    command! QuickFixOpenAll :call StartQuickFixOpenAll()
    
    function!  StartQuickFixOpenAll()
        if empty(getqflist())
            return
        endif
        let s:prev_val = ""
        for d in getqflist()
            let s:curr_val = bufname(d.bufnr)
            if (s:curr_val != s:prev_val)
                exec "edit " . s:curr_val
            endif
            let s:prev_val = s:curr_val
        endfor
    endfunction
    

    So once I have a grep result I'm satisfied with ... the plugin has a function :QuickFixOpenAll ... I had to modify the plugin as given (added the following line to the quickfixplugin.vim). And I renamed his given function StartQuickFixOpenAll ...

     " Create command
     command! QuickFixOpenAll :call StartQuickFixOpenAll()
    

    Then you have all the files in the grep result open as buffers ... if you want to run any commeon operations such as find/replace you can prefix the regular command with the "bufdo" command which will perform your command in all ... in VIM type "help bufdo"

    You can fairly trivially modify this plugin if you wan to use tabs ... it uses the commaned "edit" ... just replace that with "tabe" and :QuickFixOpenAll will open each result buffer in a new tab.

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

    For achieving what you want you have to open the quickfix/error window after calling grep:

    :copen
    

    I have a script that makes it for me every time i use grep.

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