4

When I use vim to edit a markdown file I have nice syntax coloring (via vim-markdown and vim-markdown-preview plug-ins).

I would like to have this same syntax coloring when I am using less (pager) to view a file. Is there a way to get less to do this syntax coloring? or is there another pager that has this functionality?

MERM
  • 629
  • 7
  • 21
  • 4
    I also looked at [highlight](http://www.andre-simon.de/doku/highlight/en/highlight.php) which works similarly and has nicer (imho) output colors. `LESSOPEN="| /usr/local/bin/highlight --out-format=xterm256 %s"` – MERM Jan 10 '15 at 03:12
  • 1
    Also, found [vimpager](https://github.com/rkitover/vimpager) which gives me the same syntax coloring as vim :)) It works well if you use a stripped down version of your .vimrc as .vimpagerrc See [here](http://vim.wikia.com/wiki/Using_vim_as_a_syntax-highlighting_pager) for some suggestions. – MERM Jan 10 '15 at 03:16
  • I wound up setting up my bash config so my PAGER so that it would prefer vimpager or less using highlight, then src-hilite-lesspipe.sh. – MERM Jan 29 '15 at 21:52
  • You should put your first comment into an answer - does a better job (actually answers the question) than the accepted answer – jmetz Sep 28 '22 at 13:49
  • Suggestion: Use more comments in your code. Because: If another user has Deuteranopia, colours may seem similar and/or may not have enough contrast with the background colour. [WCAG: Accessible colour and contrast ratios](https://bootcamp.uxdesign.cc/wcag-accessible-colour-and-contrast-ratios-5e94ea3f81f4 "WCAG: Accessible colour and contrast ratios"). [Designing the Terminal for color accessibility](https://www.bloomberg.com/ux/2021/10/14/designing-the-terminal-for-color-accessibility/ "Designing the Terminal for color accessibility"). – Stef Feb 23 '23 at 11:14

5 Answers5

2

Here is a way to view GitHub markdown files in a console using less.

  1. Install pandoc and lynx.

    a. For Ubuntu (and WSL): sudo apt install pandoc lynx

    b. For MacOS: brew install pandoc lynx

  2. Name the following script file as less and save it somewhere on the PATH, for example /usr/local/bin

  3. Make the script executable, for example chmod a+x /usr/local/bin/less
  4. Type hash -r or /usr/bin/less will run instead of this script
#!/bin/bash

if [ "${1##*.}" == md ]; then
  pandoc "$1" | lynx -stdin
else
  /usr/bin/less "$1"
fi
Mike Slinn
  • 7,705
  • 5
  • 51
  • 85
1

You could use e2ansi, a package that use Emacs in batch mode to perform syntax highlighting. You can configure less to use it by defining the LESSOPEN environment variable.

To get Emacs to syntax highlight MarkDown, you will also need to install markdown-mode.

(Finally, to get the same syntax highlighting in less as you have in your editor, I guess you would have to switch to Emacs ;) Of course, given that Emacs comes with a Vim compatibility package called Evil Mode, it might not be as bad as it seems.)

Lindydancer
  • 25,428
  • 4
  • 49
  • 68
0

With Vim Markdown highlighting (and folding) up and running, the most straightforward solution is to evoke vim in the read only mode with either vim -R, or (at least on Ubuntu) more elegantly:

$ view filename.md

Add the following at the very bottom of your .vimrc file, and view will behave just like less with the added benefit of your favourite syntax highlighting (not only for markdown!) and folding:

" less behaviour for view
" https://stackoverflow.com/a/39836959/2192488

" http://vim.wikia.com/wiki/Using_vim_as_a_syntax-highlighting_pager
function! LessBehaviour()
    if (!&modifiable || &ro)
        set nonumber
        set nospell
        set laststatus=0    " Status line
        set cmdheight=1
        set guioptions=aiMr    " No menu bar, nor tool bar
        noremap u <C-u>
        noremap d <C-d>
        noremap q :q<CR>
    endif
endfunction

" https://vi.stackexchange.com/a/9101/3168
augroup ReadOnly
    au!
    au VimEnter * :call LessBehaviour() 
augroup END

There exists also a more rigorous less.sh script. On my system, it comes packaged with vim. To find it, use:

$ find /usr/share/vim -name less.sh

However, contrary to the script listed above, folding will not work with this less.sh.

Serge Stroobandt
  • 28,495
  • 9
  • 107
  • 102
0

I now use, from best to worse, vimpager, highlight, src-hilite-lesspipe.sh, less, and then more; using bash to determine what is available on whichever system I am on. Here is what I finally arrived at:

.bash_profile:

#############################################
#   Pager                                   #
#############################################
PAGER='more'
export PAGER

if (have less); then
  LESS='-i -R -e -X -z-2 -M -P%t?f%f :stdin .?pb%pb\%:?lbLine %lb:?bbByte %bb:-...'
  LESSCHARSET='utf-8'
  LESSHISTFILE=-
  PAGER='less'
  MANPAGER='less'

  export LESS LESSCHARSET LESSHISTFILE PAGER MANPAGER

  export LESS_TERMCAP_so="$(tput bold; tput setaf 0; tput setab 4)"    # begin standout
  export LESS_TERMCAP_se="$(tput sgr0)"                                # end   standout
  export LESS_TERMCAP_us="$(tput smul; tput setaf 3)"                  # begin underscore
  export LESS_TERMCAP_ue="$(tput rmul; tput sgr0)"                     # end   underscore
  export LESS_TERMCAP_mr="$(tput rev)"                                 # begin reverse
  export LESS_TERMCAP_mb="$(tput blink)"                               # begin blink
  export LESS_TERMCAP_mh="$(tput dim)"                                 # begin dim
  export LESS_TERMCAP_md="$(tput bold; tput setaf 6)"                  # begin bold
  export LESS_TERMCAP_ZH="$(tput sitm)"                                # begin italics
  export LESS_TERMCAP_ZR="$(tput ritm)"                                # end   italics
  export LESS_TERMCAP_me="$(tput sgr0)"                                # end all modes

# make less more friendly for non-text input files, see lesspipe(1)
  [ -x /usr/bin/lesspipe ] && eval "$(lesspipe)"

  if (have src-hilite-lesspipe.sh); then
    export LESSOPEN="| src-hilite-lesspipe.sh %s"
  fi

  if (have highlight); then
    export LESSOPEN="| highlight --out-format=xterm256 --style=solarized-dark %s"
  fi

fi

if (have vimpager); then
  export PAGER='vimpager'
fi

if (have vimcat); then
  alias v='vimcat'
fi
MERM
  • 629
  • 7
  • 21
-3

less itself doesn't support syntax highlighting, but you could use GNU-source-highlight which does.

First, you'd need to install it. E.g., on RHEL/Centos/Fedora (as root):

$ yum install source-highlight

On Debian/Ubuntu

$ apt-get install source-highlight

Then configure your pager to use it:

$ export LESSOPEN="| /usr/bin/src-hilite-lesspipe.sh %s"
$ export LESS=" -R"

Now, it should just replace less:

$ less README.md # pager with syntax highlighting
Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • 4
    As anyone actually had results with this method? On ubuntu 14.04 this does add syntax highlighting for less, but it does not support Markdown ; it makes sense since Markdown is not listed on https://www.gnu.org/software/src-highlite/ – gou1 Feb 24 '15 at 13:13
  • 2
    There is an open bug for Markdown support here: http://savannah.gnu.org/bugs/?46542 – Daniel Compton Dec 07 '16 at 22:09
  • 1
    It should be : apt-get install source-highlight – muenalan May 25 '17 at 06:49
  • 1
    @muenalan indeed, not sure how I missed that. Edited and fixed. Thanks for noticing! – Mureinik May 25 '17 at 07:45
  • 2
    As of today, source-highlight still does not support markdown (which actually makes this answer *not* answer the original question). – René Mar 29 '20 at 11:12