Get colors in less or more
When I read a file in Linux with the command less
or more
, how can I get the content in colors?
linux colors less
migrated from stackoverflow.com Mar 9 '10 at 10:50
This question came from our site for professional and enthusiast programmers.
add a comment |
When I read a file in Linux with the command less
or more
, how can I get the content in colors?
linux colors less
migrated from stackoverflow.com Mar 9 '10 at 10:50
This question came from our site for professional and enthusiast programmers.
5
This seems related: superuser.com/questions/36022/less-and-grep-color - does it help?
– Jonik
Mar 9 '10 at 13:40
4
The title of this question is very misleading. Many people landing on this page expect a solution to the coloring issue you will get when piping a command with colored output toless
: colors are lost. (The answers to that ”piping issue“ involveless -R
,unbuffer
etc.) But the actual question refers to opening a file! — The ambiguity lies primarily in the question's title, but even besides that, IMHO the question is still too broad: ”read a file“ could refer to any file (probably plain text). (well, ”get the content in colors“ is probably referring to syntax highlighting.)
– myrdd
Dec 3 '18 at 23:05
I need to correct myself, only 3 (or 4) of 14 answers are missing the OP's actual question: the answers by ChristopheD, Puneet and Onlyjob; and maybe jbbr. Still, two of those answers are part of the three highest-voted ones.
– myrdd
Dec 3 '18 at 23:38
add a comment |
When I read a file in Linux with the command less
or more
, how can I get the content in colors?
linux colors less
When I read a file in Linux with the command less
or more
, how can I get the content in colors?
linux colors less
linux colors less
edited Jul 11 '16 at 15:11
Mark Amery
1501112
1501112
asked Mar 9 '10 at 10:44
Open the wayOpen the way
2,096133965
2,096133965
migrated from stackoverflow.com Mar 9 '10 at 10:50
This question came from our site for professional and enthusiast programmers.
migrated from stackoverflow.com Mar 9 '10 at 10:50
This question came from our site for professional and enthusiast programmers.
5
This seems related: superuser.com/questions/36022/less-and-grep-color - does it help?
– Jonik
Mar 9 '10 at 13:40
4
The title of this question is very misleading. Many people landing on this page expect a solution to the coloring issue you will get when piping a command with colored output toless
: colors are lost. (The answers to that ”piping issue“ involveless -R
,unbuffer
etc.) But the actual question refers to opening a file! — The ambiguity lies primarily in the question's title, but even besides that, IMHO the question is still too broad: ”read a file“ could refer to any file (probably plain text). (well, ”get the content in colors“ is probably referring to syntax highlighting.)
– myrdd
Dec 3 '18 at 23:05
I need to correct myself, only 3 (or 4) of 14 answers are missing the OP's actual question: the answers by ChristopheD, Puneet and Onlyjob; and maybe jbbr. Still, two of those answers are part of the three highest-voted ones.
– myrdd
Dec 3 '18 at 23:38
add a comment |
5
This seems related: superuser.com/questions/36022/less-and-grep-color - does it help?
– Jonik
Mar 9 '10 at 13:40
4
The title of this question is very misleading. Many people landing on this page expect a solution to the coloring issue you will get when piping a command with colored output toless
: colors are lost. (The answers to that ”piping issue“ involveless -R
,unbuffer
etc.) But the actual question refers to opening a file! — The ambiguity lies primarily in the question's title, but even besides that, IMHO the question is still too broad: ”read a file“ could refer to any file (probably plain text). (well, ”get the content in colors“ is probably referring to syntax highlighting.)
– myrdd
Dec 3 '18 at 23:05
I need to correct myself, only 3 (or 4) of 14 answers are missing the OP's actual question: the answers by ChristopheD, Puneet and Onlyjob; and maybe jbbr. Still, two of those answers are part of the three highest-voted ones.
– myrdd
Dec 3 '18 at 23:38
5
5
This seems related: superuser.com/questions/36022/less-and-grep-color - does it help?
– Jonik
Mar 9 '10 at 13:40
This seems related: superuser.com/questions/36022/less-and-grep-color - does it help?
– Jonik
Mar 9 '10 at 13:40
4
4
The title of this question is very misleading. Many people landing on this page expect a solution to the coloring issue you will get when piping a command with colored output to
less
: colors are lost. (The answers to that ”piping issue“ involve less -R
, unbuffer
etc.) But the actual question refers to opening a file! — The ambiguity lies primarily in the question's title, but even besides that, IMHO the question is still too broad: ”read a file“ could refer to any file (probably plain text). (well, ”get the content in colors“ is probably referring to syntax highlighting.)– myrdd
Dec 3 '18 at 23:05
The title of this question is very misleading. Many people landing on this page expect a solution to the coloring issue you will get when piping a command with colored output to
less
: colors are lost. (The answers to that ”piping issue“ involve less -R
, unbuffer
etc.) But the actual question refers to opening a file! — The ambiguity lies primarily in the question's title, but even besides that, IMHO the question is still too broad: ”read a file“ could refer to any file (probably plain text). (well, ”get the content in colors“ is probably referring to syntax highlighting.)– myrdd
Dec 3 '18 at 23:05
I need to correct myself, only 3 (or 4) of 14 answers are missing the OP's actual question: the answers by ChristopheD, Puneet and Onlyjob; and maybe jbbr. Still, two of those answers are part of the three highest-voted ones.
– myrdd
Dec 3 '18 at 23:38
I need to correct myself, only 3 (or 4) of 14 answers are missing the OP's actual question: the answers by ChristopheD, Puneet and Onlyjob; and maybe jbbr. Still, two of those answers are part of the three highest-voted ones.
– myrdd
Dec 3 '18 at 23:38
add a comment |
15 Answers
15
active
oldest
votes
You can utilize the power of pygmentize with less - automatically! (No need to pipe by hand.)
Install pygments
with your package manager or pip (possibly called python-pygments
) or get it here http://pygments.org/download/.
Write a file ~/.lessfilter
#!/bin/sh
case "$1" in
*.awk|*.groff|*.java|*.js|*.m4|*.php|*.pl|*.pm|*.pod|*.sh|
*.ad[asb]|*.asm|*.inc|*.[ch]|*.[ch]pp|*.[ch]xx|*.cc|*.hh|
*.lsp|*.l|*.pas|*.p|*.xml|*.xps|*.xsl|*.axp|*.ppd|*.pov|
*.diff|*.patch|*.py|*.rb|*.sql|*.ebuild|*.eclass)
pygmentize -f 256 "$1";;
.bashrc|.bash_aliases|.bash_environment)
pygmentize -f 256 -l sh "$1";;
*)
if grep -q "#!/bin/bash" "$1" 2> /dev/null; then
pygmentize -f 256 -l sh "$1"
else
exit 1
fi
esac
exit 0
In your .bashrc
add
export LESS='-R'
export LESSOPEN='|~/.lessfilter %s'
Also, you need to make ~/.lessfilter
executable by running
chmod u+x ~/.lessfilter
Tested on Debian.
You get the idea. This can of course be improved further, accepting more extensions or parsing the shebang for other interpreters than bash. See some of the other answers for that.
The idea came from an old blog post from the makers of Pygments, but the original post doesn't exist anymore.
6
If you want to have coloring of the source code files, you also need to make ~/.lessfilter executable by runningchmod u+x ~/.lessfilter
. You also need to have pygmentize (pygments.org/download) installed.
– Sergiy Belozorov
Dec 18 '12 at 11:07
Can anyone confirm that this works as it has no effect for me when I execute a command likels -l | less
– puk
Oct 30 '13 at 13:59
7
@puk you can do something likels --color=always -l | less -R
. Obviously a lot to type but you could alias it to something likell
. That is if you don't want to use any extra libraries.
– PhilT
Jul 23 '14 at 16:17
2
added @SergiyByelozyorov's comment into the answer.
– andrybak
Jan 18 '15 at 12:54
1
My edit was rejected so I guess I'll post it as a comment instead: Don't test the exit codes of commands indirectly. You can useif grep -q "#!/bin/bash" "$1"
(the-q
suppresses standard output). You may want to redirect standard error with2>/dev/null
.
– Tom Fenech
Oct 23 '15 at 13:16
|
show 7 more comments
Try the following:
less -R
from man less
:
-r
or--raw-control-chars
Causes "raw" control characters to be displayed. (...)
-R
or--RAW-CONTROL-CHARS
Like
-r
, but only ANSI "color" escape sequences are output in "raw" form. (...)
16
This is useful when the file itself contains the escape codes that will need to be displayed.
– Nitrodist
Dec 16 '11 at 21:16
1
I used to know about less -r but searching in the file using "/" kept bringing up the wrong lines. -R seems to do a better job. Thanks for the tip.
– Amos Shapira
Aug 6 '13 at 0:28
51
It should be noted that most programs use theisatty(2)
syscall to check whether their standard output is a terminal, and usually disable colorized output if it is not. For any pipe to less,isatty
will return 0. To check whether this works, tryecho -e 'x1b[32;1mtestx1b[m' | less -r
– mic_e
Sep 24 '13 at 22:53
10
This answer does not excel in the actually does something test.
– Michael Wolf
May 9 '14 at 22:24
17
You can also type-R
when you already openedless
to achieve this.
– Scz
Sep 1 '16 at 7:56
|
show 7 more comments
I got the answer in another post: Less and Grep: Getting colored results when using a pipe from grep to less
When you simply run
grep --color
it
impliesgrep --color=auto
which
detects whether the output is a
terminal and if so enables colors.
However, when it detects a pipe it
disables coloring. The following
command:
grep --color=always "search string" * | less -R
Will always enable coloring and
override the automatic detection, and
you will get the color highlighting in
less.
Warning: Don't put --color=always
as an alias, it break things sometimes. That's why there is an --color=auto
option.
4
Nice, thanks. Except that I need to use-R
as an option toless
, as well.
– naught101
May 8 '12 at 6:41
10
I believegrep -R
is for specifying recursive search.less -R
is necessary forless
to correctly spit the colors back out.grep --color=always [grep cmds] | less -R
works for me on OS X 10.7.3!
– Steven Lu
May 9 '12 at 13:56
@naught101 @Steven Lu Edited in, though it seems that some people may not need to useless -R
(according to the author of the original post, anyway).
– jtpereyda
Oct 22 '13 at 17:17
2
Is there anyway to let grep know just pipe less -R command and then just do coloring? So, we don't have to put --color=always and less -R all the time.
– A-letubby
Feb 27 '15 at 7:50
1
This is by far the simplest working answer. Thanks!
– Danny Staple
Oct 3 '15 at 11:16
|
show 1 more comment
Use view
instead of less
. It opens the file with vim
in readonly mode.
It's practically a coloured less
: a pager where you can search with / (and more). The only drawback is that you can't exit with q but you need :q
Also, you get the same colouring as vim
(since you're in fact using vim
).
How about the performance of big files? Vim syntax highlighting is know to be slow on huge files.
– pihentagy
Feb 20 '14 at 10:28
I don't know what's your value for 'big', but opening a ~10000 lines file is instantaneous, search inside included.
– Riccardo Galli
Feb 20 '14 at 11:50
1
I upvoted (I didn't know aboutview
) but another downside is that j/k/up/down don't instantly scroll, since there is a cursor.
– Tyler Collier
Mar 1 '15 at 17:38
6
Note that you may need to addview -
when piping
– user45909
Mar 2 '15 at 0:54
6
vim
is an editor, which loads the complete file into memory, whereasless
is a pager, loading the file only partially into memory. You will know the difference with huge files.
– sjas
Jul 20 '16 at 12:13
|
show 3 more comments
pygmentize
supports the -g
option to automatically guess the lexer to be used which is useful for files read from STDIN
without checking any extension type.
Using that, you only need to set the following 2 exports in your .bashrc
without any additional scripts:
export LESS='-R'
export LESSOPEN='|pygmentize -g %s'
4
Concise and effective. I prefer defining an alias, because sometimes less is better. So: alias lesc='LESS="-R" LESSOPEN="|pygmentize -g %s" less'
– Tiago
Apr 28 '14 at 18:27
add a comment |
To tell less to show colors call it with -R:
less -R
Unfortunately some programs detect that their stdout is not a terminal and disable colors - e.g pacman (Arch Linux package manager).
In those cases its possible to use unbuffer
:
unbuffer <command> | less -R
Example using pacman
unbuffer pacman -Ss firefox | less -R
The unbuffer
command is usually part of the expect-dev
(Debian/Ubuntu) or expect
(Arch Linux) package.
To answer the question for completeness:
As others already answered, pygmentize
is great for colorizing source code. It does not require unbuffer
. Easiest call:
pygmentize someSource.cpp | less -R
2
To useunbuffer
on Ubuntu,sudo apt install expect
– wisbucky
Dec 4 '18 at 18:37
add a comment |
You didn't say what this color should mean, e.g. what should the colors be for a text file?
If what you want is syntax highlighting for source code, you need a source code highlighter. I sometimes use pygmentize like this
pygmentize file.cpp | less
or
pygmentize file.cpp | more
There are other highlighters around.
This is pretty fast. If you don't mind firing up vim
there is a read-only mode that can give you syntax highlighting if you have it in vim
.
view file.cpp
or alternatively see churnd's answer.
add a comment |
This is yet another pygments-based answer, with several major improvements:
- does not break
lesspipe
orlessfile
filters - works with multiple inputs to
less
- correctly parses the script type from the shebang header
- works for all 434 file types lexable by Pygments
- color scheme is parameterized as an environment variable
Install Pygments and Gawk
sudo apt-get install python-pygments python3-pygments gawk
Set Environment Variables
Check whether lesspipe
or lessfile
is already enabled:
echo $LESSOPEN
If you don't see either program referenced there, ensure that lesspipe
is installed (most distros come with it).
Add the following to ~/.bashrc
:
# sets LESSOPEN and LESSCLOSE variables
eval "$(SHELL=/bin/sh lesspipe)"
# interpret color characters
export LESS='-R'
# to list available styles: `pygmentize -L styles`
export PYGMENTIZE_STYLE='paraiso-dark'
# optional
alias ls='ls --color=always'
alias grep='grep --color=always'
If you don't want lesspipe
, replace the eval
statement with:
export LESSOPEN='|~/.lessfilter %s'
Create ~/.lessfilter
Add the following code and make the file executable: chmod u+x ~/.lessfilter
#!/bin/bash
for path in "$@"; do
# match by known filenames
filename=$(basename "$path")
case "$filename" in
.bashrc|bash.bashrc|.bash_aliases|.bash_environment|.bash_profile|
.bash_login|.bash_logout|.profile|.zshrc|.zprofile|.zshrc|.zlogin|
.zlogout|zshrc|zprofile|zshrc|zlogin|zlogout|.cshrc|.cshdirs|
csh.cshrc|csh.login|csh.logout|.tcshrc|.kshrc|ksh.kshrc)
# shell lexer
pygmentize -f 256 -O style=$PYGMENTIZE_STYLE -l sh "$path"
;;
.htaccess|apache.conf|apache2.conf|Dockerfile|Kconfig|external.in*|
standard-modules.in|nginx.conf|pacman.conf|squid.conf|termcap|
termcap.src|terminfo|terminfo.src|control|sources.list|CMakeLists.txt|
Makefile|makefile|Makefile.*|GNUmakefile|SConstruct|SConscript|
.Rhistory|.Rprofile|.Renviron|Rakefile|Gemfile|PKGBUILD|autohandler|
dhandler|autodelegate|.vimrc|.exrc|.gvimrc|vimrc|exrc|gvimrc|todo.txt)
# filename recognized
pygmentize -f 256 -O style=$PYGMENTIZE_STYLE "$path"
;;
*)
ext=$([[ "$filename" = *.* ]] && echo ".${filename##*.}" || echo '')
case "$ext" in
.as|.mxml|.bc|.g|.gd|.gi|.gap|.nb|.cdf|.nbp|.ma|.mu|.at|.run|
.apl|.adl|.adls|.adlf|.adlx|.cadl|.odin|.c-objdump|.s|
.cpp-objdump|.c++-objdump|.cxx-objdump|.d-objdump|.S|.hsail|
.ll|.asm|.ASM|.objdump-intel|.objdump|.tasm|.au3|.ahk|.ahkl|
.bb|.decls|.bmx|.bas|.monkey|.BAS|.bst|.bib|.abap|.ABAP|.cbl|
.CBL|.cob|.COB|.cpy|.CPY|.gdc|.maql|.p|.cls|.c|.h|.idc|.cpp|
.hpp|.c++|.h++|.cc|.hh|.cxx|.hxx|.C|.H|.cp|.CPP|.ino|.clay|
.cu|.cuh|.ec|.eh|.mq4|.mq5|.mqh|.nc|.pike|.pmod|.swg|.i|.vala|
.vapi|.capnp|.chpl|.icl|.dcl|.cf|.docker|.ini|.cfg|.inf|
.pc|.properties|.reg|.tf|.pypylog|.cr|.csd|.orc|.sco|.css|
.less|.sass|.scss|.croc|.d|.di|.smali|.jsonld|.json|.yaml|
.yml|.dpatch|.darcspatch|.diff|.patch|.wdiff|.boo|.aspx|.asax|
.ascx|.ashx|.asmx|.axd|.cs|.fs|.fsi|.n|.vb|.als|.bro|.crmsh|
.pcmk|.msc|.pan|.proto|.pp|.rsl|.sbl|.thrift|.rpf|
.dylan-console|.dylan|.dyl|.intr|.lid|.hdp|.ecl|.e|.elm|.ex|
.exs|.erl|.hrl|.es|.escript|.erl-sh|.aheui|.befunge|.bf|.b|
.camkes|.idl4|.cdl|.cw|.factor|.fan|.flx|.flxh|.frt|.f|.F|
.f03|.f90|.F03|.F90|.PRG|.prg|.go|.abnf|.bnf|.jsgf|.cyp|
.cypher|.asy|.vert|.frag|.geo|.plot|.plt|.ps|.eps|.pov|.inc|
.agda|.cry|.hs|.idr|.kk|.kki|.lagda|.lcry|.lhs|.lidr|.hx|
.hxsl|.hxml|.sv|.svh|.v|.vhdl|.vhd|.dtd|.haml|.html|.htm|
.xhtml|.xslt|.pug|.jade|.scaml|.xml|.xsl|.rss|.xsd|.wsdl|
.wsf|.xpl|.pro|.ipf|.nsi|.nsh|.spec|.i6t|.ni|.i7x|.t|.io|
.ijs|.coffee|.dart|.eg|.js|.jsm|.juttle|.kal|.lasso|
.lasso[89]|.ls|.mask|.j|.ts|.tsx|.jl|.aj|.ceylon|.clj|
.cljs|.golo|.gs|.gsx|.gsp|.vark|.gst|.groovy|.gradle|.ik|
.java|.kt|.pig|.scala|.xtend|.cpsa|.cl|.lisp|.el|.hy|.lsp|.nl|
.kif|.rkt|.rktd|.rktl|.scm|.ss|.shen|.xtm|.cmake|.mak|.mk|
.[1234567]|.man|.md|.css.in|.js.in|.xul.in|.rst|.rest|.tex|
.aux|.toc|.m|.sci|.sce|.tst|.ml|.mli|.mll|.mly|.opa|.sml|.sig|
.fun|.bug|.jag|.mo|.stan|.def|.mod|.mt|.ncl|.nim|.nimrod|.nit|
.nix|.cps|.x|.xi|.xm|.xmi|.mm|.swift|.ooc|.psi|.psl|.G|.ebnf|
.rl|.treetop|.tt|.adb|.ads|.ada|.pas|.dpr|.pwn|.sp|.pl|.pm|
.nqp|.p6|.6pl|.p6l|.pl6|.6pm|.p6m|.pm6|.php|.php[345]|.zep|
.praat|.proc|.psc|.lgt|.logtalk|.prolog|.pyx|.pxd|.pxi|.dg|
.py3tb|.py|.pyw|.sc|.tac|.sage|.pytb|.qvto|.Rout|.Rd|.R|.rq|
.sparql|.ttl|.r|.r3|.reb|.red|.reds|.txt|.rnc|.graph|
.instances|.robot|.fy|.fancypack|.rb|.rbw|.rake|.gemspec|
.rbx|.duby|.rs|.rs.in|.SAS|.sas|.applescript|.chai|.ezt|
.mac|.hyb|.jcl|.lsl|.lua|.wlua|.moo|.moon|.rexx|.rex|.rx|
.arexx|.sh|.ksh|.bash|.ebuild|.eclass|.exheres-0|.exlib|.zsh|
.sh-session|.shell-session|.bat|.cmd|.fish|.load|.ps1|.psm1|
.tcsh|.csh|.ns2|.st|.smv|.snobol|.rql|.sql|.sqlite3-console|
.do|.ado|.scd|.tcl|.rvt|.ng2|.tmpl|.spt|.cfc|.cfm|.cfml|
.evoque|.kid|.handlebars|.hbs|.phtml|.jsp|.liquid|.mao|.mhtml|
.mc|.mi|.myt|.rhtml|.tpl|.ssp|.tea|.twig|.vm|.fhtml|.sls|
.feature|.tap|.awk|.vim|.pot|.po|.weechatlog|.todotxt|.thy|
.lean|.rts|.u|.vcl|.bpl|.sil|.vpr|.cirru|.duel|.jbst|.qml|
.qbs|.slim|.xqy|.xquery|.xq|.xql|.xqm|.whiley|.x10)
# extension recognized
pygmentize -f 256 -O style=$PYGMENTIZE_STYLE "$path"
;;
*)
# parse the shebang script header if it exists
lexer=$(head -n 1 "$path" |grep "^#!" |awk -F" "
'match($1, //(w*)$/, a) {if (a[1]!="env") {print a[1]} else {print $2}}')
case "$lexer" in
node|nodejs)
# workaround for lack of Node.js lexer alias
pygmentize -f 256 -O style=$PYGMENTIZE_STYLE
-l js "$path"
;;
"")
exit 1
;;
*)
pygmentize -f 256 -O style=$PYGMENTIZE_STYLE
-l $lexer "$path"
;;
esac
;;
esac
;;
esac
done
exit 0
The one drawback about this approach is that Pygments is a Python program, and so on first use during a shell session, there is a "cold start" delay. Subsequent invocations are much faster.
– Joe Coder
Oct 12 '18 at 1:52
Nice! Really comprehensive.
– Dario Seidl
Nov 11 '18 at 16:01
add a comment |
Use the GNU Source-highlight; you can install it with apt
if you have it, or otherwise install it from source. Then set up an "input preprocessor" for less, with help from the Source-highligh' documentations for setting up with less:
This was suggested by Konstantine Serebriany. The script src-hilite-lesspipe.sh will be installed together with source-highlight. You can use the following environment variables:
export LESSOPEN="| /path/to/src-hilite-lesspipe.sh %s"
export LESS=' -R '
This way, when you use less to browse a file, if it is a source file handled by source-highlight, it will be automatically highlighted.
Xavier-Emmanuel Vincent recently provided an alternative version of ANSI color scheme, esc256.style: some terminals can handle 256 colors. Xavier also provided a script which checks how many colors your terminal can handle, and in case, uses the 256 variant. The script is called source-highlight-esc.sh and it will be installed together with the other binaries.
add a comment |
To expand upon another answer, you can make it work for most if not all of your scripts that don't have extensions by changing the .lessfilter file around just a bit:
#!/bin/sh
case "$1" in
*.awk|*.groff|*.java|*.js|*.m4|*.php|*.pl|*.pm|*.pod|*.sh|
*.ad[asb]|*.asm|*.inc|*.[ch]|*.[ch]pp|*.[ch]xx|*.cc|*.hh|
*.lsp|*.l|*.pas|*.p|*.xml|*.xps|*.xsl|*.axp|*.ppd|*.pov|
*.diff|*.patch|*.py|*.rb|*.sql|*.ebuild|*.eclass)
pygmentize -f 256 "$1";;
.bashrc|.bash_aliases|.bash_environment)
pygmentize -f 256 -l sh "$1"
;;
*)
scriptExec=$(head -1 "$1" |grep "^#!" |awk -F" " '{print $1}')
scriptExecStatus=$?
if [ "$scriptExecStatus" -eq "0" ]; then
lexer=$(echo $scriptExec |awk -F/ '{print $NF}')
pygmentize -f 256 -l $lexer "$1"
else
exit 1
fi
esac
exit 0
You'd still need to add the two variables to .bashrc:
export LESS='-R'
export LESSOPEN='|~/.lessfilter %s'
And you'll still need to make .lessfilter executable:
$ chmod 700 ~/.lessfilter
Also I wanted to add that under debian the pygments package is called python-pygments. I had trouble locating it at first because the obvious misspelling of "pigments" as "pygments" wasn't enough of a hint to me that it was a package that might be prefixed with "python-" by the package manager.
2
2 comments: 1) Thanks for the improvement. 2) Phrases like "voted best answer" aren't great; that may change (in fact, if this is better than that answer, this post might become the top answer, at which point it'll just be confusing. Maybe just say "to expand upon another answer" or "captaincomic's answer"?
– cpast
Feb 27 '13 at 23:07
add a comment |
You can consider using most
utility which is colour-friendly alternative for less
and more
.
add a comment |
Condensed from my full blog post about improving less experience: https://www.topbug.net/blog/2016/09/27/make-gnu-less-more-powerful/
For colorful manpages, add the following to your .bashrc
or .zshrc
:
export LESS_TERMCAP_mb=$'E[1;31m' # begin bold
export LESS_TERMCAP_md=$'E[1;36m' # begin blink
export LESS_TERMCAP_me=$'E[0m' # reset bold/blink
export LESS_TERMCAP_so=$'E[01;44;33m' # begin reverse video
export LESS_TERMCAP_se=$'E[0m' # reset reverse video
export LESS_TERMCAP_us=$'E[1;32m' # begin underline
export LESS_TERMCAP_ue=$'E[0m' # reset underline
For syntax highlighting, using an existing powerful lesspipe.sh
to handle it instead of writing your own: https://github.com/wofr06/lesspipe
add a comment |
I found this simple elegant solution. You don't have to install anything extra as it is already there by default on most machines. As vim
is installed by default on most machines, it includes a macro to run vim
like less
Some of the options to use it are to create an alias:
alias vless='vim -u /usr/share/vim/vim74/macros/less.vim'
or create a symbolic link:
ln -s /usr/share/vim/vim74/macros/less.sh ~/bin/vless
Then you just run vless myfile.py
I got most of the information here
add a comment |
The most intuitive and straight forward solution for me was using pygmentize
by adding the lines below to .bashrc
export LESS='-R'
export LESSOPEN='|pygmentize -g %s'
In case you couldn't call pygmentize
, just install like
pip install pygments
ps. The pygmentize
executable binary would sit in /usr/local/bin/
or in your /home/username/.virtualenv/venvname/bin/
or somewhere.
add a comment |
None of these were working out of the box for me and I figured out an easy way to make this work so I thought I would share.
Just use tmux, that allows you access and scroll through a larger history and preserves the colors perfectly.
Doesn't tmux permit multiple terminals from one screen, how does it change less's display of colours?
– Xen2050
Oct 26 '18 at 13:23
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "3"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f117841%2fget-colors-in-less-or-more%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
15 Answers
15
active
oldest
votes
15 Answers
15
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can utilize the power of pygmentize with less - automatically! (No need to pipe by hand.)
Install pygments
with your package manager or pip (possibly called python-pygments
) or get it here http://pygments.org/download/.
Write a file ~/.lessfilter
#!/bin/sh
case "$1" in
*.awk|*.groff|*.java|*.js|*.m4|*.php|*.pl|*.pm|*.pod|*.sh|
*.ad[asb]|*.asm|*.inc|*.[ch]|*.[ch]pp|*.[ch]xx|*.cc|*.hh|
*.lsp|*.l|*.pas|*.p|*.xml|*.xps|*.xsl|*.axp|*.ppd|*.pov|
*.diff|*.patch|*.py|*.rb|*.sql|*.ebuild|*.eclass)
pygmentize -f 256 "$1";;
.bashrc|.bash_aliases|.bash_environment)
pygmentize -f 256 -l sh "$1";;
*)
if grep -q "#!/bin/bash" "$1" 2> /dev/null; then
pygmentize -f 256 -l sh "$1"
else
exit 1
fi
esac
exit 0
In your .bashrc
add
export LESS='-R'
export LESSOPEN='|~/.lessfilter %s'
Also, you need to make ~/.lessfilter
executable by running
chmod u+x ~/.lessfilter
Tested on Debian.
You get the idea. This can of course be improved further, accepting more extensions or parsing the shebang for other interpreters than bash. See some of the other answers for that.
The idea came from an old blog post from the makers of Pygments, but the original post doesn't exist anymore.
6
If you want to have coloring of the source code files, you also need to make ~/.lessfilter executable by runningchmod u+x ~/.lessfilter
. You also need to have pygmentize (pygments.org/download) installed.
– Sergiy Belozorov
Dec 18 '12 at 11:07
Can anyone confirm that this works as it has no effect for me when I execute a command likels -l | less
– puk
Oct 30 '13 at 13:59
7
@puk you can do something likels --color=always -l | less -R
. Obviously a lot to type but you could alias it to something likell
. That is if you don't want to use any extra libraries.
– PhilT
Jul 23 '14 at 16:17
2
added @SergiyByelozyorov's comment into the answer.
– andrybak
Jan 18 '15 at 12:54
1
My edit was rejected so I guess I'll post it as a comment instead: Don't test the exit codes of commands indirectly. You can useif grep -q "#!/bin/bash" "$1"
(the-q
suppresses standard output). You may want to redirect standard error with2>/dev/null
.
– Tom Fenech
Oct 23 '15 at 13:16
|
show 7 more comments
You can utilize the power of pygmentize with less - automatically! (No need to pipe by hand.)
Install pygments
with your package manager or pip (possibly called python-pygments
) or get it here http://pygments.org/download/.
Write a file ~/.lessfilter
#!/bin/sh
case "$1" in
*.awk|*.groff|*.java|*.js|*.m4|*.php|*.pl|*.pm|*.pod|*.sh|
*.ad[asb]|*.asm|*.inc|*.[ch]|*.[ch]pp|*.[ch]xx|*.cc|*.hh|
*.lsp|*.l|*.pas|*.p|*.xml|*.xps|*.xsl|*.axp|*.ppd|*.pov|
*.diff|*.patch|*.py|*.rb|*.sql|*.ebuild|*.eclass)
pygmentize -f 256 "$1";;
.bashrc|.bash_aliases|.bash_environment)
pygmentize -f 256 -l sh "$1";;
*)
if grep -q "#!/bin/bash" "$1" 2> /dev/null; then
pygmentize -f 256 -l sh "$1"
else
exit 1
fi
esac
exit 0
In your .bashrc
add
export LESS='-R'
export LESSOPEN='|~/.lessfilter %s'
Also, you need to make ~/.lessfilter
executable by running
chmod u+x ~/.lessfilter
Tested on Debian.
You get the idea. This can of course be improved further, accepting more extensions or parsing the shebang for other interpreters than bash. See some of the other answers for that.
The idea came from an old blog post from the makers of Pygments, but the original post doesn't exist anymore.
6
If you want to have coloring of the source code files, you also need to make ~/.lessfilter executable by runningchmod u+x ~/.lessfilter
. You also need to have pygmentize (pygments.org/download) installed.
– Sergiy Belozorov
Dec 18 '12 at 11:07
Can anyone confirm that this works as it has no effect for me when I execute a command likels -l | less
– puk
Oct 30 '13 at 13:59
7
@puk you can do something likels --color=always -l | less -R
. Obviously a lot to type but you could alias it to something likell
. That is if you don't want to use any extra libraries.
– PhilT
Jul 23 '14 at 16:17
2
added @SergiyByelozyorov's comment into the answer.
– andrybak
Jan 18 '15 at 12:54
1
My edit was rejected so I guess I'll post it as a comment instead: Don't test the exit codes of commands indirectly. You can useif grep -q "#!/bin/bash" "$1"
(the-q
suppresses standard output). You may want to redirect standard error with2>/dev/null
.
– Tom Fenech
Oct 23 '15 at 13:16
|
show 7 more comments
You can utilize the power of pygmentize with less - automatically! (No need to pipe by hand.)
Install pygments
with your package manager or pip (possibly called python-pygments
) or get it here http://pygments.org/download/.
Write a file ~/.lessfilter
#!/bin/sh
case "$1" in
*.awk|*.groff|*.java|*.js|*.m4|*.php|*.pl|*.pm|*.pod|*.sh|
*.ad[asb]|*.asm|*.inc|*.[ch]|*.[ch]pp|*.[ch]xx|*.cc|*.hh|
*.lsp|*.l|*.pas|*.p|*.xml|*.xps|*.xsl|*.axp|*.ppd|*.pov|
*.diff|*.patch|*.py|*.rb|*.sql|*.ebuild|*.eclass)
pygmentize -f 256 "$1";;
.bashrc|.bash_aliases|.bash_environment)
pygmentize -f 256 -l sh "$1";;
*)
if grep -q "#!/bin/bash" "$1" 2> /dev/null; then
pygmentize -f 256 -l sh "$1"
else
exit 1
fi
esac
exit 0
In your .bashrc
add
export LESS='-R'
export LESSOPEN='|~/.lessfilter %s'
Also, you need to make ~/.lessfilter
executable by running
chmod u+x ~/.lessfilter
Tested on Debian.
You get the idea. This can of course be improved further, accepting more extensions or parsing the shebang for other interpreters than bash. See some of the other answers for that.
The idea came from an old blog post from the makers of Pygments, but the original post doesn't exist anymore.
You can utilize the power of pygmentize with less - automatically! (No need to pipe by hand.)
Install pygments
with your package manager or pip (possibly called python-pygments
) or get it here http://pygments.org/download/.
Write a file ~/.lessfilter
#!/bin/sh
case "$1" in
*.awk|*.groff|*.java|*.js|*.m4|*.php|*.pl|*.pm|*.pod|*.sh|
*.ad[asb]|*.asm|*.inc|*.[ch]|*.[ch]pp|*.[ch]xx|*.cc|*.hh|
*.lsp|*.l|*.pas|*.p|*.xml|*.xps|*.xsl|*.axp|*.ppd|*.pov|
*.diff|*.patch|*.py|*.rb|*.sql|*.ebuild|*.eclass)
pygmentize -f 256 "$1";;
.bashrc|.bash_aliases|.bash_environment)
pygmentize -f 256 -l sh "$1";;
*)
if grep -q "#!/bin/bash" "$1" 2> /dev/null; then
pygmentize -f 256 -l sh "$1"
else
exit 1
fi
esac
exit 0
In your .bashrc
add
export LESS='-R'
export LESSOPEN='|~/.lessfilter %s'
Also, you need to make ~/.lessfilter
executable by running
chmod u+x ~/.lessfilter
Tested on Debian.
You get the idea. This can of course be improved further, accepting more extensions or parsing the shebang for other interpreters than bash. See some of the other answers for that.
The idea came from an old blog post from the makers of Pygments, but the original post doesn't exist anymore.
edited Nov 11 '18 at 15:42
answered Sep 20 '11 at 3:29
Dario SeidlDario Seidl
2,16611318
2,16611318
6
If you want to have coloring of the source code files, you also need to make ~/.lessfilter executable by runningchmod u+x ~/.lessfilter
. You also need to have pygmentize (pygments.org/download) installed.
– Sergiy Belozorov
Dec 18 '12 at 11:07
Can anyone confirm that this works as it has no effect for me when I execute a command likels -l | less
– puk
Oct 30 '13 at 13:59
7
@puk you can do something likels --color=always -l | less -R
. Obviously a lot to type but you could alias it to something likell
. That is if you don't want to use any extra libraries.
– PhilT
Jul 23 '14 at 16:17
2
added @SergiyByelozyorov's comment into the answer.
– andrybak
Jan 18 '15 at 12:54
1
My edit was rejected so I guess I'll post it as a comment instead: Don't test the exit codes of commands indirectly. You can useif grep -q "#!/bin/bash" "$1"
(the-q
suppresses standard output). You may want to redirect standard error with2>/dev/null
.
– Tom Fenech
Oct 23 '15 at 13:16
|
show 7 more comments
6
If you want to have coloring of the source code files, you also need to make ~/.lessfilter executable by runningchmod u+x ~/.lessfilter
. You also need to have pygmentize (pygments.org/download) installed.
– Sergiy Belozorov
Dec 18 '12 at 11:07
Can anyone confirm that this works as it has no effect for me when I execute a command likels -l | less
– puk
Oct 30 '13 at 13:59
7
@puk you can do something likels --color=always -l | less -R
. Obviously a lot to type but you could alias it to something likell
. That is if you don't want to use any extra libraries.
– PhilT
Jul 23 '14 at 16:17
2
added @SergiyByelozyorov's comment into the answer.
– andrybak
Jan 18 '15 at 12:54
1
My edit was rejected so I guess I'll post it as a comment instead: Don't test the exit codes of commands indirectly. You can useif grep -q "#!/bin/bash" "$1"
(the-q
suppresses standard output). You may want to redirect standard error with2>/dev/null
.
– Tom Fenech
Oct 23 '15 at 13:16
6
6
If you want to have coloring of the source code files, you also need to make ~/.lessfilter executable by running
chmod u+x ~/.lessfilter
. You also need to have pygmentize (pygments.org/download) installed.– Sergiy Belozorov
Dec 18 '12 at 11:07
If you want to have coloring of the source code files, you also need to make ~/.lessfilter executable by running
chmod u+x ~/.lessfilter
. You also need to have pygmentize (pygments.org/download) installed.– Sergiy Belozorov
Dec 18 '12 at 11:07
Can anyone confirm that this works as it has no effect for me when I execute a command like
ls -l | less
– puk
Oct 30 '13 at 13:59
Can anyone confirm that this works as it has no effect for me when I execute a command like
ls -l | less
– puk
Oct 30 '13 at 13:59
7
7
@puk you can do something like
ls --color=always -l | less -R
. Obviously a lot to type but you could alias it to something like ll
. That is if you don't want to use any extra libraries.– PhilT
Jul 23 '14 at 16:17
@puk you can do something like
ls --color=always -l | less -R
. Obviously a lot to type but you could alias it to something like ll
. That is if you don't want to use any extra libraries.– PhilT
Jul 23 '14 at 16:17
2
2
added @SergiyByelozyorov's comment into the answer.
– andrybak
Jan 18 '15 at 12:54
added @SergiyByelozyorov's comment into the answer.
– andrybak
Jan 18 '15 at 12:54
1
1
My edit was rejected so I guess I'll post it as a comment instead: Don't test the exit codes of commands indirectly. You can use
if grep -q "#!/bin/bash" "$1"
(the -q
suppresses standard output). You may want to redirect standard error with 2>/dev/null
.– Tom Fenech
Oct 23 '15 at 13:16
My edit was rejected so I guess I'll post it as a comment instead: Don't test the exit codes of commands indirectly. You can use
if grep -q "#!/bin/bash" "$1"
(the -q
suppresses standard output). You may want to redirect standard error with 2>/dev/null
.– Tom Fenech
Oct 23 '15 at 13:16
|
show 7 more comments
Try the following:
less -R
from man less
:
-r
or--raw-control-chars
Causes "raw" control characters to be displayed. (...)
-R
or--RAW-CONTROL-CHARS
Like
-r
, but only ANSI "color" escape sequences are output in "raw" form. (...)
16
This is useful when the file itself contains the escape codes that will need to be displayed.
– Nitrodist
Dec 16 '11 at 21:16
1
I used to know about less -r but searching in the file using "/" kept bringing up the wrong lines. -R seems to do a better job. Thanks for the tip.
– Amos Shapira
Aug 6 '13 at 0:28
51
It should be noted that most programs use theisatty(2)
syscall to check whether their standard output is a terminal, and usually disable colorized output if it is not. For any pipe to less,isatty
will return 0. To check whether this works, tryecho -e 'x1b[32;1mtestx1b[m' | less -r
– mic_e
Sep 24 '13 at 22:53
10
This answer does not excel in the actually does something test.
– Michael Wolf
May 9 '14 at 22:24
17
You can also type-R
when you already openedless
to achieve this.
– Scz
Sep 1 '16 at 7:56
|
show 7 more comments
Try the following:
less -R
from man less
:
-r
or--raw-control-chars
Causes "raw" control characters to be displayed. (...)
-R
or--RAW-CONTROL-CHARS
Like
-r
, but only ANSI "color" escape sequences are output in "raw" form. (...)
16
This is useful when the file itself contains the escape codes that will need to be displayed.
– Nitrodist
Dec 16 '11 at 21:16
1
I used to know about less -r but searching in the file using "/" kept bringing up the wrong lines. -R seems to do a better job. Thanks for the tip.
– Amos Shapira
Aug 6 '13 at 0:28
51
It should be noted that most programs use theisatty(2)
syscall to check whether their standard output is a terminal, and usually disable colorized output if it is not. For any pipe to less,isatty
will return 0. To check whether this works, tryecho -e 'x1b[32;1mtestx1b[m' | less -r
– mic_e
Sep 24 '13 at 22:53
10
This answer does not excel in the actually does something test.
– Michael Wolf
May 9 '14 at 22:24
17
You can also type-R
when you already openedless
to achieve this.
– Scz
Sep 1 '16 at 7:56
|
show 7 more comments
Try the following:
less -R
from man less
:
-r
or--raw-control-chars
Causes "raw" control characters to be displayed. (...)
-R
or--RAW-CONTROL-CHARS
Like
-r
, but only ANSI "color" escape sequences are output in "raw" form. (...)
Try the following:
less -R
from man less
:
-r
or--raw-control-chars
Causes "raw" control characters to be displayed. (...)
-R
or--RAW-CONTROL-CHARS
Like
-r
, but only ANSI "color" escape sequences are output in "raw" form. (...)
edited Jul 26 '17 at 8:38
Kamil Maciorowski
28k156185
28k156185
answered Mar 9 '10 at 10:48
ChristopheDChristopheD
4,93421110
4,93421110
16
This is useful when the file itself contains the escape codes that will need to be displayed.
– Nitrodist
Dec 16 '11 at 21:16
1
I used to know about less -r but searching in the file using "/" kept bringing up the wrong lines. -R seems to do a better job. Thanks for the tip.
– Amos Shapira
Aug 6 '13 at 0:28
51
It should be noted that most programs use theisatty(2)
syscall to check whether their standard output is a terminal, and usually disable colorized output if it is not. For any pipe to less,isatty
will return 0. To check whether this works, tryecho -e 'x1b[32;1mtestx1b[m' | less -r
– mic_e
Sep 24 '13 at 22:53
10
This answer does not excel in the actually does something test.
– Michael Wolf
May 9 '14 at 22:24
17
You can also type-R
when you already openedless
to achieve this.
– Scz
Sep 1 '16 at 7:56
|
show 7 more comments
16
This is useful when the file itself contains the escape codes that will need to be displayed.
– Nitrodist
Dec 16 '11 at 21:16
1
I used to know about less -r but searching in the file using "/" kept bringing up the wrong lines. -R seems to do a better job. Thanks for the tip.
– Amos Shapira
Aug 6 '13 at 0:28
51
It should be noted that most programs use theisatty(2)
syscall to check whether their standard output is a terminal, and usually disable colorized output if it is not. For any pipe to less,isatty
will return 0. To check whether this works, tryecho -e 'x1b[32;1mtestx1b[m' | less -r
– mic_e
Sep 24 '13 at 22:53
10
This answer does not excel in the actually does something test.
– Michael Wolf
May 9 '14 at 22:24
17
You can also type-R
when you already openedless
to achieve this.
– Scz
Sep 1 '16 at 7:56
16
16
This is useful when the file itself contains the escape codes that will need to be displayed.
– Nitrodist
Dec 16 '11 at 21:16
This is useful when the file itself contains the escape codes that will need to be displayed.
– Nitrodist
Dec 16 '11 at 21:16
1
1
I used to know about less -r but searching in the file using "/" kept bringing up the wrong lines. -R seems to do a better job. Thanks for the tip.
– Amos Shapira
Aug 6 '13 at 0:28
I used to know about less -r but searching in the file using "/" kept bringing up the wrong lines. -R seems to do a better job. Thanks for the tip.
– Amos Shapira
Aug 6 '13 at 0:28
51
51
It should be noted that most programs use the
isatty(2)
syscall to check whether their standard output is a terminal, and usually disable colorized output if it is not. For any pipe to less, isatty
will return 0. To check whether this works, try echo -e 'x1b[32;1mtestx1b[m' | less -r
– mic_e
Sep 24 '13 at 22:53
It should be noted that most programs use the
isatty(2)
syscall to check whether their standard output is a terminal, and usually disable colorized output if it is not. For any pipe to less, isatty
will return 0. To check whether this works, try echo -e 'x1b[32;1mtestx1b[m' | less -r
– mic_e
Sep 24 '13 at 22:53
10
10
This answer does not excel in the actually does something test.
– Michael Wolf
May 9 '14 at 22:24
This answer does not excel in the actually does something test.
– Michael Wolf
May 9 '14 at 22:24
17
17
You can also type
-R
when you already opened less
to achieve this.– Scz
Sep 1 '16 at 7:56
You can also type
-R
when you already opened less
to achieve this.– Scz
Sep 1 '16 at 7:56
|
show 7 more comments
I got the answer in another post: Less and Grep: Getting colored results when using a pipe from grep to less
When you simply run
grep --color
it
impliesgrep --color=auto
which
detects whether the output is a
terminal and if so enables colors.
However, when it detects a pipe it
disables coloring. The following
command:
grep --color=always "search string" * | less -R
Will always enable coloring and
override the automatic detection, and
you will get the color highlighting in
less.
Warning: Don't put --color=always
as an alias, it break things sometimes. That's why there is an --color=auto
option.
4
Nice, thanks. Except that I need to use-R
as an option toless
, as well.
– naught101
May 8 '12 at 6:41
10
I believegrep -R
is for specifying recursive search.less -R
is necessary forless
to correctly spit the colors back out.grep --color=always [grep cmds] | less -R
works for me on OS X 10.7.3!
– Steven Lu
May 9 '12 at 13:56
@naught101 @Steven Lu Edited in, though it seems that some people may not need to useless -R
(according to the author of the original post, anyway).
– jtpereyda
Oct 22 '13 at 17:17
2
Is there anyway to let grep know just pipe less -R command and then just do coloring? So, we don't have to put --color=always and less -R all the time.
– A-letubby
Feb 27 '15 at 7:50
1
This is by far the simplest working answer. Thanks!
– Danny Staple
Oct 3 '15 at 11:16
|
show 1 more comment
I got the answer in another post: Less and Grep: Getting colored results when using a pipe from grep to less
When you simply run
grep --color
it
impliesgrep --color=auto
which
detects whether the output is a
terminal and if so enables colors.
However, when it detects a pipe it
disables coloring. The following
command:
grep --color=always "search string" * | less -R
Will always enable coloring and
override the automatic detection, and
you will get the color highlighting in
less.
Warning: Don't put --color=always
as an alias, it break things sometimes. That's why there is an --color=auto
option.
4
Nice, thanks. Except that I need to use-R
as an option toless
, as well.
– naught101
May 8 '12 at 6:41
10
I believegrep -R
is for specifying recursive search.less -R
is necessary forless
to correctly spit the colors back out.grep --color=always [grep cmds] | less -R
works for me on OS X 10.7.3!
– Steven Lu
May 9 '12 at 13:56
@naught101 @Steven Lu Edited in, though it seems that some people may not need to useless -R
(according to the author of the original post, anyway).
– jtpereyda
Oct 22 '13 at 17:17
2
Is there anyway to let grep know just pipe less -R command and then just do coloring? So, we don't have to put --color=always and less -R all the time.
– A-letubby
Feb 27 '15 at 7:50
1
This is by far the simplest working answer. Thanks!
– Danny Staple
Oct 3 '15 at 11:16
|
show 1 more comment
I got the answer in another post: Less and Grep: Getting colored results when using a pipe from grep to less
When you simply run
grep --color
it
impliesgrep --color=auto
which
detects whether the output is a
terminal and if so enables colors.
However, when it detects a pipe it
disables coloring. The following
command:
grep --color=always "search string" * | less -R
Will always enable coloring and
override the automatic detection, and
you will get the color highlighting in
less.
Warning: Don't put --color=always
as an alias, it break things sometimes. That's why there is an --color=auto
option.
I got the answer in another post: Less and Grep: Getting colored results when using a pipe from grep to less
When you simply run
grep --color
it
impliesgrep --color=auto
which
detects whether the output is a
terminal and if so enables colors.
However, when it detects a pipe it
disables coloring. The following
command:
grep --color=always "search string" * | less -R
Will always enable coloring and
override the automatic detection, and
you will get the color highlighting in
less.
Warning: Don't put --color=always
as an alias, it break things sometimes. That's why there is an --color=auto
option.
edited Mar 20 '17 at 10:17
Community♦
1
1
answered Apr 27 '11 at 4:19
PuneetPuneet
1,251182
1,251182
4
Nice, thanks. Except that I need to use-R
as an option toless
, as well.
– naught101
May 8 '12 at 6:41
10
I believegrep -R
is for specifying recursive search.less -R
is necessary forless
to correctly spit the colors back out.grep --color=always [grep cmds] | less -R
works for me on OS X 10.7.3!
– Steven Lu
May 9 '12 at 13:56
@naught101 @Steven Lu Edited in, though it seems that some people may not need to useless -R
(according to the author of the original post, anyway).
– jtpereyda
Oct 22 '13 at 17:17
2
Is there anyway to let grep know just pipe less -R command and then just do coloring? So, we don't have to put --color=always and less -R all the time.
– A-letubby
Feb 27 '15 at 7:50
1
This is by far the simplest working answer. Thanks!
– Danny Staple
Oct 3 '15 at 11:16
|
show 1 more comment
4
Nice, thanks. Except that I need to use-R
as an option toless
, as well.
– naught101
May 8 '12 at 6:41
10
I believegrep -R
is for specifying recursive search.less -R
is necessary forless
to correctly spit the colors back out.grep --color=always [grep cmds] | less -R
works for me on OS X 10.7.3!
– Steven Lu
May 9 '12 at 13:56
@naught101 @Steven Lu Edited in, though it seems that some people may not need to useless -R
(according to the author of the original post, anyway).
– jtpereyda
Oct 22 '13 at 17:17
2
Is there anyway to let grep know just pipe less -R command and then just do coloring? So, we don't have to put --color=always and less -R all the time.
– A-letubby
Feb 27 '15 at 7:50
1
This is by far the simplest working answer. Thanks!
– Danny Staple
Oct 3 '15 at 11:16
4
4
Nice, thanks. Except that I need to use
-R
as an option to less
, as well.– naught101
May 8 '12 at 6:41
Nice, thanks. Except that I need to use
-R
as an option to less
, as well.– naught101
May 8 '12 at 6:41
10
10
I believe
grep -R
is for specifying recursive search. less -R
is necessary for less
to correctly spit the colors back out. grep --color=always [grep cmds] | less -R
works for me on OS X 10.7.3!– Steven Lu
May 9 '12 at 13:56
I believe
grep -R
is for specifying recursive search. less -R
is necessary for less
to correctly spit the colors back out. grep --color=always [grep cmds] | less -R
works for me on OS X 10.7.3!– Steven Lu
May 9 '12 at 13:56
@naught101 @Steven Lu Edited in, though it seems that some people may not need to use
less -R
(according to the author of the original post, anyway).– jtpereyda
Oct 22 '13 at 17:17
@naught101 @Steven Lu Edited in, though it seems that some people may not need to use
less -R
(according to the author of the original post, anyway).– jtpereyda
Oct 22 '13 at 17:17
2
2
Is there anyway to let grep know just pipe less -R command and then just do coloring? So, we don't have to put --color=always and less -R all the time.
– A-letubby
Feb 27 '15 at 7:50
Is there anyway to let grep know just pipe less -R command and then just do coloring? So, we don't have to put --color=always and less -R all the time.
– A-letubby
Feb 27 '15 at 7:50
1
1
This is by far the simplest working answer. Thanks!
– Danny Staple
Oct 3 '15 at 11:16
This is by far the simplest working answer. Thanks!
– Danny Staple
Oct 3 '15 at 11:16
|
show 1 more comment
Use view
instead of less
. It opens the file with vim
in readonly mode.
It's practically a coloured less
: a pager where you can search with / (and more). The only drawback is that you can't exit with q but you need :q
Also, you get the same colouring as vim
(since you're in fact using vim
).
How about the performance of big files? Vim syntax highlighting is know to be slow on huge files.
– pihentagy
Feb 20 '14 at 10:28
I don't know what's your value for 'big', but opening a ~10000 lines file is instantaneous, search inside included.
– Riccardo Galli
Feb 20 '14 at 11:50
1
I upvoted (I didn't know aboutview
) but another downside is that j/k/up/down don't instantly scroll, since there is a cursor.
– Tyler Collier
Mar 1 '15 at 17:38
6
Note that you may need to addview -
when piping
– user45909
Mar 2 '15 at 0:54
6
vim
is an editor, which loads the complete file into memory, whereasless
is a pager, loading the file only partially into memory. You will know the difference with huge files.
– sjas
Jul 20 '16 at 12:13
|
show 3 more comments
Use view
instead of less
. It opens the file with vim
in readonly mode.
It's practically a coloured less
: a pager where you can search with / (and more). The only drawback is that you can't exit with q but you need :q
Also, you get the same colouring as vim
(since you're in fact using vim
).
How about the performance of big files? Vim syntax highlighting is know to be slow on huge files.
– pihentagy
Feb 20 '14 at 10:28
I don't know what's your value for 'big', but opening a ~10000 lines file is instantaneous, search inside included.
– Riccardo Galli
Feb 20 '14 at 11:50
1
I upvoted (I didn't know aboutview
) but another downside is that j/k/up/down don't instantly scroll, since there is a cursor.
– Tyler Collier
Mar 1 '15 at 17:38
6
Note that you may need to addview -
when piping
– user45909
Mar 2 '15 at 0:54
6
vim
is an editor, which loads the complete file into memory, whereasless
is a pager, loading the file only partially into memory. You will know the difference with huge files.
– sjas
Jul 20 '16 at 12:13
|
show 3 more comments
Use view
instead of less
. It opens the file with vim
in readonly mode.
It's practically a coloured less
: a pager where you can search with / (and more). The only drawback is that you can't exit with q but you need :q
Also, you get the same colouring as vim
(since you're in fact using vim
).
Use view
instead of less
. It opens the file with vim
in readonly mode.
It's practically a coloured less
: a pager where you can search with / (and more). The only drawback is that you can't exit with q but you need :q
Also, you get the same colouring as vim
(since you're in fact using vim
).
edited Sep 24 '13 at 10:24
Jawa
3,15982435
3,15982435
answered Sep 24 '13 at 9:50
Riccardo GalliRiccardo Galli
43556
43556
How about the performance of big files? Vim syntax highlighting is know to be slow on huge files.
– pihentagy
Feb 20 '14 at 10:28
I don't know what's your value for 'big', but opening a ~10000 lines file is instantaneous, search inside included.
– Riccardo Galli
Feb 20 '14 at 11:50
1
I upvoted (I didn't know aboutview
) but another downside is that j/k/up/down don't instantly scroll, since there is a cursor.
– Tyler Collier
Mar 1 '15 at 17:38
6
Note that you may need to addview -
when piping
– user45909
Mar 2 '15 at 0:54
6
vim
is an editor, which loads the complete file into memory, whereasless
is a pager, loading the file only partially into memory. You will know the difference with huge files.
– sjas
Jul 20 '16 at 12:13
|
show 3 more comments
How about the performance of big files? Vim syntax highlighting is know to be slow on huge files.
– pihentagy
Feb 20 '14 at 10:28
I don't know what's your value for 'big', but opening a ~10000 lines file is instantaneous, search inside included.
– Riccardo Galli
Feb 20 '14 at 11:50
1
I upvoted (I didn't know aboutview
) but another downside is that j/k/up/down don't instantly scroll, since there is a cursor.
– Tyler Collier
Mar 1 '15 at 17:38
6
Note that you may need to addview -
when piping
– user45909
Mar 2 '15 at 0:54
6
vim
is an editor, which loads the complete file into memory, whereasless
is a pager, loading the file only partially into memory. You will know the difference with huge files.
– sjas
Jul 20 '16 at 12:13
How about the performance of big files? Vim syntax highlighting is know to be slow on huge files.
– pihentagy
Feb 20 '14 at 10:28
How about the performance of big files? Vim syntax highlighting is know to be slow on huge files.
– pihentagy
Feb 20 '14 at 10:28
I don't know what's your value for 'big', but opening a ~10000 lines file is instantaneous, search inside included.
– Riccardo Galli
Feb 20 '14 at 11:50
I don't know what's your value for 'big', but opening a ~10000 lines file is instantaneous, search inside included.
– Riccardo Galli
Feb 20 '14 at 11:50
1
1
I upvoted (I didn't know about
view
) but another downside is that j/k/up/down don't instantly scroll, since there is a cursor.– Tyler Collier
Mar 1 '15 at 17:38
I upvoted (I didn't know about
view
) but another downside is that j/k/up/down don't instantly scroll, since there is a cursor.– Tyler Collier
Mar 1 '15 at 17:38
6
6
Note that you may need to add
view -
when piping– user45909
Mar 2 '15 at 0:54
Note that you may need to add
view -
when piping– user45909
Mar 2 '15 at 0:54
6
6
vim
is an editor, which loads the complete file into memory, whereas less
is a pager, loading the file only partially into memory. You will know the difference with huge files.– sjas
Jul 20 '16 at 12:13
vim
is an editor, which loads the complete file into memory, whereas less
is a pager, loading the file only partially into memory. You will know the difference with huge files.– sjas
Jul 20 '16 at 12:13
|
show 3 more comments
pygmentize
supports the -g
option to automatically guess the lexer to be used which is useful for files read from STDIN
without checking any extension type.
Using that, you only need to set the following 2 exports in your .bashrc
without any additional scripts:
export LESS='-R'
export LESSOPEN='|pygmentize -g %s'
4
Concise and effective. I prefer defining an alias, because sometimes less is better. So: alias lesc='LESS="-R" LESSOPEN="|pygmentize -g %s" less'
– Tiago
Apr 28 '14 at 18:27
add a comment |
pygmentize
supports the -g
option to automatically guess the lexer to be used which is useful for files read from STDIN
without checking any extension type.
Using that, you only need to set the following 2 exports in your .bashrc
without any additional scripts:
export LESS='-R'
export LESSOPEN='|pygmentize -g %s'
4
Concise and effective. I prefer defining an alias, because sometimes less is better. So: alias lesc='LESS="-R" LESSOPEN="|pygmentize -g %s" less'
– Tiago
Apr 28 '14 at 18:27
add a comment |
pygmentize
supports the -g
option to automatically guess the lexer to be used which is useful for files read from STDIN
without checking any extension type.
Using that, you only need to set the following 2 exports in your .bashrc
without any additional scripts:
export LESS='-R'
export LESSOPEN='|pygmentize -g %s'
pygmentize
supports the -g
option to automatically guess the lexer to be used which is useful for files read from STDIN
without checking any extension type.
Using that, you only need to set the following 2 exports in your .bashrc
without any additional scripts:
export LESS='-R'
export LESSOPEN='|pygmentize -g %s'
answered Mar 12 '13 at 7:17
TuxdudeTuxdude
579512
579512
4
Concise and effective. I prefer defining an alias, because sometimes less is better. So: alias lesc='LESS="-R" LESSOPEN="|pygmentize -g %s" less'
– Tiago
Apr 28 '14 at 18:27
add a comment |
4
Concise and effective. I prefer defining an alias, because sometimes less is better. So: alias lesc='LESS="-R" LESSOPEN="|pygmentize -g %s" less'
– Tiago
Apr 28 '14 at 18:27
4
4
Concise and effective. I prefer defining an alias, because sometimes less is better. So: alias lesc='LESS="-R" LESSOPEN="|pygmentize -g %s" less'
– Tiago
Apr 28 '14 at 18:27
Concise and effective. I prefer defining an alias, because sometimes less is better. So: alias lesc='LESS="-R" LESSOPEN="|pygmentize -g %s" less'
– Tiago
Apr 28 '14 at 18:27
add a comment |
To tell less to show colors call it with -R:
less -R
Unfortunately some programs detect that their stdout is not a terminal and disable colors - e.g pacman (Arch Linux package manager).
In those cases its possible to use unbuffer
:
unbuffer <command> | less -R
Example using pacman
unbuffer pacman -Ss firefox | less -R
The unbuffer
command is usually part of the expect-dev
(Debian/Ubuntu) or expect
(Arch Linux) package.
To answer the question for completeness:
As others already answered, pygmentize
is great for colorizing source code. It does not require unbuffer
. Easiest call:
pygmentize someSource.cpp | less -R
2
To useunbuffer
on Ubuntu,sudo apt install expect
– wisbucky
Dec 4 '18 at 18:37
add a comment |
To tell less to show colors call it with -R:
less -R
Unfortunately some programs detect that their stdout is not a terminal and disable colors - e.g pacman (Arch Linux package manager).
In those cases its possible to use unbuffer
:
unbuffer <command> | less -R
Example using pacman
unbuffer pacman -Ss firefox | less -R
The unbuffer
command is usually part of the expect-dev
(Debian/Ubuntu) or expect
(Arch Linux) package.
To answer the question for completeness:
As others already answered, pygmentize
is great for colorizing source code. It does not require unbuffer
. Easiest call:
pygmentize someSource.cpp | less -R
2
To useunbuffer
on Ubuntu,sudo apt install expect
– wisbucky
Dec 4 '18 at 18:37
add a comment |
To tell less to show colors call it with -R:
less -R
Unfortunately some programs detect that their stdout is not a terminal and disable colors - e.g pacman (Arch Linux package manager).
In those cases its possible to use unbuffer
:
unbuffer <command> | less -R
Example using pacman
unbuffer pacman -Ss firefox | less -R
The unbuffer
command is usually part of the expect-dev
(Debian/Ubuntu) or expect
(Arch Linux) package.
To answer the question for completeness:
As others already answered, pygmentize
is great for colorizing source code. It does not require unbuffer
. Easiest call:
pygmentize someSource.cpp | less -R
To tell less to show colors call it with -R:
less -R
Unfortunately some programs detect that their stdout is not a terminal and disable colors - e.g pacman (Arch Linux package manager).
In those cases its possible to use unbuffer
:
unbuffer <command> | less -R
Example using pacman
unbuffer pacman -Ss firefox | less -R
The unbuffer
command is usually part of the expect-dev
(Debian/Ubuntu) or expect
(Arch Linux) package.
To answer the question for completeness:
As others already answered, pygmentize
is great for colorizing source code. It does not require unbuffer
. Easiest call:
pygmentize someSource.cpp | less -R
answered Oct 8 '16 at 10:37
jbbrjbbr
11112
11112
2
To useunbuffer
on Ubuntu,sudo apt install expect
– wisbucky
Dec 4 '18 at 18:37
add a comment |
2
To useunbuffer
on Ubuntu,sudo apt install expect
– wisbucky
Dec 4 '18 at 18:37
2
2
To use
unbuffer
on Ubuntu, sudo apt install expect
– wisbucky
Dec 4 '18 at 18:37
To use
unbuffer
on Ubuntu, sudo apt install expect
– wisbucky
Dec 4 '18 at 18:37
add a comment |
You didn't say what this color should mean, e.g. what should the colors be for a text file?
If what you want is syntax highlighting for source code, you need a source code highlighter. I sometimes use pygmentize like this
pygmentize file.cpp | less
or
pygmentize file.cpp | more
There are other highlighters around.
This is pretty fast. If you don't mind firing up vim
there is a read-only mode that can give you syntax highlighting if you have it in vim
.
view file.cpp
or alternatively see churnd's answer.
add a comment |
You didn't say what this color should mean, e.g. what should the colors be for a text file?
If what you want is syntax highlighting for source code, you need a source code highlighter. I sometimes use pygmentize like this
pygmentize file.cpp | less
or
pygmentize file.cpp | more
There are other highlighters around.
This is pretty fast. If you don't mind firing up vim
there is a read-only mode that can give you syntax highlighting if you have it in vim
.
view file.cpp
or alternatively see churnd's answer.
add a comment |
You didn't say what this color should mean, e.g. what should the colors be for a text file?
If what you want is syntax highlighting for source code, you need a source code highlighter. I sometimes use pygmentize like this
pygmentize file.cpp | less
or
pygmentize file.cpp | more
There are other highlighters around.
This is pretty fast. If you don't mind firing up vim
there is a read-only mode that can give you syntax highlighting if you have it in vim
.
view file.cpp
or alternatively see churnd's answer.
You didn't say what this color should mean, e.g. what should the colors be for a text file?
If what you want is syntax highlighting for source code, you need a source code highlighter. I sometimes use pygmentize like this
pygmentize file.cpp | less
or
pygmentize file.cpp | more
There are other highlighters around.
This is pretty fast. If you don't mind firing up vim
there is a read-only mode that can give you syntax highlighting if you have it in vim
.
view file.cpp
or alternatively see churnd's answer.
answered Mar 9 '10 at 13:30
Benjamin BannierBenjamin Bannier
13k23737
13k23737
add a comment |
add a comment |
This is yet another pygments-based answer, with several major improvements:
- does not break
lesspipe
orlessfile
filters - works with multiple inputs to
less
- correctly parses the script type from the shebang header
- works for all 434 file types lexable by Pygments
- color scheme is parameterized as an environment variable
Install Pygments and Gawk
sudo apt-get install python-pygments python3-pygments gawk
Set Environment Variables
Check whether lesspipe
or lessfile
is already enabled:
echo $LESSOPEN
If you don't see either program referenced there, ensure that lesspipe
is installed (most distros come with it).
Add the following to ~/.bashrc
:
# sets LESSOPEN and LESSCLOSE variables
eval "$(SHELL=/bin/sh lesspipe)"
# interpret color characters
export LESS='-R'
# to list available styles: `pygmentize -L styles`
export PYGMENTIZE_STYLE='paraiso-dark'
# optional
alias ls='ls --color=always'
alias grep='grep --color=always'
If you don't want lesspipe
, replace the eval
statement with:
export LESSOPEN='|~/.lessfilter %s'
Create ~/.lessfilter
Add the following code and make the file executable: chmod u+x ~/.lessfilter
#!/bin/bash
for path in "$@"; do
# match by known filenames
filename=$(basename "$path")
case "$filename" in
.bashrc|bash.bashrc|.bash_aliases|.bash_environment|.bash_profile|
.bash_login|.bash_logout|.profile|.zshrc|.zprofile|.zshrc|.zlogin|
.zlogout|zshrc|zprofile|zshrc|zlogin|zlogout|.cshrc|.cshdirs|
csh.cshrc|csh.login|csh.logout|.tcshrc|.kshrc|ksh.kshrc)
# shell lexer
pygmentize -f 256 -O style=$PYGMENTIZE_STYLE -l sh "$path"
;;
.htaccess|apache.conf|apache2.conf|Dockerfile|Kconfig|external.in*|
standard-modules.in|nginx.conf|pacman.conf|squid.conf|termcap|
termcap.src|terminfo|terminfo.src|control|sources.list|CMakeLists.txt|
Makefile|makefile|Makefile.*|GNUmakefile|SConstruct|SConscript|
.Rhistory|.Rprofile|.Renviron|Rakefile|Gemfile|PKGBUILD|autohandler|
dhandler|autodelegate|.vimrc|.exrc|.gvimrc|vimrc|exrc|gvimrc|todo.txt)
# filename recognized
pygmentize -f 256 -O style=$PYGMENTIZE_STYLE "$path"
;;
*)
ext=$([[ "$filename" = *.* ]] && echo ".${filename##*.}" || echo '')
case "$ext" in
.as|.mxml|.bc|.g|.gd|.gi|.gap|.nb|.cdf|.nbp|.ma|.mu|.at|.run|
.apl|.adl|.adls|.adlf|.adlx|.cadl|.odin|.c-objdump|.s|
.cpp-objdump|.c++-objdump|.cxx-objdump|.d-objdump|.S|.hsail|
.ll|.asm|.ASM|.objdump-intel|.objdump|.tasm|.au3|.ahk|.ahkl|
.bb|.decls|.bmx|.bas|.monkey|.BAS|.bst|.bib|.abap|.ABAP|.cbl|
.CBL|.cob|.COB|.cpy|.CPY|.gdc|.maql|.p|.cls|.c|.h|.idc|.cpp|
.hpp|.c++|.h++|.cc|.hh|.cxx|.hxx|.C|.H|.cp|.CPP|.ino|.clay|
.cu|.cuh|.ec|.eh|.mq4|.mq5|.mqh|.nc|.pike|.pmod|.swg|.i|.vala|
.vapi|.capnp|.chpl|.icl|.dcl|.cf|.docker|.ini|.cfg|.inf|
.pc|.properties|.reg|.tf|.pypylog|.cr|.csd|.orc|.sco|.css|
.less|.sass|.scss|.croc|.d|.di|.smali|.jsonld|.json|.yaml|
.yml|.dpatch|.darcspatch|.diff|.patch|.wdiff|.boo|.aspx|.asax|
.ascx|.ashx|.asmx|.axd|.cs|.fs|.fsi|.n|.vb|.als|.bro|.crmsh|
.pcmk|.msc|.pan|.proto|.pp|.rsl|.sbl|.thrift|.rpf|
.dylan-console|.dylan|.dyl|.intr|.lid|.hdp|.ecl|.e|.elm|.ex|
.exs|.erl|.hrl|.es|.escript|.erl-sh|.aheui|.befunge|.bf|.b|
.camkes|.idl4|.cdl|.cw|.factor|.fan|.flx|.flxh|.frt|.f|.F|
.f03|.f90|.F03|.F90|.PRG|.prg|.go|.abnf|.bnf|.jsgf|.cyp|
.cypher|.asy|.vert|.frag|.geo|.plot|.plt|.ps|.eps|.pov|.inc|
.agda|.cry|.hs|.idr|.kk|.kki|.lagda|.lcry|.lhs|.lidr|.hx|
.hxsl|.hxml|.sv|.svh|.v|.vhdl|.vhd|.dtd|.haml|.html|.htm|
.xhtml|.xslt|.pug|.jade|.scaml|.xml|.xsl|.rss|.xsd|.wsdl|
.wsf|.xpl|.pro|.ipf|.nsi|.nsh|.spec|.i6t|.ni|.i7x|.t|.io|
.ijs|.coffee|.dart|.eg|.js|.jsm|.juttle|.kal|.lasso|
.lasso[89]|.ls|.mask|.j|.ts|.tsx|.jl|.aj|.ceylon|.clj|
.cljs|.golo|.gs|.gsx|.gsp|.vark|.gst|.groovy|.gradle|.ik|
.java|.kt|.pig|.scala|.xtend|.cpsa|.cl|.lisp|.el|.hy|.lsp|.nl|
.kif|.rkt|.rktd|.rktl|.scm|.ss|.shen|.xtm|.cmake|.mak|.mk|
.[1234567]|.man|.md|.css.in|.js.in|.xul.in|.rst|.rest|.tex|
.aux|.toc|.m|.sci|.sce|.tst|.ml|.mli|.mll|.mly|.opa|.sml|.sig|
.fun|.bug|.jag|.mo|.stan|.def|.mod|.mt|.ncl|.nim|.nimrod|.nit|
.nix|.cps|.x|.xi|.xm|.xmi|.mm|.swift|.ooc|.psi|.psl|.G|.ebnf|
.rl|.treetop|.tt|.adb|.ads|.ada|.pas|.dpr|.pwn|.sp|.pl|.pm|
.nqp|.p6|.6pl|.p6l|.pl6|.6pm|.p6m|.pm6|.php|.php[345]|.zep|
.praat|.proc|.psc|.lgt|.logtalk|.prolog|.pyx|.pxd|.pxi|.dg|
.py3tb|.py|.pyw|.sc|.tac|.sage|.pytb|.qvto|.Rout|.Rd|.R|.rq|
.sparql|.ttl|.r|.r3|.reb|.red|.reds|.txt|.rnc|.graph|
.instances|.robot|.fy|.fancypack|.rb|.rbw|.rake|.gemspec|
.rbx|.duby|.rs|.rs.in|.SAS|.sas|.applescript|.chai|.ezt|
.mac|.hyb|.jcl|.lsl|.lua|.wlua|.moo|.moon|.rexx|.rex|.rx|
.arexx|.sh|.ksh|.bash|.ebuild|.eclass|.exheres-0|.exlib|.zsh|
.sh-session|.shell-session|.bat|.cmd|.fish|.load|.ps1|.psm1|
.tcsh|.csh|.ns2|.st|.smv|.snobol|.rql|.sql|.sqlite3-console|
.do|.ado|.scd|.tcl|.rvt|.ng2|.tmpl|.spt|.cfc|.cfm|.cfml|
.evoque|.kid|.handlebars|.hbs|.phtml|.jsp|.liquid|.mao|.mhtml|
.mc|.mi|.myt|.rhtml|.tpl|.ssp|.tea|.twig|.vm|.fhtml|.sls|
.feature|.tap|.awk|.vim|.pot|.po|.weechatlog|.todotxt|.thy|
.lean|.rts|.u|.vcl|.bpl|.sil|.vpr|.cirru|.duel|.jbst|.qml|
.qbs|.slim|.xqy|.xquery|.xq|.xql|.xqm|.whiley|.x10)
# extension recognized
pygmentize -f 256 -O style=$PYGMENTIZE_STYLE "$path"
;;
*)
# parse the shebang script header if it exists
lexer=$(head -n 1 "$path" |grep "^#!" |awk -F" "
'match($1, //(w*)$/, a) {if (a[1]!="env") {print a[1]} else {print $2}}')
case "$lexer" in
node|nodejs)
# workaround for lack of Node.js lexer alias
pygmentize -f 256 -O style=$PYGMENTIZE_STYLE
-l js "$path"
;;
"")
exit 1
;;
*)
pygmentize -f 256 -O style=$PYGMENTIZE_STYLE
-l $lexer "$path"
;;
esac
;;
esac
;;
esac
done
exit 0
The one drawback about this approach is that Pygments is a Python program, and so on first use during a shell session, there is a "cold start" delay. Subsequent invocations are much faster.
– Joe Coder
Oct 12 '18 at 1:52
Nice! Really comprehensive.
– Dario Seidl
Nov 11 '18 at 16:01
add a comment |
This is yet another pygments-based answer, with several major improvements:
- does not break
lesspipe
orlessfile
filters - works with multiple inputs to
less
- correctly parses the script type from the shebang header
- works for all 434 file types lexable by Pygments
- color scheme is parameterized as an environment variable
Install Pygments and Gawk
sudo apt-get install python-pygments python3-pygments gawk
Set Environment Variables
Check whether lesspipe
or lessfile
is already enabled:
echo $LESSOPEN
If you don't see either program referenced there, ensure that lesspipe
is installed (most distros come with it).
Add the following to ~/.bashrc
:
# sets LESSOPEN and LESSCLOSE variables
eval "$(SHELL=/bin/sh lesspipe)"
# interpret color characters
export LESS='-R'
# to list available styles: `pygmentize -L styles`
export PYGMENTIZE_STYLE='paraiso-dark'
# optional
alias ls='ls --color=always'
alias grep='grep --color=always'
If you don't want lesspipe
, replace the eval
statement with:
export LESSOPEN='|~/.lessfilter %s'
Create ~/.lessfilter
Add the following code and make the file executable: chmod u+x ~/.lessfilter
#!/bin/bash
for path in "$@"; do
# match by known filenames
filename=$(basename "$path")
case "$filename" in
.bashrc|bash.bashrc|.bash_aliases|.bash_environment|.bash_profile|
.bash_login|.bash_logout|.profile|.zshrc|.zprofile|.zshrc|.zlogin|
.zlogout|zshrc|zprofile|zshrc|zlogin|zlogout|.cshrc|.cshdirs|
csh.cshrc|csh.login|csh.logout|.tcshrc|.kshrc|ksh.kshrc)
# shell lexer
pygmentize -f 256 -O style=$PYGMENTIZE_STYLE -l sh "$path"
;;
.htaccess|apache.conf|apache2.conf|Dockerfile|Kconfig|external.in*|
standard-modules.in|nginx.conf|pacman.conf|squid.conf|termcap|
termcap.src|terminfo|terminfo.src|control|sources.list|CMakeLists.txt|
Makefile|makefile|Makefile.*|GNUmakefile|SConstruct|SConscript|
.Rhistory|.Rprofile|.Renviron|Rakefile|Gemfile|PKGBUILD|autohandler|
dhandler|autodelegate|.vimrc|.exrc|.gvimrc|vimrc|exrc|gvimrc|todo.txt)
# filename recognized
pygmentize -f 256 -O style=$PYGMENTIZE_STYLE "$path"
;;
*)
ext=$([[ "$filename" = *.* ]] && echo ".${filename##*.}" || echo '')
case "$ext" in
.as|.mxml|.bc|.g|.gd|.gi|.gap|.nb|.cdf|.nbp|.ma|.mu|.at|.run|
.apl|.adl|.adls|.adlf|.adlx|.cadl|.odin|.c-objdump|.s|
.cpp-objdump|.c++-objdump|.cxx-objdump|.d-objdump|.S|.hsail|
.ll|.asm|.ASM|.objdump-intel|.objdump|.tasm|.au3|.ahk|.ahkl|
.bb|.decls|.bmx|.bas|.monkey|.BAS|.bst|.bib|.abap|.ABAP|.cbl|
.CBL|.cob|.COB|.cpy|.CPY|.gdc|.maql|.p|.cls|.c|.h|.idc|.cpp|
.hpp|.c++|.h++|.cc|.hh|.cxx|.hxx|.C|.H|.cp|.CPP|.ino|.clay|
.cu|.cuh|.ec|.eh|.mq4|.mq5|.mqh|.nc|.pike|.pmod|.swg|.i|.vala|
.vapi|.capnp|.chpl|.icl|.dcl|.cf|.docker|.ini|.cfg|.inf|
.pc|.properties|.reg|.tf|.pypylog|.cr|.csd|.orc|.sco|.css|
.less|.sass|.scss|.croc|.d|.di|.smali|.jsonld|.json|.yaml|
.yml|.dpatch|.darcspatch|.diff|.patch|.wdiff|.boo|.aspx|.asax|
.ascx|.ashx|.asmx|.axd|.cs|.fs|.fsi|.n|.vb|.als|.bro|.crmsh|
.pcmk|.msc|.pan|.proto|.pp|.rsl|.sbl|.thrift|.rpf|
.dylan-console|.dylan|.dyl|.intr|.lid|.hdp|.ecl|.e|.elm|.ex|
.exs|.erl|.hrl|.es|.escript|.erl-sh|.aheui|.befunge|.bf|.b|
.camkes|.idl4|.cdl|.cw|.factor|.fan|.flx|.flxh|.frt|.f|.F|
.f03|.f90|.F03|.F90|.PRG|.prg|.go|.abnf|.bnf|.jsgf|.cyp|
.cypher|.asy|.vert|.frag|.geo|.plot|.plt|.ps|.eps|.pov|.inc|
.agda|.cry|.hs|.idr|.kk|.kki|.lagda|.lcry|.lhs|.lidr|.hx|
.hxsl|.hxml|.sv|.svh|.v|.vhdl|.vhd|.dtd|.haml|.html|.htm|
.xhtml|.xslt|.pug|.jade|.scaml|.xml|.xsl|.rss|.xsd|.wsdl|
.wsf|.xpl|.pro|.ipf|.nsi|.nsh|.spec|.i6t|.ni|.i7x|.t|.io|
.ijs|.coffee|.dart|.eg|.js|.jsm|.juttle|.kal|.lasso|
.lasso[89]|.ls|.mask|.j|.ts|.tsx|.jl|.aj|.ceylon|.clj|
.cljs|.golo|.gs|.gsx|.gsp|.vark|.gst|.groovy|.gradle|.ik|
.java|.kt|.pig|.scala|.xtend|.cpsa|.cl|.lisp|.el|.hy|.lsp|.nl|
.kif|.rkt|.rktd|.rktl|.scm|.ss|.shen|.xtm|.cmake|.mak|.mk|
.[1234567]|.man|.md|.css.in|.js.in|.xul.in|.rst|.rest|.tex|
.aux|.toc|.m|.sci|.sce|.tst|.ml|.mli|.mll|.mly|.opa|.sml|.sig|
.fun|.bug|.jag|.mo|.stan|.def|.mod|.mt|.ncl|.nim|.nimrod|.nit|
.nix|.cps|.x|.xi|.xm|.xmi|.mm|.swift|.ooc|.psi|.psl|.G|.ebnf|
.rl|.treetop|.tt|.adb|.ads|.ada|.pas|.dpr|.pwn|.sp|.pl|.pm|
.nqp|.p6|.6pl|.p6l|.pl6|.6pm|.p6m|.pm6|.php|.php[345]|.zep|
.praat|.proc|.psc|.lgt|.logtalk|.prolog|.pyx|.pxd|.pxi|.dg|
.py3tb|.py|.pyw|.sc|.tac|.sage|.pytb|.qvto|.Rout|.Rd|.R|.rq|
.sparql|.ttl|.r|.r3|.reb|.red|.reds|.txt|.rnc|.graph|
.instances|.robot|.fy|.fancypack|.rb|.rbw|.rake|.gemspec|
.rbx|.duby|.rs|.rs.in|.SAS|.sas|.applescript|.chai|.ezt|
.mac|.hyb|.jcl|.lsl|.lua|.wlua|.moo|.moon|.rexx|.rex|.rx|
.arexx|.sh|.ksh|.bash|.ebuild|.eclass|.exheres-0|.exlib|.zsh|
.sh-session|.shell-session|.bat|.cmd|.fish|.load|.ps1|.psm1|
.tcsh|.csh|.ns2|.st|.smv|.snobol|.rql|.sql|.sqlite3-console|
.do|.ado|.scd|.tcl|.rvt|.ng2|.tmpl|.spt|.cfc|.cfm|.cfml|
.evoque|.kid|.handlebars|.hbs|.phtml|.jsp|.liquid|.mao|.mhtml|
.mc|.mi|.myt|.rhtml|.tpl|.ssp|.tea|.twig|.vm|.fhtml|.sls|
.feature|.tap|.awk|.vim|.pot|.po|.weechatlog|.todotxt|.thy|
.lean|.rts|.u|.vcl|.bpl|.sil|.vpr|.cirru|.duel|.jbst|.qml|
.qbs|.slim|.xqy|.xquery|.xq|.xql|.xqm|.whiley|.x10)
# extension recognized
pygmentize -f 256 -O style=$PYGMENTIZE_STYLE "$path"
;;
*)
# parse the shebang script header if it exists
lexer=$(head -n 1 "$path" |grep "^#!" |awk -F" "
'match($1, //(w*)$/, a) {if (a[1]!="env") {print a[1]} else {print $2}}')
case "$lexer" in
node|nodejs)
# workaround for lack of Node.js lexer alias
pygmentize -f 256 -O style=$PYGMENTIZE_STYLE
-l js "$path"
;;
"")
exit 1
;;
*)
pygmentize -f 256 -O style=$PYGMENTIZE_STYLE
-l $lexer "$path"
;;
esac
;;
esac
;;
esac
done
exit 0
The one drawback about this approach is that Pygments is a Python program, and so on first use during a shell session, there is a "cold start" delay. Subsequent invocations are much faster.
– Joe Coder
Oct 12 '18 at 1:52
Nice! Really comprehensive.
– Dario Seidl
Nov 11 '18 at 16:01
add a comment |
This is yet another pygments-based answer, with several major improvements:
- does not break
lesspipe
orlessfile
filters - works with multiple inputs to
less
- correctly parses the script type from the shebang header
- works for all 434 file types lexable by Pygments
- color scheme is parameterized as an environment variable
Install Pygments and Gawk
sudo apt-get install python-pygments python3-pygments gawk
Set Environment Variables
Check whether lesspipe
or lessfile
is already enabled:
echo $LESSOPEN
If you don't see either program referenced there, ensure that lesspipe
is installed (most distros come with it).
Add the following to ~/.bashrc
:
# sets LESSOPEN and LESSCLOSE variables
eval "$(SHELL=/bin/sh lesspipe)"
# interpret color characters
export LESS='-R'
# to list available styles: `pygmentize -L styles`
export PYGMENTIZE_STYLE='paraiso-dark'
# optional
alias ls='ls --color=always'
alias grep='grep --color=always'
If you don't want lesspipe
, replace the eval
statement with:
export LESSOPEN='|~/.lessfilter %s'
Create ~/.lessfilter
Add the following code and make the file executable: chmod u+x ~/.lessfilter
#!/bin/bash
for path in "$@"; do
# match by known filenames
filename=$(basename "$path")
case "$filename" in
.bashrc|bash.bashrc|.bash_aliases|.bash_environment|.bash_profile|
.bash_login|.bash_logout|.profile|.zshrc|.zprofile|.zshrc|.zlogin|
.zlogout|zshrc|zprofile|zshrc|zlogin|zlogout|.cshrc|.cshdirs|
csh.cshrc|csh.login|csh.logout|.tcshrc|.kshrc|ksh.kshrc)
# shell lexer
pygmentize -f 256 -O style=$PYGMENTIZE_STYLE -l sh "$path"
;;
.htaccess|apache.conf|apache2.conf|Dockerfile|Kconfig|external.in*|
standard-modules.in|nginx.conf|pacman.conf|squid.conf|termcap|
termcap.src|terminfo|terminfo.src|control|sources.list|CMakeLists.txt|
Makefile|makefile|Makefile.*|GNUmakefile|SConstruct|SConscript|
.Rhistory|.Rprofile|.Renviron|Rakefile|Gemfile|PKGBUILD|autohandler|
dhandler|autodelegate|.vimrc|.exrc|.gvimrc|vimrc|exrc|gvimrc|todo.txt)
# filename recognized
pygmentize -f 256 -O style=$PYGMENTIZE_STYLE "$path"
;;
*)
ext=$([[ "$filename" = *.* ]] && echo ".${filename##*.}" || echo '')
case "$ext" in
.as|.mxml|.bc|.g|.gd|.gi|.gap|.nb|.cdf|.nbp|.ma|.mu|.at|.run|
.apl|.adl|.adls|.adlf|.adlx|.cadl|.odin|.c-objdump|.s|
.cpp-objdump|.c++-objdump|.cxx-objdump|.d-objdump|.S|.hsail|
.ll|.asm|.ASM|.objdump-intel|.objdump|.tasm|.au3|.ahk|.ahkl|
.bb|.decls|.bmx|.bas|.monkey|.BAS|.bst|.bib|.abap|.ABAP|.cbl|
.CBL|.cob|.COB|.cpy|.CPY|.gdc|.maql|.p|.cls|.c|.h|.idc|.cpp|
.hpp|.c++|.h++|.cc|.hh|.cxx|.hxx|.C|.H|.cp|.CPP|.ino|.clay|
.cu|.cuh|.ec|.eh|.mq4|.mq5|.mqh|.nc|.pike|.pmod|.swg|.i|.vala|
.vapi|.capnp|.chpl|.icl|.dcl|.cf|.docker|.ini|.cfg|.inf|
.pc|.properties|.reg|.tf|.pypylog|.cr|.csd|.orc|.sco|.css|
.less|.sass|.scss|.croc|.d|.di|.smali|.jsonld|.json|.yaml|
.yml|.dpatch|.darcspatch|.diff|.patch|.wdiff|.boo|.aspx|.asax|
.ascx|.ashx|.asmx|.axd|.cs|.fs|.fsi|.n|.vb|.als|.bro|.crmsh|
.pcmk|.msc|.pan|.proto|.pp|.rsl|.sbl|.thrift|.rpf|
.dylan-console|.dylan|.dyl|.intr|.lid|.hdp|.ecl|.e|.elm|.ex|
.exs|.erl|.hrl|.es|.escript|.erl-sh|.aheui|.befunge|.bf|.b|
.camkes|.idl4|.cdl|.cw|.factor|.fan|.flx|.flxh|.frt|.f|.F|
.f03|.f90|.F03|.F90|.PRG|.prg|.go|.abnf|.bnf|.jsgf|.cyp|
.cypher|.asy|.vert|.frag|.geo|.plot|.plt|.ps|.eps|.pov|.inc|
.agda|.cry|.hs|.idr|.kk|.kki|.lagda|.lcry|.lhs|.lidr|.hx|
.hxsl|.hxml|.sv|.svh|.v|.vhdl|.vhd|.dtd|.haml|.html|.htm|
.xhtml|.xslt|.pug|.jade|.scaml|.xml|.xsl|.rss|.xsd|.wsdl|
.wsf|.xpl|.pro|.ipf|.nsi|.nsh|.spec|.i6t|.ni|.i7x|.t|.io|
.ijs|.coffee|.dart|.eg|.js|.jsm|.juttle|.kal|.lasso|
.lasso[89]|.ls|.mask|.j|.ts|.tsx|.jl|.aj|.ceylon|.clj|
.cljs|.golo|.gs|.gsx|.gsp|.vark|.gst|.groovy|.gradle|.ik|
.java|.kt|.pig|.scala|.xtend|.cpsa|.cl|.lisp|.el|.hy|.lsp|.nl|
.kif|.rkt|.rktd|.rktl|.scm|.ss|.shen|.xtm|.cmake|.mak|.mk|
.[1234567]|.man|.md|.css.in|.js.in|.xul.in|.rst|.rest|.tex|
.aux|.toc|.m|.sci|.sce|.tst|.ml|.mli|.mll|.mly|.opa|.sml|.sig|
.fun|.bug|.jag|.mo|.stan|.def|.mod|.mt|.ncl|.nim|.nimrod|.nit|
.nix|.cps|.x|.xi|.xm|.xmi|.mm|.swift|.ooc|.psi|.psl|.G|.ebnf|
.rl|.treetop|.tt|.adb|.ads|.ada|.pas|.dpr|.pwn|.sp|.pl|.pm|
.nqp|.p6|.6pl|.p6l|.pl6|.6pm|.p6m|.pm6|.php|.php[345]|.zep|
.praat|.proc|.psc|.lgt|.logtalk|.prolog|.pyx|.pxd|.pxi|.dg|
.py3tb|.py|.pyw|.sc|.tac|.sage|.pytb|.qvto|.Rout|.Rd|.R|.rq|
.sparql|.ttl|.r|.r3|.reb|.red|.reds|.txt|.rnc|.graph|
.instances|.robot|.fy|.fancypack|.rb|.rbw|.rake|.gemspec|
.rbx|.duby|.rs|.rs.in|.SAS|.sas|.applescript|.chai|.ezt|
.mac|.hyb|.jcl|.lsl|.lua|.wlua|.moo|.moon|.rexx|.rex|.rx|
.arexx|.sh|.ksh|.bash|.ebuild|.eclass|.exheres-0|.exlib|.zsh|
.sh-session|.shell-session|.bat|.cmd|.fish|.load|.ps1|.psm1|
.tcsh|.csh|.ns2|.st|.smv|.snobol|.rql|.sql|.sqlite3-console|
.do|.ado|.scd|.tcl|.rvt|.ng2|.tmpl|.spt|.cfc|.cfm|.cfml|
.evoque|.kid|.handlebars|.hbs|.phtml|.jsp|.liquid|.mao|.mhtml|
.mc|.mi|.myt|.rhtml|.tpl|.ssp|.tea|.twig|.vm|.fhtml|.sls|
.feature|.tap|.awk|.vim|.pot|.po|.weechatlog|.todotxt|.thy|
.lean|.rts|.u|.vcl|.bpl|.sil|.vpr|.cirru|.duel|.jbst|.qml|
.qbs|.slim|.xqy|.xquery|.xq|.xql|.xqm|.whiley|.x10)
# extension recognized
pygmentize -f 256 -O style=$PYGMENTIZE_STYLE "$path"
;;
*)
# parse the shebang script header if it exists
lexer=$(head -n 1 "$path" |grep "^#!" |awk -F" "
'match($1, //(w*)$/, a) {if (a[1]!="env") {print a[1]} else {print $2}}')
case "$lexer" in
node|nodejs)
# workaround for lack of Node.js lexer alias
pygmentize -f 256 -O style=$PYGMENTIZE_STYLE
-l js "$path"
;;
"")
exit 1
;;
*)
pygmentize -f 256 -O style=$PYGMENTIZE_STYLE
-l $lexer "$path"
;;
esac
;;
esac
;;
esac
done
exit 0
This is yet another pygments-based answer, with several major improvements:
- does not break
lesspipe
orlessfile
filters - works with multiple inputs to
less
- correctly parses the script type from the shebang header
- works for all 434 file types lexable by Pygments
- color scheme is parameterized as an environment variable
Install Pygments and Gawk
sudo apt-get install python-pygments python3-pygments gawk
Set Environment Variables
Check whether lesspipe
or lessfile
is already enabled:
echo $LESSOPEN
If you don't see either program referenced there, ensure that lesspipe
is installed (most distros come with it).
Add the following to ~/.bashrc
:
# sets LESSOPEN and LESSCLOSE variables
eval "$(SHELL=/bin/sh lesspipe)"
# interpret color characters
export LESS='-R'
# to list available styles: `pygmentize -L styles`
export PYGMENTIZE_STYLE='paraiso-dark'
# optional
alias ls='ls --color=always'
alias grep='grep --color=always'
If you don't want lesspipe
, replace the eval
statement with:
export LESSOPEN='|~/.lessfilter %s'
Create ~/.lessfilter
Add the following code and make the file executable: chmod u+x ~/.lessfilter
#!/bin/bash
for path in "$@"; do
# match by known filenames
filename=$(basename "$path")
case "$filename" in
.bashrc|bash.bashrc|.bash_aliases|.bash_environment|.bash_profile|
.bash_login|.bash_logout|.profile|.zshrc|.zprofile|.zshrc|.zlogin|
.zlogout|zshrc|zprofile|zshrc|zlogin|zlogout|.cshrc|.cshdirs|
csh.cshrc|csh.login|csh.logout|.tcshrc|.kshrc|ksh.kshrc)
# shell lexer
pygmentize -f 256 -O style=$PYGMENTIZE_STYLE -l sh "$path"
;;
.htaccess|apache.conf|apache2.conf|Dockerfile|Kconfig|external.in*|
standard-modules.in|nginx.conf|pacman.conf|squid.conf|termcap|
termcap.src|terminfo|terminfo.src|control|sources.list|CMakeLists.txt|
Makefile|makefile|Makefile.*|GNUmakefile|SConstruct|SConscript|
.Rhistory|.Rprofile|.Renviron|Rakefile|Gemfile|PKGBUILD|autohandler|
dhandler|autodelegate|.vimrc|.exrc|.gvimrc|vimrc|exrc|gvimrc|todo.txt)
# filename recognized
pygmentize -f 256 -O style=$PYGMENTIZE_STYLE "$path"
;;
*)
ext=$([[ "$filename" = *.* ]] && echo ".${filename##*.}" || echo '')
case "$ext" in
.as|.mxml|.bc|.g|.gd|.gi|.gap|.nb|.cdf|.nbp|.ma|.mu|.at|.run|
.apl|.adl|.adls|.adlf|.adlx|.cadl|.odin|.c-objdump|.s|
.cpp-objdump|.c++-objdump|.cxx-objdump|.d-objdump|.S|.hsail|
.ll|.asm|.ASM|.objdump-intel|.objdump|.tasm|.au3|.ahk|.ahkl|
.bb|.decls|.bmx|.bas|.monkey|.BAS|.bst|.bib|.abap|.ABAP|.cbl|
.CBL|.cob|.COB|.cpy|.CPY|.gdc|.maql|.p|.cls|.c|.h|.idc|.cpp|
.hpp|.c++|.h++|.cc|.hh|.cxx|.hxx|.C|.H|.cp|.CPP|.ino|.clay|
.cu|.cuh|.ec|.eh|.mq4|.mq5|.mqh|.nc|.pike|.pmod|.swg|.i|.vala|
.vapi|.capnp|.chpl|.icl|.dcl|.cf|.docker|.ini|.cfg|.inf|
.pc|.properties|.reg|.tf|.pypylog|.cr|.csd|.orc|.sco|.css|
.less|.sass|.scss|.croc|.d|.di|.smali|.jsonld|.json|.yaml|
.yml|.dpatch|.darcspatch|.diff|.patch|.wdiff|.boo|.aspx|.asax|
.ascx|.ashx|.asmx|.axd|.cs|.fs|.fsi|.n|.vb|.als|.bro|.crmsh|
.pcmk|.msc|.pan|.proto|.pp|.rsl|.sbl|.thrift|.rpf|
.dylan-console|.dylan|.dyl|.intr|.lid|.hdp|.ecl|.e|.elm|.ex|
.exs|.erl|.hrl|.es|.escript|.erl-sh|.aheui|.befunge|.bf|.b|
.camkes|.idl4|.cdl|.cw|.factor|.fan|.flx|.flxh|.frt|.f|.F|
.f03|.f90|.F03|.F90|.PRG|.prg|.go|.abnf|.bnf|.jsgf|.cyp|
.cypher|.asy|.vert|.frag|.geo|.plot|.plt|.ps|.eps|.pov|.inc|
.agda|.cry|.hs|.idr|.kk|.kki|.lagda|.lcry|.lhs|.lidr|.hx|
.hxsl|.hxml|.sv|.svh|.v|.vhdl|.vhd|.dtd|.haml|.html|.htm|
.xhtml|.xslt|.pug|.jade|.scaml|.xml|.xsl|.rss|.xsd|.wsdl|
.wsf|.xpl|.pro|.ipf|.nsi|.nsh|.spec|.i6t|.ni|.i7x|.t|.io|
.ijs|.coffee|.dart|.eg|.js|.jsm|.juttle|.kal|.lasso|
.lasso[89]|.ls|.mask|.j|.ts|.tsx|.jl|.aj|.ceylon|.clj|
.cljs|.golo|.gs|.gsx|.gsp|.vark|.gst|.groovy|.gradle|.ik|
.java|.kt|.pig|.scala|.xtend|.cpsa|.cl|.lisp|.el|.hy|.lsp|.nl|
.kif|.rkt|.rktd|.rktl|.scm|.ss|.shen|.xtm|.cmake|.mak|.mk|
.[1234567]|.man|.md|.css.in|.js.in|.xul.in|.rst|.rest|.tex|
.aux|.toc|.m|.sci|.sce|.tst|.ml|.mli|.mll|.mly|.opa|.sml|.sig|
.fun|.bug|.jag|.mo|.stan|.def|.mod|.mt|.ncl|.nim|.nimrod|.nit|
.nix|.cps|.x|.xi|.xm|.xmi|.mm|.swift|.ooc|.psi|.psl|.G|.ebnf|
.rl|.treetop|.tt|.adb|.ads|.ada|.pas|.dpr|.pwn|.sp|.pl|.pm|
.nqp|.p6|.6pl|.p6l|.pl6|.6pm|.p6m|.pm6|.php|.php[345]|.zep|
.praat|.proc|.psc|.lgt|.logtalk|.prolog|.pyx|.pxd|.pxi|.dg|
.py3tb|.py|.pyw|.sc|.tac|.sage|.pytb|.qvto|.Rout|.Rd|.R|.rq|
.sparql|.ttl|.r|.r3|.reb|.red|.reds|.txt|.rnc|.graph|
.instances|.robot|.fy|.fancypack|.rb|.rbw|.rake|.gemspec|
.rbx|.duby|.rs|.rs.in|.SAS|.sas|.applescript|.chai|.ezt|
.mac|.hyb|.jcl|.lsl|.lua|.wlua|.moo|.moon|.rexx|.rex|.rx|
.arexx|.sh|.ksh|.bash|.ebuild|.eclass|.exheres-0|.exlib|.zsh|
.sh-session|.shell-session|.bat|.cmd|.fish|.load|.ps1|.psm1|
.tcsh|.csh|.ns2|.st|.smv|.snobol|.rql|.sql|.sqlite3-console|
.do|.ado|.scd|.tcl|.rvt|.ng2|.tmpl|.spt|.cfc|.cfm|.cfml|
.evoque|.kid|.handlebars|.hbs|.phtml|.jsp|.liquid|.mao|.mhtml|
.mc|.mi|.myt|.rhtml|.tpl|.ssp|.tea|.twig|.vm|.fhtml|.sls|
.feature|.tap|.awk|.vim|.pot|.po|.weechatlog|.todotxt|.thy|
.lean|.rts|.u|.vcl|.bpl|.sil|.vpr|.cirru|.duel|.jbst|.qml|
.qbs|.slim|.xqy|.xquery|.xq|.xql|.xqm|.whiley|.x10)
# extension recognized
pygmentize -f 256 -O style=$PYGMENTIZE_STYLE "$path"
;;
*)
# parse the shebang script header if it exists
lexer=$(head -n 1 "$path" |grep "^#!" |awk -F" "
'match($1, //(w*)$/, a) {if (a[1]!="env") {print a[1]} else {print $2}}')
case "$lexer" in
node|nodejs)
# workaround for lack of Node.js lexer alias
pygmentize -f 256 -O style=$PYGMENTIZE_STYLE
-l js "$path"
;;
"")
exit 1
;;
*)
pygmentize -f 256 -O style=$PYGMENTIZE_STYLE
-l $lexer "$path"
;;
esac
;;
esac
;;
esac
done
exit 0
edited Jan 2 '18 at 12:43
answered May 29 '17 at 9:21
Joe CoderJoe Coder
15115
15115
The one drawback about this approach is that Pygments is a Python program, and so on first use during a shell session, there is a "cold start" delay. Subsequent invocations are much faster.
– Joe Coder
Oct 12 '18 at 1:52
Nice! Really comprehensive.
– Dario Seidl
Nov 11 '18 at 16:01
add a comment |
The one drawback about this approach is that Pygments is a Python program, and so on first use during a shell session, there is a "cold start" delay. Subsequent invocations are much faster.
– Joe Coder
Oct 12 '18 at 1:52
Nice! Really comprehensive.
– Dario Seidl
Nov 11 '18 at 16:01
The one drawback about this approach is that Pygments is a Python program, and so on first use during a shell session, there is a "cold start" delay. Subsequent invocations are much faster.
– Joe Coder
Oct 12 '18 at 1:52
The one drawback about this approach is that Pygments is a Python program, and so on first use during a shell session, there is a "cold start" delay. Subsequent invocations are much faster.
– Joe Coder
Oct 12 '18 at 1:52
Nice! Really comprehensive.
– Dario Seidl
Nov 11 '18 at 16:01
Nice! Really comprehensive.
– Dario Seidl
Nov 11 '18 at 16:01
add a comment |
Use the GNU Source-highlight; you can install it with apt
if you have it, or otherwise install it from source. Then set up an "input preprocessor" for less, with help from the Source-highligh' documentations for setting up with less:
This was suggested by Konstantine Serebriany. The script src-hilite-lesspipe.sh will be installed together with source-highlight. You can use the following environment variables:
export LESSOPEN="| /path/to/src-hilite-lesspipe.sh %s"
export LESS=' -R '
This way, when you use less to browse a file, if it is a source file handled by source-highlight, it will be automatically highlighted.
Xavier-Emmanuel Vincent recently provided an alternative version of ANSI color scheme, esc256.style: some terminals can handle 256 colors. Xavier also provided a script which checks how many colors your terminal can handle, and in case, uses the 256 variant. The script is called source-highlight-esc.sh and it will be installed together with the other binaries.
add a comment |
Use the GNU Source-highlight; you can install it with apt
if you have it, or otherwise install it from source. Then set up an "input preprocessor" for less, with help from the Source-highligh' documentations for setting up with less:
This was suggested by Konstantine Serebriany. The script src-hilite-lesspipe.sh will be installed together with source-highlight. You can use the following environment variables:
export LESSOPEN="| /path/to/src-hilite-lesspipe.sh %s"
export LESS=' -R '
This way, when you use less to browse a file, if it is a source file handled by source-highlight, it will be automatically highlighted.
Xavier-Emmanuel Vincent recently provided an alternative version of ANSI color scheme, esc256.style: some terminals can handle 256 colors. Xavier also provided a script which checks how many colors your terminal can handle, and in case, uses the 256 variant. The script is called source-highlight-esc.sh and it will be installed together with the other binaries.
add a comment |
Use the GNU Source-highlight; you can install it with apt
if you have it, or otherwise install it from source. Then set up an "input preprocessor" for less, with help from the Source-highligh' documentations for setting up with less:
This was suggested by Konstantine Serebriany. The script src-hilite-lesspipe.sh will be installed together with source-highlight. You can use the following environment variables:
export LESSOPEN="| /path/to/src-hilite-lesspipe.sh %s"
export LESS=' -R '
This way, when you use less to browse a file, if it is a source file handled by source-highlight, it will be automatically highlighted.
Xavier-Emmanuel Vincent recently provided an alternative version of ANSI color scheme, esc256.style: some terminals can handle 256 colors. Xavier also provided a script which checks how many colors your terminal can handle, and in case, uses the 256 variant. The script is called source-highlight-esc.sh and it will be installed together with the other binaries.
Use the GNU Source-highlight; you can install it with apt
if you have it, or otherwise install it from source. Then set up an "input preprocessor" for less, with help from the Source-highligh' documentations for setting up with less:
This was suggested by Konstantine Serebriany. The script src-hilite-lesspipe.sh will be installed together with source-highlight. You can use the following environment variables:
export LESSOPEN="| /path/to/src-hilite-lesspipe.sh %s"
export LESS=' -R '
This way, when you use less to browse a file, if it is a source file handled by source-highlight, it will be automatically highlighted.
Xavier-Emmanuel Vincent recently provided an alternative version of ANSI color scheme, esc256.style: some terminals can handle 256 colors. Xavier also provided a script which checks how many colors your terminal can handle, and in case, uses the 256 variant. The script is called source-highlight-esc.sh and it will be installed together with the other binaries.
answered May 25 '14 at 19:52
arsaKasraarsaKasra
158110
158110
add a comment |
add a comment |
To expand upon another answer, you can make it work for most if not all of your scripts that don't have extensions by changing the .lessfilter file around just a bit:
#!/bin/sh
case "$1" in
*.awk|*.groff|*.java|*.js|*.m4|*.php|*.pl|*.pm|*.pod|*.sh|
*.ad[asb]|*.asm|*.inc|*.[ch]|*.[ch]pp|*.[ch]xx|*.cc|*.hh|
*.lsp|*.l|*.pas|*.p|*.xml|*.xps|*.xsl|*.axp|*.ppd|*.pov|
*.diff|*.patch|*.py|*.rb|*.sql|*.ebuild|*.eclass)
pygmentize -f 256 "$1";;
.bashrc|.bash_aliases|.bash_environment)
pygmentize -f 256 -l sh "$1"
;;
*)
scriptExec=$(head -1 "$1" |grep "^#!" |awk -F" " '{print $1}')
scriptExecStatus=$?
if [ "$scriptExecStatus" -eq "0" ]; then
lexer=$(echo $scriptExec |awk -F/ '{print $NF}')
pygmentize -f 256 -l $lexer "$1"
else
exit 1
fi
esac
exit 0
You'd still need to add the two variables to .bashrc:
export LESS='-R'
export LESSOPEN='|~/.lessfilter %s'
And you'll still need to make .lessfilter executable:
$ chmod 700 ~/.lessfilter
Also I wanted to add that under debian the pygments package is called python-pygments. I had trouble locating it at first because the obvious misspelling of "pigments" as "pygments" wasn't enough of a hint to me that it was a package that might be prefixed with "python-" by the package manager.
2
2 comments: 1) Thanks for the improvement. 2) Phrases like "voted best answer" aren't great; that may change (in fact, if this is better than that answer, this post might become the top answer, at which point it'll just be confusing. Maybe just say "to expand upon another answer" or "captaincomic's answer"?
– cpast
Feb 27 '13 at 23:07
add a comment |
To expand upon another answer, you can make it work for most if not all of your scripts that don't have extensions by changing the .lessfilter file around just a bit:
#!/bin/sh
case "$1" in
*.awk|*.groff|*.java|*.js|*.m4|*.php|*.pl|*.pm|*.pod|*.sh|
*.ad[asb]|*.asm|*.inc|*.[ch]|*.[ch]pp|*.[ch]xx|*.cc|*.hh|
*.lsp|*.l|*.pas|*.p|*.xml|*.xps|*.xsl|*.axp|*.ppd|*.pov|
*.diff|*.patch|*.py|*.rb|*.sql|*.ebuild|*.eclass)
pygmentize -f 256 "$1";;
.bashrc|.bash_aliases|.bash_environment)
pygmentize -f 256 -l sh "$1"
;;
*)
scriptExec=$(head -1 "$1" |grep "^#!" |awk -F" " '{print $1}')
scriptExecStatus=$?
if [ "$scriptExecStatus" -eq "0" ]; then
lexer=$(echo $scriptExec |awk -F/ '{print $NF}')
pygmentize -f 256 -l $lexer "$1"
else
exit 1
fi
esac
exit 0
You'd still need to add the two variables to .bashrc:
export LESS='-R'
export LESSOPEN='|~/.lessfilter %s'
And you'll still need to make .lessfilter executable:
$ chmod 700 ~/.lessfilter
Also I wanted to add that under debian the pygments package is called python-pygments. I had trouble locating it at first because the obvious misspelling of "pigments" as "pygments" wasn't enough of a hint to me that it was a package that might be prefixed with "python-" by the package manager.
2
2 comments: 1) Thanks for the improvement. 2) Phrases like "voted best answer" aren't great; that may change (in fact, if this is better than that answer, this post might become the top answer, at which point it'll just be confusing. Maybe just say "to expand upon another answer" or "captaincomic's answer"?
– cpast
Feb 27 '13 at 23:07
add a comment |
To expand upon another answer, you can make it work for most if not all of your scripts that don't have extensions by changing the .lessfilter file around just a bit:
#!/bin/sh
case "$1" in
*.awk|*.groff|*.java|*.js|*.m4|*.php|*.pl|*.pm|*.pod|*.sh|
*.ad[asb]|*.asm|*.inc|*.[ch]|*.[ch]pp|*.[ch]xx|*.cc|*.hh|
*.lsp|*.l|*.pas|*.p|*.xml|*.xps|*.xsl|*.axp|*.ppd|*.pov|
*.diff|*.patch|*.py|*.rb|*.sql|*.ebuild|*.eclass)
pygmentize -f 256 "$1";;
.bashrc|.bash_aliases|.bash_environment)
pygmentize -f 256 -l sh "$1"
;;
*)
scriptExec=$(head -1 "$1" |grep "^#!" |awk -F" " '{print $1}')
scriptExecStatus=$?
if [ "$scriptExecStatus" -eq "0" ]; then
lexer=$(echo $scriptExec |awk -F/ '{print $NF}')
pygmentize -f 256 -l $lexer "$1"
else
exit 1
fi
esac
exit 0
You'd still need to add the two variables to .bashrc:
export LESS='-R'
export LESSOPEN='|~/.lessfilter %s'
And you'll still need to make .lessfilter executable:
$ chmod 700 ~/.lessfilter
Also I wanted to add that under debian the pygments package is called python-pygments. I had trouble locating it at first because the obvious misspelling of "pigments" as "pygments" wasn't enough of a hint to me that it was a package that might be prefixed with "python-" by the package manager.
To expand upon another answer, you can make it work for most if not all of your scripts that don't have extensions by changing the .lessfilter file around just a bit:
#!/bin/sh
case "$1" in
*.awk|*.groff|*.java|*.js|*.m4|*.php|*.pl|*.pm|*.pod|*.sh|
*.ad[asb]|*.asm|*.inc|*.[ch]|*.[ch]pp|*.[ch]xx|*.cc|*.hh|
*.lsp|*.l|*.pas|*.p|*.xml|*.xps|*.xsl|*.axp|*.ppd|*.pov|
*.diff|*.patch|*.py|*.rb|*.sql|*.ebuild|*.eclass)
pygmentize -f 256 "$1";;
.bashrc|.bash_aliases|.bash_environment)
pygmentize -f 256 -l sh "$1"
;;
*)
scriptExec=$(head -1 "$1" |grep "^#!" |awk -F" " '{print $1}')
scriptExecStatus=$?
if [ "$scriptExecStatus" -eq "0" ]; then
lexer=$(echo $scriptExec |awk -F/ '{print $NF}')
pygmentize -f 256 -l $lexer "$1"
else
exit 1
fi
esac
exit 0
You'd still need to add the two variables to .bashrc:
export LESS='-R'
export LESSOPEN='|~/.lessfilter %s'
And you'll still need to make .lessfilter executable:
$ chmod 700 ~/.lessfilter
Also I wanted to add that under debian the pygments package is called python-pygments. I had trouble locating it at first because the obvious misspelling of "pigments" as "pygments" wasn't enough of a hint to me that it was a package that might be prefixed with "python-" by the package manager.
edited Jun 18 '14 at 14:57
answered Feb 27 '13 at 22:45
SpeeddymonSpeeddymon
1017
1017
2
2 comments: 1) Thanks for the improvement. 2) Phrases like "voted best answer" aren't great; that may change (in fact, if this is better than that answer, this post might become the top answer, at which point it'll just be confusing. Maybe just say "to expand upon another answer" or "captaincomic's answer"?
– cpast
Feb 27 '13 at 23:07
add a comment |
2
2 comments: 1) Thanks for the improvement. 2) Phrases like "voted best answer" aren't great; that may change (in fact, if this is better than that answer, this post might become the top answer, at which point it'll just be confusing. Maybe just say "to expand upon another answer" or "captaincomic's answer"?
– cpast
Feb 27 '13 at 23:07
2
2
2 comments: 1) Thanks for the improvement. 2) Phrases like "voted best answer" aren't great; that may change (in fact, if this is better than that answer, this post might become the top answer, at which point it'll just be confusing. Maybe just say "to expand upon another answer" or "captaincomic's answer"?
– cpast
Feb 27 '13 at 23:07
2 comments: 1) Thanks for the improvement. 2) Phrases like "voted best answer" aren't great; that may change (in fact, if this is better than that answer, this post might become the top answer, at which point it'll just be confusing. Maybe just say "to expand upon another answer" or "captaincomic's answer"?
– cpast
Feb 27 '13 at 23:07
add a comment |
You can consider using most
utility which is colour-friendly alternative for less
and more
.
add a comment |
You can consider using most
utility which is colour-friendly alternative for less
and more
.
add a comment |
You can consider using most
utility which is colour-friendly alternative for less
and more
.
You can consider using most
utility which is colour-friendly alternative for less
and more
.
answered Aug 22 '13 at 0:34
OnlyjobOnlyjob
29435
29435
add a comment |
add a comment |
Condensed from my full blog post about improving less experience: https://www.topbug.net/blog/2016/09/27/make-gnu-less-more-powerful/
For colorful manpages, add the following to your .bashrc
or .zshrc
:
export LESS_TERMCAP_mb=$'E[1;31m' # begin bold
export LESS_TERMCAP_md=$'E[1;36m' # begin blink
export LESS_TERMCAP_me=$'E[0m' # reset bold/blink
export LESS_TERMCAP_so=$'E[01;44;33m' # begin reverse video
export LESS_TERMCAP_se=$'E[0m' # reset reverse video
export LESS_TERMCAP_us=$'E[1;32m' # begin underline
export LESS_TERMCAP_ue=$'E[0m' # reset underline
For syntax highlighting, using an existing powerful lesspipe.sh
to handle it instead of writing your own: https://github.com/wofr06/lesspipe
add a comment |
Condensed from my full blog post about improving less experience: https://www.topbug.net/blog/2016/09/27/make-gnu-less-more-powerful/
For colorful manpages, add the following to your .bashrc
or .zshrc
:
export LESS_TERMCAP_mb=$'E[1;31m' # begin bold
export LESS_TERMCAP_md=$'E[1;36m' # begin blink
export LESS_TERMCAP_me=$'E[0m' # reset bold/blink
export LESS_TERMCAP_so=$'E[01;44;33m' # begin reverse video
export LESS_TERMCAP_se=$'E[0m' # reset reverse video
export LESS_TERMCAP_us=$'E[1;32m' # begin underline
export LESS_TERMCAP_ue=$'E[0m' # reset underline
For syntax highlighting, using an existing powerful lesspipe.sh
to handle it instead of writing your own: https://github.com/wofr06/lesspipe
add a comment |
Condensed from my full blog post about improving less experience: https://www.topbug.net/blog/2016/09/27/make-gnu-less-more-powerful/
For colorful manpages, add the following to your .bashrc
or .zshrc
:
export LESS_TERMCAP_mb=$'E[1;31m' # begin bold
export LESS_TERMCAP_md=$'E[1;36m' # begin blink
export LESS_TERMCAP_me=$'E[0m' # reset bold/blink
export LESS_TERMCAP_so=$'E[01;44;33m' # begin reverse video
export LESS_TERMCAP_se=$'E[0m' # reset reverse video
export LESS_TERMCAP_us=$'E[1;32m' # begin underline
export LESS_TERMCAP_ue=$'E[0m' # reset underline
For syntax highlighting, using an existing powerful lesspipe.sh
to handle it instead of writing your own: https://github.com/wofr06/lesspipe
Condensed from my full blog post about improving less experience: https://www.topbug.net/blog/2016/09/27/make-gnu-less-more-powerful/
For colorful manpages, add the following to your .bashrc
or .zshrc
:
export LESS_TERMCAP_mb=$'E[1;31m' # begin bold
export LESS_TERMCAP_md=$'E[1;36m' # begin blink
export LESS_TERMCAP_me=$'E[0m' # reset bold/blink
export LESS_TERMCAP_so=$'E[01;44;33m' # begin reverse video
export LESS_TERMCAP_se=$'E[0m' # reset reverse video
export LESS_TERMCAP_us=$'E[1;32m' # begin underline
export LESS_TERMCAP_ue=$'E[0m' # reset underline
For syntax highlighting, using an existing powerful lesspipe.sh
to handle it instead of writing your own: https://github.com/wofr06/lesspipe
edited Oct 14 '16 at 7:46
answered Oct 4 '16 at 2:51
xuhdevxuhdev
8491826
8491826
add a comment |
add a comment |
I found this simple elegant solution. You don't have to install anything extra as it is already there by default on most machines. As vim
is installed by default on most machines, it includes a macro to run vim
like less
Some of the options to use it are to create an alias:
alias vless='vim -u /usr/share/vim/vim74/macros/less.vim'
or create a symbolic link:
ln -s /usr/share/vim/vim74/macros/less.sh ~/bin/vless
Then you just run vless myfile.py
I got most of the information here
add a comment |
I found this simple elegant solution. You don't have to install anything extra as it is already there by default on most machines. As vim
is installed by default on most machines, it includes a macro to run vim
like less
Some of the options to use it are to create an alias:
alias vless='vim -u /usr/share/vim/vim74/macros/less.vim'
or create a symbolic link:
ln -s /usr/share/vim/vim74/macros/less.sh ~/bin/vless
Then you just run vless myfile.py
I got most of the information here
add a comment |
I found this simple elegant solution. You don't have to install anything extra as it is already there by default on most machines. As vim
is installed by default on most machines, it includes a macro to run vim
like less
Some of the options to use it are to create an alias:
alias vless='vim -u /usr/share/vim/vim74/macros/less.vim'
or create a symbolic link:
ln -s /usr/share/vim/vim74/macros/less.sh ~/bin/vless
Then you just run vless myfile.py
I got most of the information here
I found this simple elegant solution. You don't have to install anything extra as it is already there by default on most machines. As vim
is installed by default on most machines, it includes a macro to run vim
like less
Some of the options to use it are to create an alias:
alias vless='vim -u /usr/share/vim/vim74/macros/less.vim'
or create a symbolic link:
ln -s /usr/share/vim/vim74/macros/less.sh ~/bin/vless
Then you just run vless myfile.py
I got most of the information here
answered May 31 '18 at 9:59
WavesailorWavesailor
20826
20826
add a comment |
add a comment |
The most intuitive and straight forward solution for me was using pygmentize
by adding the lines below to .bashrc
export LESS='-R'
export LESSOPEN='|pygmentize -g %s'
In case you couldn't call pygmentize
, just install like
pip install pygments
ps. The pygmentize
executable binary would sit in /usr/local/bin/
or in your /home/username/.virtualenv/venvname/bin/
or somewhere.
add a comment |
The most intuitive and straight forward solution for me was using pygmentize
by adding the lines below to .bashrc
export LESS='-R'
export LESSOPEN='|pygmentize -g %s'
In case you couldn't call pygmentize
, just install like
pip install pygments
ps. The pygmentize
executable binary would sit in /usr/local/bin/
or in your /home/username/.virtualenv/venvname/bin/
or somewhere.
add a comment |
The most intuitive and straight forward solution for me was using pygmentize
by adding the lines below to .bashrc
export LESS='-R'
export LESSOPEN='|pygmentize -g %s'
In case you couldn't call pygmentize
, just install like
pip install pygments
ps. The pygmentize
executable binary would sit in /usr/local/bin/
or in your /home/username/.virtualenv/venvname/bin/
or somewhere.
The most intuitive and straight forward solution for me was using pygmentize
by adding the lines below to .bashrc
export LESS='-R'
export LESSOPEN='|pygmentize -g %s'
In case you couldn't call pygmentize
, just install like
pip install pygments
ps. The pygmentize
executable binary would sit in /usr/local/bin/
or in your /home/username/.virtualenv/venvname/bin/
or somewhere.
answered Jan 22 at 2:44
David JungDavid Jung
1
1
add a comment |
add a comment |
None of these were working out of the box for me and I figured out an easy way to make this work so I thought I would share.
Just use tmux, that allows you access and scroll through a larger history and preserves the colors perfectly.
Doesn't tmux permit multiple terminals from one screen, how does it change less's display of colours?
– Xen2050
Oct 26 '18 at 13:23
add a comment |
None of these were working out of the box for me and I figured out an easy way to make this work so I thought I would share.
Just use tmux, that allows you access and scroll through a larger history and preserves the colors perfectly.
Doesn't tmux permit multiple terminals from one screen, how does it change less's display of colours?
– Xen2050
Oct 26 '18 at 13:23
add a comment |
None of these were working out of the box for me and I figured out an easy way to make this work so I thought I would share.
Just use tmux, that allows you access and scroll through a larger history and preserves the colors perfectly.
None of these were working out of the box for me and I figured out an easy way to make this work so I thought I would share.
Just use tmux, that allows you access and scroll through a larger history and preserves the colors perfectly.
answered Jan 25 '18 at 18:35
math0nemath0ne
972
972
Doesn't tmux permit multiple terminals from one screen, how does it change less's display of colours?
– Xen2050
Oct 26 '18 at 13:23
add a comment |
Doesn't tmux permit multiple terminals from one screen, how does it change less's display of colours?
– Xen2050
Oct 26 '18 at 13:23
Doesn't tmux permit multiple terminals from one screen, how does it change less's display of colours?
– Xen2050
Oct 26 '18 at 13:23
Doesn't tmux permit multiple terminals from one screen, how does it change less's display of colours?
– Xen2050
Oct 26 '18 at 13:23
add a comment |
Thanks for contributing an answer to Super User!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f117841%2fget-colors-in-less-or-more%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
5
This seems related: superuser.com/questions/36022/less-and-grep-color - does it help?
– Jonik
Mar 9 '10 at 13:40
4
The title of this question is very misleading. Many people landing on this page expect a solution to the coloring issue you will get when piping a command with colored output to
less
: colors are lost. (The answers to that ”piping issue“ involveless -R
,unbuffer
etc.) But the actual question refers to opening a file! — The ambiguity lies primarily in the question's title, but even besides that, IMHO the question is still too broad: ”read a file“ could refer to any file (probably plain text). (well, ”get the content in colors“ is probably referring to syntax highlighting.)– myrdd
Dec 3 '18 at 23:05
I need to correct myself, only 3 (or 4) of 14 answers are missing the OP's actual question: the answers by ChristopheD, Puneet and Onlyjob; and maybe jbbr. Still, two of those answers are part of the three highest-voted ones.
– myrdd
Dec 3 '18 at 23:38