What is a way to read man pages in vim without using temporary files

前端 未结 11 1266
萌比男神i
萌比男神i 2020-12-12 19:36

I want to be able to read man pages in vim. For some reason, it seems that vim isn\'t able to read the output of programs through piping (i.e \'(man ls) | vi\' doesn\'t seem

相关标签:
11条回答
  • 2020-12-12 20:03

    A lot of good answers, with respect to plugins it's worth to add that vim-man* provides a set of convenience functions to open and read man pages:

    Viewing man pages, as per docs.

    • :Man printf - open printf(1) man page in a split
    • :Vman 3 putc - open putc(3) man page in a vertical split
    • :Man pri<Tab> - command completion for man page names

    * Available on GitHub: https://github.com/vim-utils/vim-man.

    0 讨论(0)
  • 2020-12-12 20:05

    By default vim reads vimscripts (=vim commands), not input files, from stdin. That is why you cannot directly pipe man output to vim; as others have mentioned you have to use vim - to make vim read from stdin.

    However piping vimscripts can be useful too:

    vim test.txt <<EOF
    :%s/[aiueo]/X/g
    :wq! output.txt
    EOF
    

    The above will use vim to open test.txt, replace all vowels with X, write the results to output.txt, and quit (ignoring changes to the original file). It uses a here document but you can of course put the vim commands in a file and use vim test.txt < myscript or cat myscript | vim test.txt to achieve the same result.

    I suspect the reason they did it this way was that you can open multiple input files but only execute one script. If input was read from stdin by default, you could only read one buffer that way.

    0 讨论(0)
  • 2020-12-12 20:05

    I combined others answers, I am using

    vman() {
        export MANPAGER="col -b" # for FreeBSD/MacOS
    
        # Make it read-only
        eval 'man $@ | vim -MR +"set filetype=man" -'
    
        unset MANPAGER
    }
    

    Usage:

    vman ls
    
    0 讨论(0)
  • 2020-12-12 20:08

    I have a better solution, the one that I used, it is like this:

    /bin/sh -c "unset PAGER;col -b -x | vim -R -c 'set ft=man nomod nolist' -c 'map q :q<CR>' -c 'map <SPACE> <C-D>' -c 'map b <C-U>' -c 'nmap K :Man <C-R>=expand(\"<cword>\")<CR><CR>' -"
    

    Hope you'll enjoy it.

    0 讨论(0)
  • 2020-12-12 20:13

    Your example code is wrong.

       tempo=`mktemp`
       man $1 > $tempo; vi $tempo
    

    But you really only need

       man $1 | vi -
    
    0 讨论(0)
  • 2020-12-12 20:14

    Vim includes a man page viewer, :Man, in its runtime files.

    Put this line in your vimrc:

    runtime! ftplugin/man.vim
    

    Now you can read syntax-highlighted man pages inside Vim by running :Man. For example:

    :Man 3 printf
    

    Even better, you can just place your cursor on a word in the buffer and press <Leader>K (\K) to see the man page for that word.

    See :h find-manpage for complete usage and installation instructions.

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