How do I redirect command output to vim in bash?












77















I am trying to redirect the output of a bash command into a new file.



If I try the pipe as below :



ls -la | vim


Bash shows me the errors :



Vim: Error reading input, exiting...
Vim: preserving files...
Vim: Finished.


I know that I can open Vim and then use :



:r !ls -la


But is there a way to do this in bash itself, so that Vim is opened and the output is pasted there automatically?










share|improve this question





























    77















    I am trying to redirect the output of a bash command into a new file.



    If I try the pipe as below :



    ls -la | vim


    Bash shows me the errors :



    Vim: Error reading input, exiting...
    Vim: preserving files...
    Vim: Finished.


    I know that I can open Vim and then use :



    :r !ls -la


    But is there a way to do this in bash itself, so that Vim is opened and the output is pasted there automatically?










    share|improve this question



























      77












      77








      77


      25






      I am trying to redirect the output of a bash command into a new file.



      If I try the pipe as below :



      ls -la | vim


      Bash shows me the errors :



      Vim: Error reading input, exiting...
      Vim: preserving files...
      Vim: Finished.


      I know that I can open Vim and then use :



      :r !ls -la


      But is there a way to do this in bash itself, so that Vim is opened and the output is pasted there automatically?










      share|improve this question
















      I am trying to redirect the output of a bash command into a new file.



      If I try the pipe as below :



      ls -la | vim


      Bash shows me the errors :



      Vim: Error reading input, exiting...
      Vim: preserving files...
      Vim: Finished.


      I know that I can open Vim and then use :



      :r !ls -la


      But is there a way to do this in bash itself, so that Vim is opened and the output is pasted there automatically?







      bash vim vi






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Aug 26 '15 at 1:00









      muru

      1




      1










      asked Aug 13 '14 at 16:13









      faizalfaizal

      1,13261826




      1,13261826






















          6 Answers
          6






          active

          oldest

          votes


















          126














          You can use process substitution (this also works with applications that can't read from STDIN):



          vim <(ls -la)


          Or use vim's function to read from STDIN:



          ls -la | vim -





          share|improve this answer





















          • 2





            <(ls -la) is actually process substitution rather than command substitution.

            – Eliah Kagan
            Sep 2 '14 at 1:32






          • 1





            I really like vim's option, it allows me to search, find and save the output easily from data dumps.

            – Josue Alexander Ibarra
            May 11 '16 at 21:36



















          36














          You're really close on your own. You were just missing one character.



          ls -la | vim -





          share|improve this answer



















          • 7





            Well technically it's two characters. ;)

            – Cory Klein
            Oct 24 '17 at 22:14



















          18














          Here's another approach, hopefully to teach someone something new.



          If you know that the command :r !ls -la works inside vim, you can do the following to open vim and make it run the command as soon as it opens, straight from bash:



          vim -c ':r! ls -la'


          This is the equivalent of opening vim then executing the command :r! ls -la. This should work with any vim command, where the command comes after the -c option.






          share|improve this answer































            12














            You can tell vim to open stdin:



            ls -la | vim -





            share|improve this answer































              1














              If your goal is simply to get the output into a text file then you don't need to invoke vim. Bash can do it alone with



              ls -la > outputfile.txt





              share|improve this answer































                1














                setlocal buftype=nofile



                This is a good option if you are going to create an alias to replace less:



                seq 100 | vim +':setlocal buftype=nofile' -


                Now you don't need to type the ! to quit.



                Another option is:



                seq 100 | vim +'nnoremap q :quit!' -


                so you can exit with just q<enter>.






                share|improve this answer


























                • Good idea. Definitely shorter than typing this long ! :)

                  – kode
                  Jun 21 '17 at 15:04











                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%2f510890%2fhow-do-i-redirect-command-output-to-vim-in-bash%23new-answer', 'question_page');
                }
                );

                Post as a guest















                Required, but never shown

























                6 Answers
                6






                active

                oldest

                votes








                6 Answers
                6






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                126














                You can use process substitution (this also works with applications that can't read from STDIN):



                vim <(ls -la)


                Or use vim's function to read from STDIN:



                ls -la | vim -





                share|improve this answer





















                • 2





                  <(ls -la) is actually process substitution rather than command substitution.

                  – Eliah Kagan
                  Sep 2 '14 at 1:32






                • 1





                  I really like vim's option, it allows me to search, find and save the output easily from data dumps.

                  – Josue Alexander Ibarra
                  May 11 '16 at 21:36
















                126














                You can use process substitution (this also works with applications that can't read from STDIN):



                vim <(ls -la)


                Or use vim's function to read from STDIN:



                ls -la | vim -





                share|improve this answer





















                • 2





                  <(ls -la) is actually process substitution rather than command substitution.

                  – Eliah Kagan
                  Sep 2 '14 at 1:32






                • 1





                  I really like vim's option, it allows me to search, find and save the output easily from data dumps.

                  – Josue Alexander Ibarra
                  May 11 '16 at 21:36














                126












                126








                126







                You can use process substitution (this also works with applications that can't read from STDIN):



                vim <(ls -la)


                Or use vim's function to read from STDIN:



                ls -la | vim -





                share|improve this answer















                You can use process substitution (this also works with applications that can't read from STDIN):



                vim <(ls -la)


                Or use vim's function to read from STDIN:



                ls -la | vim -






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Aug 26 '15 at 0:58









                muru

                1




                1










                answered Aug 13 '14 at 16:58









                chaoschaos

                19.7k85968




                19.7k85968








                • 2





                  <(ls -la) is actually process substitution rather than command substitution.

                  – Eliah Kagan
                  Sep 2 '14 at 1:32






                • 1





                  I really like vim's option, it allows me to search, find and save the output easily from data dumps.

                  – Josue Alexander Ibarra
                  May 11 '16 at 21:36














                • 2





                  <(ls -la) is actually process substitution rather than command substitution.

                  – Eliah Kagan
                  Sep 2 '14 at 1:32






                • 1





                  I really like vim's option, it allows me to search, find and save the output easily from data dumps.

                  – Josue Alexander Ibarra
                  May 11 '16 at 21:36








                2




                2





                <(ls -la) is actually process substitution rather than command substitution.

                – Eliah Kagan
                Sep 2 '14 at 1:32





                <(ls -la) is actually process substitution rather than command substitution.

                – Eliah Kagan
                Sep 2 '14 at 1:32




                1




                1





                I really like vim's option, it allows me to search, find and save the output easily from data dumps.

                – Josue Alexander Ibarra
                May 11 '16 at 21:36





                I really like vim's option, it allows me to search, find and save the output easily from data dumps.

                – Josue Alexander Ibarra
                May 11 '16 at 21:36













                36














                You're really close on your own. You were just missing one character.



                ls -la | vim -





                share|improve this answer



















                • 7





                  Well technically it's two characters. ;)

                  – Cory Klein
                  Oct 24 '17 at 22:14
















                36














                You're really close on your own. You were just missing one character.



                ls -la | vim -





                share|improve this answer



















                • 7





                  Well technically it's two characters. ;)

                  – Cory Klein
                  Oct 24 '17 at 22:14














                36












                36








                36







                You're really close on your own. You were just missing one character.



                ls -la | vim -





                share|improve this answer













                You're really close on your own. You were just missing one character.



                ls -la | vim -






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Aug 13 '14 at 16:18









                Cris HoldorphCris Holdorph

                524148




                524148








                • 7





                  Well technically it's two characters. ;)

                  – Cory Klein
                  Oct 24 '17 at 22:14














                • 7





                  Well technically it's two characters. ;)

                  – Cory Klein
                  Oct 24 '17 at 22:14








                7




                7





                Well technically it's two characters. ;)

                – Cory Klein
                Oct 24 '17 at 22:14





                Well technically it's two characters. ;)

                – Cory Klein
                Oct 24 '17 at 22:14











                18














                Here's another approach, hopefully to teach someone something new.



                If you know that the command :r !ls -la works inside vim, you can do the following to open vim and make it run the command as soon as it opens, straight from bash:



                vim -c ':r! ls -la'


                This is the equivalent of opening vim then executing the command :r! ls -la. This should work with any vim command, where the command comes after the -c option.






                share|improve this answer




























                  18














                  Here's another approach, hopefully to teach someone something new.



                  If you know that the command :r !ls -la works inside vim, you can do the following to open vim and make it run the command as soon as it opens, straight from bash:



                  vim -c ':r! ls -la'


                  This is the equivalent of opening vim then executing the command :r! ls -la. This should work with any vim command, where the command comes after the -c option.






                  share|improve this answer


























                    18












                    18








                    18







                    Here's another approach, hopefully to teach someone something new.



                    If you know that the command :r !ls -la works inside vim, you can do the following to open vim and make it run the command as soon as it opens, straight from bash:



                    vim -c ':r! ls -la'


                    This is the equivalent of opening vim then executing the command :r! ls -la. This should work with any vim command, where the command comes after the -c option.






                    share|improve this answer













                    Here's another approach, hopefully to teach someone something new.



                    If you know that the command :r !ls -la works inside vim, you can do the following to open vim and make it run the command as soon as it opens, straight from bash:



                    vim -c ':r! ls -la'


                    This is the equivalent of opening vim then executing the command :r! ls -la. This should work with any vim command, where the command comes after the -c option.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Aug 13 '14 at 17:11









                    Alaa AliAlaa Ali

                    22.5k96994




                    22.5k96994























                        12














                        You can tell vim to open stdin:



                        ls -la | vim -





                        share|improve this answer




























                          12














                          You can tell vim to open stdin:



                          ls -la | vim -





                          share|improve this answer


























                            12












                            12








                            12







                            You can tell vim to open stdin:



                            ls -la | vim -





                            share|improve this answer













                            You can tell vim to open stdin:



                            ls -la | vim -






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Aug 13 '14 at 16:20









                            iffyiffy

                            9521712




                            9521712























                                1














                                If your goal is simply to get the output into a text file then you don't need to invoke vim. Bash can do it alone with



                                ls -la > outputfile.txt





                                share|improve this answer




























                                  1














                                  If your goal is simply to get the output into a text file then you don't need to invoke vim. Bash can do it alone with



                                  ls -la > outputfile.txt





                                  share|improve this answer


























                                    1












                                    1








                                    1







                                    If your goal is simply to get the output into a text file then you don't need to invoke vim. Bash can do it alone with



                                    ls -la > outputfile.txt





                                    share|improve this answer













                                    If your goal is simply to get the output into a text file then you don't need to invoke vim. Bash can do it alone with



                                    ls -la > outputfile.txt






                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Mar 24 '17 at 15:44









                                    Jake StewartJake Stewart

                                    111




                                    111























                                        1














                                        setlocal buftype=nofile



                                        This is a good option if you are going to create an alias to replace less:



                                        seq 100 | vim +':setlocal buftype=nofile' -


                                        Now you don't need to type the ! to quit.



                                        Another option is:



                                        seq 100 | vim +'nnoremap q :quit!' -


                                        so you can exit with just q<enter>.






                                        share|improve this answer


























                                        • Good idea. Definitely shorter than typing this long ! :)

                                          – kode
                                          Jun 21 '17 at 15:04
















                                        1














                                        setlocal buftype=nofile



                                        This is a good option if you are going to create an alias to replace less:



                                        seq 100 | vim +':setlocal buftype=nofile' -


                                        Now you don't need to type the ! to quit.



                                        Another option is:



                                        seq 100 | vim +'nnoremap q :quit!' -


                                        so you can exit with just q<enter>.






                                        share|improve this answer


























                                        • Good idea. Definitely shorter than typing this long ! :)

                                          – kode
                                          Jun 21 '17 at 15:04














                                        1












                                        1








                                        1







                                        setlocal buftype=nofile



                                        This is a good option if you are going to create an alias to replace less:



                                        seq 100 | vim +':setlocal buftype=nofile' -


                                        Now you don't need to type the ! to quit.



                                        Another option is:



                                        seq 100 | vim +'nnoremap q :quit!' -


                                        so you can exit with just q<enter>.






                                        share|improve this answer















                                        setlocal buftype=nofile



                                        This is a good option if you are going to create an alias to replace less:



                                        seq 100 | vim +':setlocal buftype=nofile' -


                                        Now you don't need to type the ! to quit.



                                        Another option is:



                                        seq 100 | vim +'nnoremap q :quit!' -


                                        so you can exit with just q<enter>.







                                        share|improve this answer














                                        share|improve this answer



                                        share|improve this answer








                                        edited Jan 30 at 12:25

























                                        answered Jan 29 '17 at 14:43









                                        Ciro Santilli 新疆改造中心 六四事件 法轮功Ciro Santilli 新疆改造中心 六四事件 法轮功

                                        10.2k44751




                                        10.2k44751













                                        • Good idea. Definitely shorter than typing this long ! :)

                                          – kode
                                          Jun 21 '17 at 15:04



















                                        • Good idea. Definitely shorter than typing this long ! :)

                                          – kode
                                          Jun 21 '17 at 15:04

















                                        Good idea. Definitely shorter than typing this long ! :)

                                        – kode
                                        Jun 21 '17 at 15:04





                                        Good idea. Definitely shorter than typing this long ! :)

                                        – kode
                                        Jun 21 '17 at 15:04


















                                        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.




                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function () {
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f510890%2fhow-do-i-redirect-command-output-to-vim-in-bash%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

                                        How to change which sound is reproduced for terminal bell?

                                        Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

                                        Can I use Tabulator js library in my java Spring + Thymeleaf project?