Highlight manpages syntax?

Solution 1:

I use the following shell function to view manpages in Vim, which provides nice syntax highlighing:

man() {
  /usr/bin/man $* | \
    col -b | \
    vim -R -c 'set ft=man nomod nolist' -
}

Solution 2:

add export PAGER=most to .bashrc

Solution 3:

aha. adding this to ~/.bashrc does the trick. It's not perfect though. but hey:

# Less Colors for Man Pages 
export LESS_TERMCAP_mb=$'\E[01;31m'       # begin blinking 
export LESS_TERMCAP_md=$'\E[01;38;5;74m'  # begin bold 
export LESS_TERMCAP_me=$'\E[0m'           # end mode 
export LESS_TERMCAP_se=$'\E[0m'           # end standout-mode 
export LESS_TERMCAP_so=$'\E[38;5;246m'    # begin standout-mode - info box 
export LESS_TERMCAP_ue=$'\E[0m'           # end underline 
export LESS_TERMCAP_us=$'\E[04;38;5;146m' # begin underline

Solution 4:

Does it have to be in the terminal?

Else you can use: System->Help->System Documentation and on the left of the window that comes up click "Manual Pages" (found this tip here)

Or you can use Konqueror, which shows the man pages like this: Man page in Konqueror

Solution 5:

The answer Adam Byrtek seems to limit lines to 80 characters and doesn't reflow text when you resize your terminal.

You can fix this by setting MANPAGER instead. You can also set PAGER like daithib8 does but that is more general. MANPAGER overrides PAGER for the man command.

From this

export MANPAGER="col -b | vim -c 'set ft=man ts=8 nomod nolist nonu' -c 'nnoremap i <nop>' -"

He has some pretty good extra options such as setting ts (tabstop), nonu (nonumber), and unmapping i (insert). So I will merge the two solutions. Also made some improvements like using the command command and expanding arguments with "$@"

As a function:

man() {
  MANPAGER="col -b | vim -R -c 'set ft=man ts=8 nomod nolist nonu' -c 'nnoremap i <nop>' -" command man "$@"
}

EDIT: scroll down to my UPDATE to see benefits of using this messy form and a cleaner way to write the function with some extra vim mappings.


But doing all this is a really big mess. The following solutions require installation but they are cleaner solutions.

These solutions don't have the Vim: Reading from stdin... that the above solution has.

I recommend daithib8's answer except to use MANPAGER instead of PAGER since this question is just about manpages. And I would also add it to my .bash_profile or .profile so it doesn't get exported on each invocation of bash. You will have to install most though.

# In .profile or .bash_profile
MANPAGER='most'

most doesn't have hjkl movement like in vim and less, but it is the fastest.

Also, vimpager is pretty good. I like using it for huge manpages such as man bash. You get all the cool things from vim such as line numbers number, highlighted search hlsearch, and all your vim plugins.

If you have YouCompleteMe installed vimpager also doesn't show this message:

The ycmd server SHUT DOWN (restart with ':YcmRestartServer'). Unexpected exit code 1. Type ':YcmToggleLogs ycmd_54959_stderr_UvwUrj.log' to check the logs.

Not sure why, but it's nice not to see that.

# In .profile or .bash_profile
export MANPAGER='vimpager'

Startup can be a bit slower than most because vimpager sources your .vimrc, which can be a good thing, if you want to use your vim plugins. To disable .vimrc sourcing:

# In .profile or .bash_profile
export MANPAGER='vimpager -u NONE'

You can also do a hybrid approach with functions:

# Use 'command man' instead of 'man' if you have overridden
# 'man' with a function, like how I did in the "messy" solutions above.

manmost() {
    MANPAGER='most' man "$@"
}

manvim() {
    MANPAGER='vimpager -u NONE' man "$@"
}

manvimrc() {
    MANPAGER='vimpager' man "$@"
}

So now, export MANPAGER will set your default MANPAGER in your .bash_profile or .profile, but you can use any one of these functions to quickly use another MANPAGER.


UPDATE

Actually, vimpager is a little different than actual vim. For example, in normal mode, w is mapped to b and b is mapped to & <C-B><SNR>1_L. But this is how other pagers are - less doesn't have a cursor.

If you would like to use actual vim, with a messy solution, I found this. It suppresses the Vim: Reading from stdin... with a hack. Still has the YouCompleteMe message though. You can probably set another -c command to do turn off YouCompleteMe. But I'll just ignore that message.

manrealvim() {
  MANPAGER='bash -c "vim -MRn -c \"set ft=man nomod nolist nospell nonu\" -c \"nm q :qa!<CR>\" -c \"nm <end> G\" -c \"nm <home> gg\"</dev/tty <(col -b)"' man "$@"
}

In newer vim versions, you can avoid this hack with --not-a-term:

I cleaned up the function with newline escapes because this is the version I am using.

manvim() {
  MANPAGER='col -b | '\
'vim -MR -c "set ft=man ts=8 nomod nolist nospell nonu" '\
'-c "set colorcolumn= hlsearch incsearch" '\
'-c "nun <buffer> q" '\
'-c "nn q :qa!<CR>" '\
'-c "nn <end> G" '\
'-c "nn <home> gg" '\
'- --not-a-term' man "$@"
}

Or you can just use neovim > v0.2.2:

# not sure why but the -M flag breaks it but it's not required
# Seems like nvim already sets -M when ft=man
mannvim() {
  MANPAGER='col -b | '\
'nvim -R -c "set ft=man ts=8 nomod nolist nospell nonu" '\
'-c "nun <buffer> q" '\
'-c "nn q :qa!<CR>" '\
'-c "nn <end> G" '\
'-c "nn <home> gg" '\
'-' man "$@"
}

You might want to look through the vim options passed to customize. I looked through them from all those posts I listed and chose the ones I liked. Or you can just copy me.

You could also parse vim --version to determine whether to use the hack or --not-a-term. I won't do that here.


MY CURRENT METHOD

If you would like to set this vim method as your default MANPAGER, do the following:

  1. Add these executable scripts in your path:

    There are two scripts because the first is for general paging (replacement for less), while the second is for manpaging (has manpage syntax highlighting.

    # Put this in ~/bin/vimrealpager
    # I call it vimrealpager to avoid name clashes with vimpager mentioned above
    # Make sure ~/bin/ is in your path
    
    # ***SCRIPT START***
    
    #!/usr/bin/env sh
    
    col -b |
    vim -MR -c 'set nomod nolist nospell nonu' \
    -c 'set colorcolumn= hlsearch incsearch' \
    -c 'nn q :qa!<CR>' \
    -c 'nn <end> G' \
    -c 'nn <home> gg' \
    "$@" \
    --not-a-term \
    -
    
    # Put this in ~/bin/vimrealmanpager
    # ***SCRIPT START***
    
    #!/usr/bin/env sh
    
    vimrealpager -c 'set ft=man' \
    -c 'nun <buffer> q'
    
  2. In your ~/.profile:

    export MANPAGER='vimrealmanpager'
    

You could also do export -f on the functions I showed earlier, but it works in bash but not POSIX shell, so you would have to put it in .bash_profile instead of .profile.

A similar thing can be done for nvim but I'm not gonna add it here unless requested.