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

link|improve this question

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

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

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

5 Answers

up vote 3 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.

link|improve this answer
feedback

Use VIM as a syntax highlighting pager.

link|improve this answer
no i asked for more (or less), not for vim thanks anyway – flow Mar 9 '10 at 13:30
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
feedback

I got the answer in another post: Less and Grep - Color

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 -R "search string" * | less

Will always enable coloring and override the automatic detection, and you will get the color highlighting in less.

link|improve this answer
Nice, thanks. Except that I need to use -R as an option to less, as well. – naught101 May 8 at 6:41
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 at 13:56
feedback

Try the following:

less -R

link|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
This is useful when the file itself contains the escape codes that will need to be displayed. – Nitrodist Dec 16 '11 at 21:16
feedback

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.

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

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