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

我与影子孤独终老i 提交于 2019-12-03 02:25:55

问题


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 to work, bonus points to somebody who can explain why), and to get around this, I've been using the following little script:

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

This script uses temporary files which I guess works fine, but I was wondering if there was a good way to read man pages in vim without resorting to making temporary files


回答1:


For some reason, it seems that vim isn't able to read the output of programs through piping […]

According to the man-page, you need to specify a file of - to get it to read from standard input; so:

man ls | vi -

If that doesn't work, you might try using process substitution:

vi <(man $1)

which creates a sort of pseudo-file and passes it to vi.




回答2:


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.




回答3:


On my system (Mac OS X), I found that the above left control characters in the output. Instead I used:

export MANPAGER="col -b | vim -MR - "

then just e.g.

man vim

The vim options turn off modifying the buffer and make it read-only. This stops vim complaining if you try to exit with ":q" (you can use :q! of course, but you might as well set the options).

This is also handy for general use - I have the following. The -c command names the buffer, just for completeness.

alias vimpager="vim -MR -c 'file [stdin]' -"



回答4:


Your example code is wrong.

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

But you really only need

   man $1 | vi -



回答5:


Here is what I did, I've made function in my .bashrc with this

vman() { vim <(man $1); }

so when I call vman this automatically calls vim with stdin the man itself, it works great.




回答6:


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.




回答7:


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.




回答8:


You also can press shift-k on your c function to print the man page




回答9:


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



回答10:


You can always use info command for info pages and do info {cmd} | vim.

  • Source.


来源:https://stackoverflow.com/questions/16740246/what-is-a-way-to-read-man-pages-in-vim-without-using-temporary-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!