Hacker News new | past | comments | ask | show | jobs | submit login
Ccat – syntax highlighting cat (github.com/jingweno)
127 points by jingweno on May 6, 2015 | hide | past | favorite | 45 comments



There's also the "python-pygments" package which provides awesome syntax highlighting. You can alias it to ccat too:

    alias ccat='pygmentize -g'


Brilliant!

While I was reading the ccat page I was wishing it was a colorized less, rather than a colorized cat.

Then I thought to read the man page for less, because sometimes what you want is already there. And it does handle color, but only if they're escaped colors. Oh well.

Then I saw your comment, tried pygmentize on a python script, and there's some nice colored code, that scrolled off the terminal.

Then I piped that into less, just to be grumpy. Yep, it's all white ... but wait, what are all those escape characters?

...

pygmentize -g myScript.py |less -r

The colors! The colors! And in less, no less.


Just a quick tip, you would want to use `less -R` (upper case R) instead. `less -r` allows all raw characters which makes it difficult for less to maintain screen appearance. From the man page:

    -R or --RAW-CONTROL-CHARS
        Like  -r,  but only ANSI "color" escape sequences are output in "raw" 
        form.  Unlike -r, the screen appearance is maintained correctly in most
        cases.


You might also want to take a look at less's input preprocessor, which could be used to automatically colorize source.


Install src-hilite and use LESSOPEN:

LESS=' -R '

LESSOPEN='| /usr/local/bin/src-hilite-lesspipe.sh %s'


You can use vimpager instead of less to get highlighting if you want.


I usta do that, but there were occasional quirks. But then, I have occasional quirks and I'm still allowed out of the house.


Especially if you need something more than "JavaScript, Java, Ruby, Python, Go, and C" - pygments has a list of supported languages a tiny bit longer than that: http://pygments.org/languages/

True, it takes some time to highlight some files - for example 5k loc long Racket files or very long XML/HTML files - but it supports all the languages I use (or used or may use in the future), while this tool supports only one of the languages I use on a daily basis.


I do the same. It's funny to see a program with the same name as my alias doing the same thing.


I have a small wrapper around `pygmentize -g` in my dotfiles that optionally overwrites the lexer based on the mime-type, as determined by `file`:

https://github.com/gvalkov/dotfiles/blob/master/bin/cats


I got a tad further and detect if the file is markdown, if so i will display in lynx https://github.com/meandavejustice/dotfiles/blob/master/bin/...


Yes, brilliant!

I read http://pygments.org/docs/styles and other sites but I didn't find how I can change the default colors. Do I have to edit (clone) pygments or is there a config file?


Pretty sure there's no config file, as of now.

For what it's worth, here's[1] the file which describes the colors used in the source. List of colors[2].

[1] https://bitbucket.org/birkenfeld/pygments-main/src/a287342ee...

[2] https://bitbucket.org/birkenfeld/pygments-main/src/a287342ee...


Thanks!


This is pretty cool, and nice demo gif. Thanks for making it!

Honestly, the biggest advantage for me over all other proposed alternatives is that it's a pure Go binary, so I can just do this:

    go get -u github.com/jingweno/ccat
On any of my machines. One predictable command will fetch the source, all dependencies and build it without any configuration.

I use Go as my primary development language, so I'm more likely to have it installed than any other tools that are not go-gettable.


Not to diminish OP's project, but I pipe most things to vim to get all my standard highlighting goodness:

    cat file_name | vi -


I do something similar except with "view" aka "vi -R" aka read-only mode.

A couple things I like about "view":

1. No warning screen when using "view file.c" if you or someone else happens to have the same file open for editing.

2. No "unsaved changes" prompt when quitting "view -" (or in other words, no need to force-quit with :q!)

Edit: Also worth noting is that "view file.c" vs "cat file.c |view -" gives vim a better chance of getting the syntax highlighting right since it knows the file extension.


to avoid the "useless use of cat", I find this approach most welcome at the end of a long pipe, i.e.:

    grep ... files | sed ... | awk ... | vim -


Mandatory mention: Emacs works here as well, but is less practical because of longer startup times.


