I need to use the
less command with the syntax highlighting of the vim command for
bash and other languages.
How do I apply syntax highlighting colors according to
vim colors for
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.Sign up to join this community
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
Even on Cygwin you can do it with the minor adjustment of the shell script path and installing with
apt-cyg instead of
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 '
-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
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:
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.
the main difference between
vi is that view doesn't "lock" the file you're viewing by creating a .swp file.
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
.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.
less requires third-party tools 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.
If you're already using Vim, the consistency (and support of almost any file type) makes this worth a try.
After dabbling in some of the answers above, I think the most configuration-free solution is to just install and use
After install you should be able to immediately use
vimpager instead of
less at your shell.
vimpager in other instances where
less is used, you can change the
PAGER variables your shell config (
.bashrc etc.) by adding:
export PAGER=/usr/local/bin/vimpager export MANPAGER=/usr/local/bin/vimpager
The install paths may (need to?) be different on OSX, but this works fine on Arch linux.
You can then set an
alias to always substitute
vimpager, in the same file:
(As described at the github readme)