Syntax highlighting with pygments in the shell

First install Pygments:

$ apt-get install python-pygments

And create this function in your .zshrc or .bashrc:

colorize_via_pygmentize () {
    if [ ! -x $(which pygmentize) ]
    then
        echo package \'pygmentize\' is not installed!
        exit -1
    fi
    if [ $# -eq 0 ]
    then
        pygmentize -g $@
    fi
    for FNAME in $@
    do
        filename=$(basename "$FNAME") 
        lexer=`pygmentize -N \"$filename\"` 
        if [ "Z$lexer" != "Ztext" ]
        then
            pygmentize -l $lexer "$FNAME"
        else
            pygmentize -g "$FNAME"
        fi
    done
}

If you want a colorize display for less, you can add this function:

cless () {
    colorize_via_pygmentize $1 | less
}

Now, you should have syntax highlighting if you use:

$ cless yourfilename
If you want to ask me a question or leave me a message add @bougui505 in your comment.