2

Problem

I want to view man-pages in colour, preserving nroff's bold and italic directives.

How would I edit a buffer's text and setup highlight regions:

  • manpage_bold: For all instances of X <backspace> X:

    • Conceal / remove the final 2 characters, leaving only the first.
  • manpage_italic: For all instances of _ <backspace> X:

    • Conceal / remove the leading two characters, leaving only the last.

Where X is any character other than backspace.


Background

I have less configured to display manual pages in colour:

Coloured man page example

This is achieved by telling less to replace overstrikes that groff/nroff produces to indicate bold / italics with ANSI SGR colour codes.

I want vim to be able to do the analogous thing: view man pages with the original bold / italics by removing the overstrikes and setting up highlight regions.

Overstriking

What's an overstrike I hear you ask? *takes deep breath*

Unix is a venerable old operating system, dating from the times when teletypes hammered inked ribbon, imprint characters on paper.

Bold characters were printed by printing the character, backspacing, then printing the same character again - twice as much ink was impressed upon the paper.

Italic characters are indicated by printing an underscore (_), backspace and then the character to be rendered. (source)

Over 40 years later, man still uses nroff to generate the text that is displayed from the troff man page source file... and nroff still uses overstriking to indicate boldface / italics.


Non-solutions

neoman and vim-man-pager only colour by regular expression- they remove the nroff bold / italic directives which are useful in warnings, etc.

When using GROFF_SGR=1 to produce ANSI SGR codes rather than overstrikes, AnsiEsc is needed to display the output, and this doesn't work with set ft=man syntax highlighting.

  • 1
    you might want to check my plugin Colorizer (at least I just added highlighting for those nroff sequences mentioned here – Christian Brabandt Aug 21 '17 at 7:53
  • Thanks, but that doesn't strip the control characters - I can't use Ctrl-K to jump to the referenced man page. Also, it's noticeably slower than my less setup. – Tom Hale Aug 30 '17 at 6:23
  • of course it doesn't. It is supposed to hide them using concealing. and yes concealing is known to be slow, especially with long documents and many ansi control characters. – Christian Brabandt Aug 30 '17 at 7:06
2

It is not entirely clear from your question+comments if you want to remove the original control sequences bofore applying syntax highlighting in vim. Here are some solutions/alternatives.

From inside the editor:

Neovim ships with a :Man command that displays man pages and does all the magic for you. (This one removes the original control characters).

For plain vim you might use a plugin like vim-man to achive the same.

From the command line:

If you want to use vim or neovim as a pager from the command line you can use vimpager or nvimpager. Both can handle and highlight man pages by default (nvimpager relies on the code from neovim for that). Vimpager works with vim and neovim, nvimpager is faster but only for neovim.

(Disclosure: I am the developer of nvimpager and have also helped in development with vimpager.)

DIY:

You can look up the implementation in Neovim or vim-man to see how they do it and adapt to your needs.

|improve this answer|||||
  • Thanks! I'm now using neovim and :Man seems to do the trick, however I'd like to be able to change the underline to a different colour as it looks ugly to my eyes. How do I find out the SyntaxRegionname of both bold and underline? – Tom Hale Aug 22 '18 at 6:16
  • 1
    You can use the synID() and synIDattr() functions to find the syntax group names and other attributes of any line and char in the file. – Lucas Aug 22 '18 at 12:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.