Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

when I read a file in Linux with the command 'less' or 'more', how can I get the content in colors?

share|improve this question

migrated from stackoverflow.com Mar 9 '10 at 10:50

This question came from our site for professional and enthusiast programmers.

2  
This seems related: superuser.com/questions/36022/less-and-grep-color - does it help? –  Jonik Mar 9 '10 at 13:40

10 Answers 10

up vote 32 down vote accepted

You can utilize the power of pygmentize with less - automatically! (No need to pipe by hand.)

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"
        ;;
    *)
        grep "#\!/bin/bash" "$1" > /dev/null
        if [ "$?" -eq "0" ]; 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'

Tested on debian.

The idea comes from here Using Pygments with less.

share|improve this answer
1  
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 Byelozyorov 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
    
Any idea how to chain this with Vladimir Linek's lessopen.sh?? it is setup with this form: LESSOPEN="lessopen.sh %s" –  Gregory Dec 23 '13 at 5:12
    
@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 at 16:17

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. (...)

share|improve this answer
    
it does not work there is some conf file for "less" like for vim there is .vimrc ? –  flow Mar 9 '10 at 10:59
5  
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
4  
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;1mtest\x1b[m' | less -r –  mic_e Sep 24 '13 at 22:53
1  
This answer does not excel in the actually does something test. –  Michael Wolf May 9 at 22:24

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 implies grep --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.

share|improve this answer
1  
Nice, thanks. Except that I need to use -R as an option to less, as well. –  naught101 May 8 '12 at 6:41
4  
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). –  Josh P Oct 22 '13 at 17:17

Use VIM as a syntax highlighting pager.

share|improve this answer
    
no i asked for more (or less), not for vim thanks anyway –  flow Mar 9 '10 at 13:30
1  
Obviously syntax highlighting isn't built into more or less by default, so the workaround is using an alias to one of VIM's macros. It behaves more or less like more or less. :) –  churnd Apr 23 '10 at 14:54

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.

share|improve this answer

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'
share|improve this answer
    
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 at 18:27

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).

share|improve this answer
    
How about the performance of big files? Vim syntax highlighting is know to be slow on huge files. –  pihentagy Feb 20 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 at 11:50

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.

share|improve this answer
1  
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
    
Thank you for the tip. Duly changed. –  Speeddymon Feb 28 '13 at 20:10

You can consider using most utility which is colour-friendly alternative for less and more.

share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.