8

Is there a way to "colorize" manpages output? I mean - the output of for example:

man command_name

5 Answers 5

9

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' -
}
5
  • 1
    and finally I won't be pressing j by mistake all the time
    – daithib8
    Apr 18, 2011 at 16:24
  • 1
    Well, j works fine in less as well :) Apr 18, 2011 at 16:26
  • 1
    But you should change $* to "$@" to avoid potential problems with white space. Otherwise, thanks a bunch! Apr 21, 2011 at 13:09
  • 1
    I suggest a little modification, to get more close to less style: substitute vim with /usr/share/vim/vim72/macros/less.sh.
    – enzotib
    Apr 25, 2011 at 9:07
  • @enzotib as versions change, use /usr/share/vim/vim*/macros/less.sh will be version proof, as of 2020
    – laviex
    May 25, 2020 at 23:27
5

add export PAGER=most to .bashrc

2
  • 1
    this is great, but is there a way to make it work for HELP and INFO commands ?
    – user111764
    May 27, 2016 at 8:56
  • 1
    It's export PAGER="most" (with quotation marks), and you'll need to install it first using apt-get install most (cf. cyberciti.biz/faq/unix-linux-color-man-pages-configuration).
    – Kurt Peek
    Jan 17, 2017 at 14:25
4

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
1
  • 1
    Why man pages in virtual terminal have a minimum of color (cyan for bold), and in graphic terminal are black and white?
    – enzotib
    Apr 18, 2011 at 7:01
3

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

2
  • 1
    wow. didn't know one can do that.
    – Stann
    May 24, 2011 at 15:12
  • 1
    Does any other browser support this?
    – udiboy1209
    Apr 7, 2014 at 5:52
3

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .