less command and syntax highlighting
Solution 1
Syntax highlighting of less
, works just fine on most *nix systems.
apt install source-highlight
export LESSOPEN="| /usr/share/source-highlight/src-hilite-lesspipe.sh %s"
export LESS=' -R '
On Fedora/RedHat based distros use /usr/bin/src-hilite-lesspipe.sh
instead.
Even on Cygwin you can do it with the minor adjustment of the shell script path and installing with apt-cyg
instead of apt
.
However, using this drastically slows down browsing of large files. I suggest to use alias
in such a way to only implement the LESSOPEN
export above when needed, like this:
alias lessh='LESSOPEN="| /usr/bin/src-hilite-lesspipe.sh %s" less -M '
where the -M
flag is convenient to also show filename and line number.
Also remember to copy the script into your bin path:
cp /usr/share/source-highlight/src-hilite-lesspipe.sh /usr/bin/src-hilite-lesspipe.sh
UPDATE: 2019-07-24
Apparently, on more recent Cygwin installs, you have the following files in your path:
source-highlight.exe
source-highlight-esc.sh
source-highlight-settings.exe
So now you also need to execute the source-highlight-settings.exe
that will add the configuration file:
$HOME/.source-highlight/source-highlight.conf
.
Solution 2
less
doesn't support syntax highlighting.
vim
, like all vi
clones has a read-only mode called view
which you can use to just view files. it supports all features of vim including syntax highlighting.
e.g.
view filename.py
the main difference between view
and vi
is that view doesn't "lock" the file you're viewing by creating a .swp file.
Solution 3
I tend to disagree with Ingo, less
can be taught to highlight syntax. Check out this answer on SuperUser. Basically, you have to install GNU's source-highlight (available in all major distro package repos), and then add the following to your .bashrc
(or .bash_profile
or what have you):
export LESSOPEN="| /path/to/src-hilite-lesspipe.sh %s"
export LESS=" -R "
However, note that source-highlight
is not as powerful as vim
's highlighter. Use whatever suits you best.
Solution 4
less
cannot be taught to highlight syntax elements, but Vim can be used as a pager, i.e. a replacement for less
. There are more advanced plugins, but the basic script actually ships with Vim ($VIMRUNTIME/macros/less.sh
). For the full information, see Using vim as a syntax-highlighting pager on the Vim Tips Wiki.
PersianGulf
My God is Allah and my religious book is Quran.Up to now my life was based on my God's will and whatever I gained or lost is due to my God's want. My aspiration is to put my life in favour of my lord, Allah since I believe this would be a desirable life. I am a fan and supporter of Free Software and i do all my computer works based on Freesoftware. My other interests are Linux/GNU family and BSD. My main speciality is network administration but I also have decent knowledge in C/C++ and Python progmming. In general I love programming languages. My favorite sport is mountain climbing because I love outdoors and generaly I'm a nature lover. I truely love to increase my knowledge because I'm eager to know as much as possible. Not only I have this desire for myself, but also for every human being and I will do my best to help anyone who wishes to be on this path. (You can find everythinf about me at http://pahlevanzadeh.net)
Updated on September 18, 2022Comments
-
PersianGulf over 1 year
I need to use the
less command
with the syntax highlighting of the vim command forpython
,C
,bash
and other languages.How do I apply syntax highlighting colors according to
vim
colors forless command
? -
PersianGulf over 10 yearsif your
view command
means fromradare package
, it's not read only -
Felix over 10 yearsHmm, my
view
command doesn't seem to support syntax highlighting. I've tried:syntax enable
and:syntax on
, but I always getsyntax: Not an editor command
. Myvim
supports highlighting, though. -
Alessio over 10 years@felix - do you have another
vi
likenvi
orelvis
installed? check your/usr/bin/view
- on my debian system, it's a symlink to/etc/alternatives/view
which is, in turn, a symlink to/usr/bin/vim.basic
. -
Felix over 10 yearsHmm, in my case it's a symlink to
/usr/bin/ex
, which, curiously, is owned by thevi
package. -
Alessio over 10 yearsah, but which
vi
package? vim? elvis? nvi? -
not2qubit almost 10 yearsI'm surprised people doesn't even bother to Google before posting wrong answers...
-
Ingo Karkat almost 10 years@user1147688: If you carefully read Felix's answer, the GNU source-highlight is an external program, and the integration with less is very loose. It's not
less
that does the highlighting; but Vim indeed has this capability built-in. -
SMBiggs over 9 yearsThe alias 'lessh' doesn't work for me. Running bash on osx. Sigh.
-
not2qubit about 9 years
-
x-yuri almost 9 yearsI can confirm what @Felix says, on
arch linux
view
is a symlink to/usr/bin/ex
ofcore/vi
package. -
x-yuri almost 9 yearsSee this answer:
alias vless='vim -u $VIMRUNTIME/macros/less.vim'
-
michael over 7 years+1 but I'm sure what these comments are trying to say -- can't tell if they're even positive or negative ("...google before posting wrong answers"?, "installing xyz is more correct than just using vim"?). This answer is clearly the "most correct answer". Basically the OP didn't know this feature of vim existed, and this answer lets the OP know that it exists. Occam's razor ftw. The answer could only be improved by showing an example, e.g.,
$ diff file1.c file2.c | /usr/share/vim/vim74/macros/less.sh
-
mhvelplund over 5 yearsThis worked for me on OSX: first I installed with:
brew install source-highlight
, then I added an alias:alias lessh='LESSOPEN="| src-hilite-lesspipe.sh %s" less -R '
-
Michael over 5 yearsTo use highlighting in a pipe, like
grep something somewhere | less
use this on vi:grep something somewhere | view -
-
Usman Khan almost 4 yearsErm,
less
does support syntax highlighting on 3 out of 3 of my systems...