Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Lots of programming-oriented editors will colorize source code. Is there a command that will colorize source code for viewing in the terminal?

I could open a file with emacs -nw (which opens in the terminal instead of popping up a new window), but I'm looking for something that works like less (or that works with less -R, which passes through color escape sequences in its input).

share|improve this question
up vote 51 down vote accepted

With highlight on a terminal that supports the same colour escape sequences as xterm:

highlight -O xterm256 your-file | less -R

With ruby-rouge:

rougify your-file | less -R

With python-pygments:

pygmentize your-file | less -R

With GNU source-highlight:

source-highlight -f esc256 -i your-file | less -R

You can also use vim as a pager with the help of macros/less.sh script shipped with vim (see :h less within vim for details):

On my system:

sh /usr/share/vim/vim74/macros/less.sh your-file

Or you could use any of the syntax highlighters that support HTML output and use elinks or w3m as the pager (or elinks -dump -dump-color-mode 3 | less -R) like with GNU source-highlight:

source-highlight -o STDOUT -i your-file | elinks -dump -dump-color-mode 3 | less -R
share|improve this answer
    
That's encyclopedic, thanks! I settled on pygmentize since I already have pygments installed. – alexis Mar 3 at 15:10

I use GNU source-highlight combined with less -R for this. Add the following lines to your shell's initialization file (~/.bashrc, for example):

cless(){
 LESSOPEN='| source-highlight --failsafe --out-format=esc256 -o STDOUT -i %s 2>/dev/null ' less -R "$@"
}

That should automatically color source code according to the language used and, if it can't, it will launch less on its input directly.

share|improve this answer

If you're interested in colors in less more generally, you might want to look at lesspipe.sh. See, for example, http://freecode.com/projects/lesspipe.

lesspipe.sh is an input filter for the pager less as described in less's man page. The script runs under a ksh-compatible shell (e.g. bash, zsh) and allows you to use less to view files with binary content, compressed files, archives, and files contained in archives.

It will also colorize shell scripts, perl scripts, etc. similarly to a text editor, but without the use of any "preprocessing" program to do the colorizing.

share|improve this answer
    
Thanks! I think lesspipe.sh is what I was trying to remember before I posted the question... – alexis Mar 3 at 15:08

I enjoy simplicity and I use nano text editor, in this case I usually proceed as follow:

check witch syntax highlight is available: ls -la /usr/share/nano/

include it in my nanorc file with following command, or by doing it manually

echo 'include /usr/share/nano/sh.nanorc' >> ~/.nanorc

about less I suggest to give a look at this page

share|improve this answer

It is called syntax highlighting.

GNU Source-highlight when given a source file, produces a document with syntax highlighting.

I use the lessfilter script below, or you could make less invoke it automatically like with:

export LESSOPEN="| /path/to/lessfilter %s"
export LESS=' -R '

I found this article and started doing it this way:

#!/bin/sh
file -b -L "$1" | grep -q text &&
  /usr/share/source-highlight/src-hilite-lesspipe.sh "$1"

(src-hilite-lesspipe.sh is shipped with source-highlight but not all distributions include it)

share|improve this answer
3  
"If you go to the article the author must be using a Mac since he uses capitals for his commands": I'm afraid you're a bit confused: Clearly you are not aware that export defines an environment variable, not an alias. The command less uses flags from the environment variable LESS, so your answer needs some work. – alexis Mar 3 at 13:44

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.