The Computer Oracle

How to syntax highlight via Less

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: Underwater World

--

Chapters
00:00 Question
00:48 Accepted answer (Score 160)
01:27 Answer 2 (Score 138)
01:43 Answer 3 (Score 41)
02:02 Answer 4 (Score 22)
02:42 Thank you

--

Full question
https://superuser.com/questions/71588/ho...

Accepted answer links:
[GNU's source-highlight]: http://www.gnu.org/software/src-highlite/
[here]: http://www.gnu.org/software/src-highlite...

Answer 3 links:
[Pygments Python syntax highliter]: http://pygments.org/

Answer 4 links:
[I improved it]: https://github.com/huyz/less.vim
[my blog post on it]: https://web.archive.org/web/201304092017.../
[original]: http://huyz.us/2011/a-less-like-pager-wi.../
[image]: https://i.stack.imgur.com/4EZlX.png

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#syntaxhighlighting #less

#avk47



ACCEPTED ANSWER

Score 168


You can use GNU's source-highlight, as shown here (path may differ, see below):

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

As of Debian Stretch and Fedora 25, package names and script paths differ

  • Debian:

    sudo apt install libsource-highlight-common source-highlight
    dpkg -L libsource-highlight-common | grep lesspipe
    # /usr/share/source-highlight/src-hilite-lesspipe.sh
    
  • Fedora:

    sudo dnf install source-highlight
    rpm -ql source-highlight | grep lesspipe
    # /usr/bin/source-highlight/src-hilite-lesspipe.sh
    



ANSWER 2

Score 146


Best of both previous answers: you can invoke system default editor from within less, by pressing v.




ANSWER 3

Score 42


pygmentize somefile.ex | less -R

or

function cless () {
    pygmentize -f terminal "$1" | less -R
}

Pygmentize comes as a part of the Pygments Python syntax highliter.




ANSWER 4

Score 17


I think that you should use a text editor. I like vim myself. That will give you LOTS of power when viewing files and then when you want to edit them you will already know the basics.

Here are some of the advantages of using a text editor (specifically vim):

  • syntax-highlighting
  • powerful movement commands
  • find
  • jump to specific location in a file (called a mark)
  • folding (useful when you just want to see function stubbs)

To open your file in readonly mode use this:

vim -R <file name>

Here is a basic navigation guide:

j - move down one line
k - move up one line
h - left one char
l -right one char

ctrl-f - forward one page
ctrl-b - back one page

/<something> - search for something
n - next of whatever you searched for
N - next (search backwards) of whatever you searched for

:q - quit
:q! - quit without saving
:w - save

Here is a link for more information:

http://www.viemu.com/a_vi_vim_graphical_cheat_sheet_tutorial.html

Just to recap, if you will use unix vim is pretty fundamental. I have heard that learning vim is like learning to type. It is the next most useful tool you can learn for programming.

(Just to avoid editor wars you could also look into emacs or another editor, however I personally prefer vim)