Bash – Viewing man pages in vim

bashfunctionmanvim

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

Best Answer

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

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