Viewing man pages in vim











up vote
7
down vote

favorite
2












I wrote a function in bash to see manpages in vim



viman () { man "$@" | vim -R +":set ft=man" - ; }


This works fine, the only problem occurs if I pass a manpage to it which doesn't exist. It prints that the manpage doesn't exist but still opens vim with an empty buffer.

So, I changed the function to check the error code ( which is 16 here ) and exit if the manpage doesn't exist. The modefied function looks somewhat like this -



viman () { man "$@" | [[ $? == 16 ]] && exit 1 | vim -R +":set ft=man" -  ; }


But, now it doesn't do anything!!



I just want to quit the program if the manpage doesn't exist otherwise open the manpage with vim










share|improve this question




























    up vote
    7
    down vote

    favorite
    2












    I wrote a function in bash to see manpages in vim



    viman () { man "$@" | vim -R +":set ft=man" - ; }


    This works fine, the only problem occurs if I pass a manpage to it which doesn't exist. It prints that the manpage doesn't exist but still opens vim with an empty buffer.

    So, I changed the function to check the error code ( which is 16 here ) and exit if the manpage doesn't exist. The modefied function looks somewhat like this -



    viman () { man "$@" | [[ $? == 16 ]] && exit 1 | vim -R +":set ft=man" -  ; }


    But, now it doesn't do anything!!



    I just want to quit the program if the manpage doesn't exist otherwise open the manpage with vim










    share|improve this question


























      up vote
      7
      down vote

      favorite
      2









      up vote
      7
      down vote

      favorite
      2






      2





      I wrote a function in bash to see manpages in vim



      viman () { man "$@" | vim -R +":set ft=man" - ; }


      This works fine, the only problem occurs if I pass a manpage to it which doesn't exist. It prints that the manpage doesn't exist but still opens vim with an empty buffer.

      So, I changed the function to check the error code ( which is 16 here ) and exit if the manpage doesn't exist. The modefied function looks somewhat like this -



      viman () { man "$@" | [[ $? == 16 ]] && exit 1 | vim -R +":set ft=man" -  ; }


      But, now it doesn't do anything!!



      I just want to quit the program if the manpage doesn't exist otherwise open the manpage with vim










      share|improve this question















      I wrote a function in bash to see manpages in vim



      viman () { man "$@" | vim -R +":set ft=man" - ; }


      This works fine, the only problem occurs if I pass a manpage to it which doesn't exist. It prints that the manpage doesn't exist but still opens vim with an empty buffer.

      So, I changed the function to check the error code ( which is 16 here ) and exit if the manpage doesn't exist. The modefied function looks somewhat like this -



      viman () { man "$@" | [[ $? == 16 ]] && exit 1 | vim -R +":set ft=man" -  ; }


      But, now it doesn't do anything!!



      I just want to quit the program if the manpage doesn't exist otherwise open the manpage with vim







      bash vim man function






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited yesterday









      Jeff Schaller

      37.6k1052121




      37.6k1052121










      asked yesterday









      Ritajit Kundu

      416




      416






















          4 Answers
          4






          active

          oldest

          votes

















          up vote
          7
          down vote



          accepted










          Try this: capture the man output, and if successful launch vim



          viman () { text=$(man "$@") && echo "$text" | vim -R +":set ft=man" - ; }





          share|improve this answer





















          • Capturing the output in a text is a simple and brilliant idea. Putting all things together the viman function is ready - viman () { text=$(man "$@") && echo "$text" | vim -R +":set ft=man nomod nonu noma nolist colorcolumn=" - ; }
            – Ritajit Kundu
            yesterday




















          up vote
          3
          down vote













          I like the idea of checking the man return code; you can't pipe to the test, though. You could just run man twice:



          viman () { man "$@" >/dev/null 2>&1 && man "$@" | vim -R +":set ft=man" - ; }


          This runs man ... | vim ... only if the first invocation of man was successful.






          share|improve this answer






























            up vote
            2
            down vote













            There's an environment variable called MANPAGER which can be used to make man call the command you want for displaying the manpage. The advantage of this is that you call man directly, and it won't run the pager at all if the manpage didn't exist.



            So a wrapper script, say in ~/bin/vimman:





            #! /bin/sh
            vim -R +":set ft=man" -


            With this in your shell initialisation files somewhere:



            export MANPAGER="$HOME/bin/vimman"


            And you can directly run man foo to manpages in Vim.



            (Depending on the man command being used, you could also have:



            export MANPAGER='vim -R +":set ft=man" -'


            directly instead of a wrapper script.)





            If you have a new enough Vim, you can use the --not-a-term option to stop Vim from complaining about stdin not being a TTY.





            Shameless plug: I wrote a small plugin to facilitate using Vim as manpager.






            share|improve this answer























            • This is also a nice solution, but let me ask you, I depend a lot on manpage than on google or at least try to, so I've written different wrappers to make manpager interesting , for there is a gui manpager that works with yelp, now I am wondering if the yelp will be able to work with this?
              – Ritajit Kundu
              yesterday










            • @RitajitKundu if you're using yelp, you can directly open manpages in it: yelp man:foo (askubuntu.com/a/390095/158442)
              – muru
              yesterday


















            up vote
            1
            down vote













            Based on this answer this starts vim and exits if there's nothing in the buffer. The disadvantage is that it starts vim so the screen "flashes". It also doesn't set an exit code when a man page isn't found.



            viman () { vim -R +':set ft=man|exe !search(".")?"quit!":""' <(man "$@" 2>/dev/null); }


            This is an improvement on Jeff Schaller's answer in that it doesn't load the the man page twice when it exists. It also doesn't load vim unnecessarily like my previous example. And it does set an exit code when there's no man page.



            viman () { man -f "$@" >/dev/null 2>&1 && vim -R +":set ft=man" <(man "$@"); }


            Both examples use Bash process substitution in order to avoid the "Vim: Reading from stdin..." message.



            Neither loads the page into a variable.






            share|improve this answer





















            • An even more lightweight alternative to man -w may be man -w.
              – Matteo Italia
              yesterday











            Your Answer








            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "106"
            };
            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',
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            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%2funix.stackexchange.com%2fquestions%2f487415%2fviewing-man-pages-in-vim%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            4 Answers
            4






            active

            oldest

            votes








            4 Answers
            4






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            7
            down vote



            accepted










            Try this: capture the man output, and if successful launch vim



            viman () { text=$(man "$@") && echo "$text" | vim -R +":set ft=man" - ; }





            share|improve this answer





















            • Capturing the output in a text is a simple and brilliant idea. Putting all things together the viman function is ready - viman () { text=$(man "$@") && echo "$text" | vim -R +":set ft=man nomod nonu noma nolist colorcolumn=" - ; }
              – Ritajit Kundu
              yesterday

















            up vote
            7
            down vote



            accepted










            Try this: capture the man output, and if successful launch vim



            viman () { text=$(man "$@") && echo "$text" | vim -R +":set ft=man" - ; }





            share|improve this answer





















            • Capturing the output in a text is a simple and brilliant idea. Putting all things together the viman function is ready - viman () { text=$(man "$@") && echo "$text" | vim -R +":set ft=man nomod nonu noma nolist colorcolumn=" - ; }
              – Ritajit Kundu
              yesterday















            up vote
            7
            down vote



            accepted







            up vote
            7
            down vote



            accepted






            Try this: capture the man output, and if successful launch vim



            viman () { text=$(man "$@") && echo "$text" | vim -R +":set ft=man" - ; }





            share|improve this answer












            Try this: capture the man output, and if successful launch vim



            viman () { text=$(man "$@") && echo "$text" | vim -R +":set ft=man" - ; }






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered yesterday









            glenn jackman

            50k569106




            50k569106












            • Capturing the output in a text is a simple and brilliant idea. Putting all things together the viman function is ready - viman () { text=$(man "$@") && echo "$text" | vim -R +":set ft=man nomod nonu noma nolist colorcolumn=" - ; }
              – Ritajit Kundu
              yesterday




















            • Capturing the output in a text is a simple and brilliant idea. Putting all things together the viman function is ready - viman () { text=$(man "$@") && echo "$text" | vim -R +":set ft=man nomod nonu noma nolist colorcolumn=" - ; }
              – Ritajit Kundu
              yesterday


















            Capturing the output in a text is a simple and brilliant idea. Putting all things together the viman function is ready - viman () { text=$(man "$@") && echo "$text" | vim -R +":set ft=man nomod nonu noma nolist colorcolumn=" - ; }
            – Ritajit Kundu
            yesterday






            Capturing the output in a text is a simple and brilliant idea. Putting all things together the viman function is ready - viman () { text=$(man "$@") && echo "$text" | vim -R +":set ft=man nomod nonu noma nolist colorcolumn=" - ; }
            – Ritajit Kundu
            yesterday














            up vote
            3
            down vote













            I like the idea of checking the man return code; you can't pipe to the test, though. You could just run man twice:



            viman () { man "$@" >/dev/null 2>&1 && man "$@" | vim -R +":set ft=man" - ; }


            This runs man ... | vim ... only if the first invocation of man was successful.






            share|improve this answer



























              up vote
              3
              down vote













              I like the idea of checking the man return code; you can't pipe to the test, though. You could just run man twice:



              viman () { man "$@" >/dev/null 2>&1 && man "$@" | vim -R +":set ft=man" - ; }


              This runs man ... | vim ... only if the first invocation of man was successful.






              share|improve this answer

























                up vote
                3
                down vote










                up vote
                3
                down vote









                I like the idea of checking the man return code; you can't pipe to the test, though. You could just run man twice:



                viman () { man "$@" >/dev/null 2>&1 && man "$@" | vim -R +":set ft=man" - ; }


                This runs man ... | vim ... only if the first invocation of man was successful.






                share|improve this answer














                I like the idea of checking the man return code; you can't pipe to the test, though. You could just run man twice:



                viman () { man "$@" >/dev/null 2>&1 && man "$@" | vim -R +":set ft=man" - ; }


                This runs man ... | vim ... only if the first invocation of man was successful.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited yesterday

























                answered yesterday









                Jeff Schaller

                37.6k1052121




                37.6k1052121






















                    up vote
                    2
                    down vote













                    There's an environment variable called MANPAGER which can be used to make man call the command you want for displaying the manpage. The advantage of this is that you call man directly, and it won't run the pager at all if the manpage didn't exist.



                    So a wrapper script, say in ~/bin/vimman:





                    #! /bin/sh
                    vim -R +":set ft=man" -


                    With this in your shell initialisation files somewhere:



                    export MANPAGER="$HOME/bin/vimman"


                    And you can directly run man foo to manpages in Vim.



                    (Depending on the man command being used, you could also have:



                    export MANPAGER='vim -R +":set ft=man" -'


                    directly instead of a wrapper script.)





                    If you have a new enough Vim, you can use the --not-a-term option to stop Vim from complaining about stdin not being a TTY.





                    Shameless plug: I wrote a small plugin to facilitate using Vim as manpager.






                    share|improve this answer























                    • This is also a nice solution, but let me ask you, I depend a lot on manpage than on google or at least try to, so I've written different wrappers to make manpager interesting , for there is a gui manpager that works with yelp, now I am wondering if the yelp will be able to work with this?
                      – Ritajit Kundu
                      yesterday










                    • @RitajitKundu if you're using yelp, you can directly open manpages in it: yelp man:foo (askubuntu.com/a/390095/158442)
                      – muru
                      yesterday















                    up vote
                    2
                    down vote













                    There's an environment variable called MANPAGER which can be used to make man call the command you want for displaying the manpage. The advantage of this is that you call man directly, and it won't run the pager at all if the manpage didn't exist.



                    So a wrapper script, say in ~/bin/vimman:





                    #! /bin/sh
                    vim -R +":set ft=man" -


                    With this in your shell initialisation files somewhere:



                    export MANPAGER="$HOME/bin/vimman"


                    And you can directly run man foo to manpages in Vim.



                    (Depending on the man command being used, you could also have:



                    export MANPAGER='vim -R +":set ft=man" -'


                    directly instead of a wrapper script.)





                    If you have a new enough Vim, you can use the --not-a-term option to stop Vim from complaining about stdin not being a TTY.





                    Shameless plug: I wrote a small plugin to facilitate using Vim as manpager.






                    share|improve this answer























                    • This is also a nice solution, but let me ask you, I depend a lot on manpage than on google or at least try to, so I've written different wrappers to make manpager interesting , for there is a gui manpager that works with yelp, now I am wondering if the yelp will be able to work with this?
                      – Ritajit Kundu
                      yesterday










                    • @RitajitKundu if you're using yelp, you can directly open manpages in it: yelp man:foo (askubuntu.com/a/390095/158442)
                      – muru
                      yesterday













                    up vote
                    2
                    down vote










                    up vote
                    2
                    down vote









                    There's an environment variable called MANPAGER which can be used to make man call the command you want for displaying the manpage. The advantage of this is that you call man directly, and it won't run the pager at all if the manpage didn't exist.



                    So a wrapper script, say in ~/bin/vimman:





                    #! /bin/sh
                    vim -R +":set ft=man" -


                    With this in your shell initialisation files somewhere:



                    export MANPAGER="$HOME/bin/vimman"


                    And you can directly run man foo to manpages in Vim.



                    (Depending on the man command being used, you could also have:



                    export MANPAGER='vim -R +":set ft=man" -'


                    directly instead of a wrapper script.)





                    If you have a new enough Vim, you can use the --not-a-term option to stop Vim from complaining about stdin not being a TTY.





                    Shameless plug: I wrote a small plugin to facilitate using Vim as manpager.






                    share|improve this answer














                    There's an environment variable called MANPAGER which can be used to make man call the command you want for displaying the manpage. The advantage of this is that you call man directly, and it won't run the pager at all if the manpage didn't exist.



                    So a wrapper script, say in ~/bin/vimman:





                    #! /bin/sh
                    vim -R +":set ft=man" -


                    With this in your shell initialisation files somewhere:



                    export MANPAGER="$HOME/bin/vimman"


                    And you can directly run man foo to manpages in Vim.



                    (Depending on the man command being used, you could also have:



                    export MANPAGER='vim -R +":set ft=man" -'


                    directly instead of a wrapper script.)





                    If you have a new enough Vim, you can use the --not-a-term option to stop Vim from complaining about stdin not being a TTY.





                    Shameless plug: I wrote a small plugin to facilitate using Vim as manpager.







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited yesterday

























                    answered yesterday









                    muru

                    35.4k582157




                    35.4k582157












                    • This is also a nice solution, but let me ask you, I depend a lot on manpage than on google or at least try to, so I've written different wrappers to make manpager interesting , for there is a gui manpager that works with yelp, now I am wondering if the yelp will be able to work with this?
                      – Ritajit Kundu
                      yesterday










                    • @RitajitKundu if you're using yelp, you can directly open manpages in it: yelp man:foo (askubuntu.com/a/390095/158442)
                      – muru
                      yesterday


















                    • This is also a nice solution, but let me ask you, I depend a lot on manpage than on google or at least try to, so I've written different wrappers to make manpager interesting , for there is a gui manpager that works with yelp, now I am wondering if the yelp will be able to work with this?
                      – Ritajit Kundu
                      yesterday










                    • @RitajitKundu if you're using yelp, you can directly open manpages in it: yelp man:foo (askubuntu.com/a/390095/158442)
                      – muru
                      yesterday
















                    This is also a nice solution, but let me ask you, I depend a lot on manpage than on google or at least try to, so I've written different wrappers to make manpager interesting , for there is a gui manpager that works with yelp, now I am wondering if the yelp will be able to work with this?
                    – Ritajit Kundu
                    yesterday




                    This is also a nice solution, but let me ask you, I depend a lot on manpage than on google or at least try to, so I've written different wrappers to make manpager interesting , for there is a gui manpager that works with yelp, now I am wondering if the yelp will be able to work with this?
                    – Ritajit Kundu
                    yesterday












                    @RitajitKundu if you're using yelp, you can directly open manpages in it: yelp man:foo (askubuntu.com/a/390095/158442)
                    – muru
                    yesterday




                    @RitajitKundu if you're using yelp, you can directly open manpages in it: yelp man:foo (askubuntu.com/a/390095/158442)
                    – muru
                    yesterday










                    up vote
                    1
                    down vote













                    Based on this answer this starts vim and exits if there's nothing in the buffer. The disadvantage is that it starts vim so the screen "flashes". It also doesn't set an exit code when a man page isn't found.



                    viman () { vim -R +':set ft=man|exe !search(".")?"quit!":""' <(man "$@" 2>/dev/null); }


                    This is an improvement on Jeff Schaller's answer in that it doesn't load the the man page twice when it exists. It also doesn't load vim unnecessarily like my previous example. And it does set an exit code when there's no man page.



                    viman () { man -f "$@" >/dev/null 2>&1 && vim -R +":set ft=man" <(man "$@"); }


                    Both examples use Bash process substitution in order to avoid the "Vim: Reading from stdin..." message.



                    Neither loads the page into a variable.






                    share|improve this answer





















                    • An even more lightweight alternative to man -w may be man -w.
                      – Matteo Italia
                      yesterday















                    up vote
                    1
                    down vote













                    Based on this answer this starts vim and exits if there's nothing in the buffer. The disadvantage is that it starts vim so the screen "flashes". It also doesn't set an exit code when a man page isn't found.



                    viman () { vim -R +':set ft=man|exe !search(".")?"quit!":""' <(man "$@" 2>/dev/null); }


                    This is an improvement on Jeff Schaller's answer in that it doesn't load the the man page twice when it exists. It also doesn't load vim unnecessarily like my previous example. And it does set an exit code when there's no man page.



                    viman () { man -f "$@" >/dev/null 2>&1 && vim -R +":set ft=man" <(man "$@"); }


                    Both examples use Bash process substitution in order to avoid the "Vim: Reading from stdin..." message.



                    Neither loads the page into a variable.






                    share|improve this answer





















                    • An even more lightweight alternative to man -w may be man -w.
                      – Matteo Italia
                      yesterday













                    up vote
                    1
                    down vote










                    up vote
                    1
                    down vote









                    Based on this answer this starts vim and exits if there's nothing in the buffer. The disadvantage is that it starts vim so the screen "flashes". It also doesn't set an exit code when a man page isn't found.



                    viman () { vim -R +':set ft=man|exe !search(".")?"quit!":""' <(man "$@" 2>/dev/null); }


                    This is an improvement on Jeff Schaller's answer in that it doesn't load the the man page twice when it exists. It also doesn't load vim unnecessarily like my previous example. And it does set an exit code when there's no man page.



                    viman () { man -f "$@" >/dev/null 2>&1 && vim -R +":set ft=man" <(man "$@"); }


                    Both examples use Bash process substitution in order to avoid the "Vim: Reading from stdin..." message.



                    Neither loads the page into a variable.






                    share|improve this answer












                    Based on this answer this starts vim and exits if there's nothing in the buffer. The disadvantage is that it starts vim so the screen "flashes". It also doesn't set an exit code when a man page isn't found.



                    viman () { vim -R +':set ft=man|exe !search(".")?"quit!":""' <(man "$@" 2>/dev/null); }


                    This is an improvement on Jeff Schaller's answer in that it doesn't load the the man page twice when it exists. It also doesn't load vim unnecessarily like my previous example. And it does set an exit code when there's no man page.



                    viman () { man -f "$@" >/dev/null 2>&1 && vim -R +":set ft=man" <(man "$@"); }


                    Both examples use Bash process substitution in order to avoid the "Vim: Reading from stdin..." message.



                    Neither loads the page into a variable.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered yesterday









                    Dennis Williamson

                    5,34812232




                    5,34812232












                    • An even more lightweight alternative to man -w may be man -w.
                      – Matteo Italia
                      yesterday


















                    • An even more lightweight alternative to man -w may be man -w.
                      – Matteo Italia
                      yesterday
















                    An even more lightweight alternative to man -w may be man -w.
                    – Matteo Italia
                    yesterday




                    An even more lightweight alternative to man -w may be man -w.
                    – Matteo Italia
                    yesterday


















                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Unix & Linux Stack Exchange!


                    • 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%2funix.stackexchange.com%2fquestions%2f487415%2fviewing-man-pages-in-vim%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

                    In PowerPoint, is there a keyboard shortcut for bulleted / numbered list?

                    How to put 3 figures in Latex with 2 figures side by side and 1 below these side by side images but in...