The Computer Oracle

How to syntax highlight via Less

--------------------------------------------------
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
and get $2,000 discount on your first invoice
--------------------------------------------------

Take control of your privacy with Proton's trusted, Swiss-based, secure services.
Choose what you need and safeguard your digital life:
Mail: https://go.getproton.me/SH1CU
VPN: https://go.getproton.me/SH1DI
Password Manager: https://go.getproton.me/SH1DJ
Drive: https://go.getproton.me/SH1CT


Music by Eric Matyas
https://www.soundimage.org
Track title: Thinking It Over

--

Chapters
00:00 How To Syntax Highlight Via Less
00:36 Accepted Answer Score 168
01:05 Answer 2 Score 17
02:13 Answer 3 Score 146
02:23 Answer 4 Score 42
02:35 Thank you

--

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

--

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)