Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add highlighting for manpages? #523

Closed
LunarLambda opened this issue Mar 9, 2019 · 15 comments
Closed

Add highlighting for manpages? #523

LunarLambda opened this issue Mar 9, 2019 · 15 comments
Labels
help wanted Extra attention is needed question Further information is requested syntax-highlighting

Comments

@LunarLambda
Copy link
Contributor

I would like to use bat as my MANPAGER. I am not a fan of most(1), my current alternative is using nvim(1) with the :Man! command.

@sharkdp
Copy link
Owner

sharkdp commented Mar 14, 2019

Thank you for the feedback! I thought a little bit about this and played around with nvims version, which is really cool! If you set

export MANPAGER="nvim -c 'set ft=man' -"

you don't need to call :Man! manually. If I understand this correctly, they basically have a specialized vim syntax (https://github.com/neovim/neovim/blob/master/runtime/syntax/man.vim) which highlights certain parts of mans output (e.g. section headings like PROLOG, SYNOPSIS, COPYRIGHT, …).

One thing which is really neat is that they highlight the SYNOPSIS part with C syntax if the man page comes from a corresponding section.

I'm not really sure if there is something that we could do even better than nvim, but that's definitely something we could look into. I think we might be able to write a specialized Sublime syntax to do the same. We would then simply need to set MANPAGER="bat -p -l man".

@sharkdp sharkdp added help wanted Extra attention is needed question Further information is requested syntax-highlighting labels Mar 14, 2019
@LunarLambda
Copy link
Contributor Author

I have MANPAGER set like that already (-c Man! works too on the version I have), which works okay but isn't ideal for me.

Thank you for considering it!

@sharkdp
Copy link
Owner

sharkdp commented Mar 14, 2019

which works okay but isn't ideal for me.

What are changes or features you are looking for?

@LunarLambda
Copy link
Contributor Author

LunarLambda commented Mar 14, 2019

I'm used to less controls, which nvim emulates somewhat when viewing manpages but not perfectly (mostly scrolling is missing), and it's visually a little distracting to me. I'd mostly like the highlighting as a reading aid.

Also would in general be nice to decouple this from nvim since not everyone uses it and I've increasingly been trying different editors recently.

There's also most as an alternative but only has special highlighting for manpages, and I already use bat for displaying / skimming through files.

@sharkdp
Copy link
Owner

sharkdp commented Mar 14, 2019

I've been playing around with this for a little bit. Here is a rather hacky first version.

If somebody wants to try it (or even work on this):

Step 1

Save the following syntax as ~/.config/bat/syntaxes/Man.sublime-syntax.

%YAML 1.2
---
# http://www.sublimetext.com/docs/3/syntax.html
name: Man
file_extensions:
  - man
scope: source.man

variables:
  section_heading: '^\S.*$'

contexts:
  main:
    - match: ^
      push: first_line

  first_line:
    - match: '([A-Z0-9]+)(\()([^)]+)(\))'
      captures:
        1: meta.preprocessor
        2: keyword.operator
        3: string.quoted.other
        4: keyword.operator

    - match: '$'
      push: body

  body:
    - match: '^(SYNOPSIS|SYNTAX|SINTASSI|SKŁADNIA|СИНТАКСИС|書式)'
      push: Packages/C/C.sublime-syntax
      scope: markup.heading
      with_prototype:
        - match: '(?={{section_heading}})'
          pop: true

    - match: '^\S.*$'
      scope: markup.heading

    - match: '\b([a-z0-9_]+)(\()([^)]*)(\))'
      captures:
        1: entity.name.function
        2: keyword.operator
        3: constant.numeric
        4: keyword.operator

Step 2

Run

bat cache --build

Step 3

Set up the pager:

export MANPAGER="sh -c 'sed -e s/.\\\\x08//g | bat -l man -p'"

Step 4

Run man. For now, we do not make any decision based on the man section.

man 2 select
man  fopen
…

@LunarLambda
Copy link
Contributor Author

LunarLambda commented Mar 14, 2019 via email

@sharkdp
Copy link
Owner

sharkdp commented Mar 14, 2019

Here is how it looks like (if everything goes well):

image

@LunarLambda
Copy link
Contributor Author

LunarLambda commented Mar 14, 2019 via email

@YodaEmbedding
Copy link

YodaEmbedding commented May 16, 2019

Slightly missed opportunity for the title "bat man page highlights". :P

@sharkdp
Copy link
Owner

sharkdp commented Aug 31, 2019

I have added my initial version from above to the repository so that everyone can contribute to the further development. See README for instructions.

@LunarLambda
Copy link
Contributor Author

LunarLambda commented Aug 31, 2019 via email

@sharkdp
Copy link
Owner

sharkdp commented Aug 31, 2019

Released in bat v0.12.

@dufferzafar
Copy link

I tried using bat as my man pager, but it currently lacks the highlight of command line options..

@sharkdp
Copy link
Owner

sharkdp commented Mar 19, 2020

it currently lacks the highlight of command line options..

this feature has not been released yet: #718. If you want to help with the next release, please see #845

@danisztls
Copy link

danisztls commented May 29, 2021

Another advantage to bat over neovim is that it is, anecdotally, faster to open.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed question Further information is requested syntax-highlighting
Projects
None yet
Development

No branches or pull requests

5 participants