alias ccat='supercat' (http://supercat.nosredna.net/)

My dad and I wrote virtually this same tool 8 or 10 years ago. The program uses arbitrary regular expressions to match and colorize any file you desire to write a set of rules for. And it ships with some common rules too. This was before pygmentize was very popular (or around at all).

Those who don't google search are doomed to reinvent..


> Those who don't google search are doomed to reinvent..

Not to be critical - but you are also forcing people to reinvent by having a custom-rule parser, rather than wrapping an existing lexer library.


> Those who don't google search are doomed to reinvent..

the duckduckgo team agrees ;)


I've been using source-highlight[1] to achieve the same result.

[edit]: alias ccat="source-highlight --out-format=esc --style-file=/usr/share/source-highlight/esc.style --failsafe -i "

I'll have to see how this compares.

[1]: http://www.gnu.org/software/src-highlite/


Why not use `less` instead of `cat`? Besides it is very easy to use `pygmentize` and `source-highlight` to get syntax highlighting result.


Seeing as it also reads multiple files and/or standard input, I am reminded of this:

http://harmful.cat-v.org/cat-v/

"cat isn't for printing files with line numbers, it isn't for compressing multiple blank lines, it's not for looking at non-printing ASCII characters, it's for concatenating files."

Why not make it just a syntax highlighting filter? Or is the "cat" functionality too trivial to be factored out, and thus it has grown to include other things? I suppose it's a bit of a philosophical question...


Maybe it's like grep. `grep asterisk` is different from `cat asterisk |grep`, in that it knows which file it's in and which line number of that file the match is on.


I thought the same thing. There is literally no reason to reimplement cat in a tool for highlighting source code.


Another solution for machines with vim installed:

  vim -u /usr/share/vim/vim73/macros/less.vim <file>
less.vim makes vim behave like less with syntax highlighting.


only two days back i added this alias:

alias vless=/usr/share/vim/vim73/macros/less.sh

after finding it on a search for a colorized pager


I'd just hoped someone would make this last week! Only I was hoping it would be named catfancier:

https://twitter.com/tomweingarten/status/592751132788838401


Do people cat sourcecode to read it? For anything larger than a five line shell script, I'd assume one would open it in vim or emacs.


My synapses, my circumstances and random() sometimes lead me to use any of cat, less, vim, view, grep and probably others that don't come to mind at the moment.


I use the highlight package[1] that comes with Debian/Ubuntu. With that:

    highlight -O ansi file.ext | less -R
    highlight -O xterm256 file.ext | less -R
highlight can also convert the input to other formats: HTML, XHTML, RTF, LaTeX, TeX, BBCode or SVG.

An alternative to `less -R` is the `most` pager [2].

[1] https://packages.debian.org/jessie/highlight [2] https://packages.debian.org/jessie/most


This is unfortunate, there is already a "ccat" command short for "ccrypt cat" installable through "ccrypt" on debian based systems.


Huge missed opportunity to name this thing `scat`, and claim the poop emoji as a logo.


I've always used vimcat. It's pretty damn slow, but I don't mind.


Same here! I never had issues with it being slow, however. Maybe I just haven't tried to vimcat a file that was large enough to matter.


Anything over like 20 lines for me takes a second or so. Then again the machine is like 175 miles away and its a core 2 duo... so I suppose that's reasonable.


I'm using vimcat for this. Available as part of vimpager https://github.com/rkitover/vimpager


Big fan of hicat myself: https://github.com/rstacruz/hicat.


What i usually have done for years is this :

sudo apt-get install python-pygments; alias ccat='pygmentize'

then :

ccat program.js

ccat loader.rb #etc...



A syntax highlighting pager might be more useful to me. cat is a tool that I only use for plumbing, and I rarely want coloring control codes anywhere but stdout.


I'm so glad my brain does the syntax highlighting for me.


Some people also say they are glad/happy to code in Notepad.exe

Brains do a lot of things, but there is a limit somewhere or at the very least a trade off of speed. Simple things like syntax highlighting can help you find what you are looking for faster.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: