Given the result of grep -n, how can I open vim in that specific line? (using only keyboard)












7














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?










share|improve this question
























  • You can try something like that: echo a/b/c:70: keyword | awk '{print $1}' | sed 's,:$,,' | sed 's,:, +,' | xargs vim && reset.
    – Arkadiusz Drabczyk
    Dec 11 '18 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 '18 at 12:49
















7














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?










share|improve this question
























  • You can try something like that: echo a/b/c:70: keyword | awk '{print $1}' | sed 's,:$,,' | sed 's,:, +,' | xargs vim && reset.
    – Arkadiusz Drabczyk
    Dec 11 '18 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 '18 at 12:49














7












7








7


2





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?










share|improve this question















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?







command-line vim grep






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 11 '18 at 14:07









muru

1




1










asked Dec 11 '18 at 12:26









CIsForCookies

21318




21318












  • You can try something like that: echo a/b/c:70: keyword | awk '{print $1}' | sed 's,:$,,' | sed 's,:, +,' | xargs vim && reset.
    – Arkadiusz Drabczyk
    Dec 11 '18 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 '18 at 12:49


















  • You can try something like that: echo a/b/c:70: keyword | awk '{print $1}' | sed 's,:$,,' | sed 's,:, +,' | xargs vim && reset.
    – Arkadiusz Drabczyk
    Dec 11 '18 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 '18 at 12:49
















You can try something like that: echo a/b/c:70: keyword | awk '{print $1}' | sed 's,:$,,' | sed 's,:, +,' | xargs vim && reset.
– Arkadiusz Drabczyk
Dec 11 '18 at 12:48




You can try something like that: echo a/b/c:70: keyword | awk '{print $1}' | sed 's,:$,,' | sed 's,:, +,' | xargs vim && reset.
– Arkadiusz Drabczyk
Dec 11 '18 at 12:48




2




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 '18 at 12:49




Also, if you're interested in using Vim more efficiently, do check out the dedicated Vi and Vim Stack Exchange site.
– muru
Dec 11 '18 at 12:49










2 Answers
2






active

oldest

votes


















11














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





share|improve this answer





















  • Trying to use the same method on git status, to no avail :( - vim -q <(git status | grep modified)
    – CIsForCookies
    Dec 11 '18 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 '18 at 13:43












  • BTW, I edited my ~/.bashrc grep alias to use colors always, and that, for some reason, broke the -q >() option...
    – CIsForCookies
    Dec 13 '18 at 19:01



















2














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.






share|improve this answer





















    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "89"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1100032%2fgiven-the-result-of-grep-n-how-can-i-open-vim-in-that-specific-line-using-on%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    11














    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





    share|improve this answer





















    • Trying to use the same method on git status, to no avail :( - vim -q <(git status | grep modified)
      – CIsForCookies
      Dec 11 '18 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 '18 at 13:43












    • BTW, I edited my ~/.bashrc grep alias to use colors always, and that, for some reason, broke the -q >() option...
      – CIsForCookies
      Dec 13 '18 at 19:01
















    11














    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





    share|improve this answer





















    • Trying to use the same method on git status, to no avail :( - vim -q <(git status | grep modified)
      – CIsForCookies
      Dec 11 '18 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 '18 at 13:43












    • BTW, I edited my ~/.bashrc grep alias to use colors always, and that, for some reason, broke the -q >() option...
      – CIsForCookies
      Dec 13 '18 at 19:01














    11












    11








    11






    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





    share|improve this answer












    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






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Dec 11 '18 at 12:42









    muru

    1




    1












    • Trying to use the same method on git status, to no avail :( - vim -q <(git status | grep modified)
      – CIsForCookies
      Dec 11 '18 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 '18 at 13:43












    • BTW, I edited my ~/.bashrc grep alias to use colors always, and that, for some reason, broke the -q >() option...
      – CIsForCookies
      Dec 13 '18 at 19:01


















    • Trying to use the same method on git status, to no avail :( - vim -q <(git status | grep modified)
      – CIsForCookies
      Dec 11 '18 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 '18 at 13:43












    • BTW, I edited my ~/.bashrc grep alias to use colors always, and that, for some reason, broke the -q >() option...
      – CIsForCookies
      Dec 13 '18 at 19:01
















    Trying to use the same method on git status, to no avail :( - vim -q <(git status | grep modified)
    – CIsForCookies
    Dec 11 '18 at 13:39




    Trying to use the same method on git status, to no avail :( - vim -q <(git status | grep modified)
    – CIsForCookies
    Dec 11 '18 at 13:39




    3




    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 '18 at 13:43






    @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 '18 at 13:43














    BTW, I edited my ~/.bashrc grep alias to use colors always, and that, for some reason, broke the -q >() option...
    – CIsForCookies
    Dec 13 '18 at 19:01




    BTW, I edited my ~/.bashrc grep alias to use colors always, and that, for some reason, broke the -q >() option...
    – CIsForCookies
    Dec 13 '18 at 19:01













    2














    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.






    share|improve this answer


























      2














      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.






      share|improve this answer
























        2












        2








        2






        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.






        share|improve this answer












        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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Dec 11 '18 at 21:35









        max630

        1212




        1212






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Ask Ubuntu!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1100032%2fgiven-the-result-of-grep-n-how-can-i-open-vim-in-that-specific-line-using-on%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Plaza Victoria

            Puebla de Zaragoza

            Musa