{"id":526,"date":"2009-10-16T00:53:09","date_gmt":"2009-10-16T04:53:09","guid":{"rendered":"http:\/\/scott.sherrillmix.com\/blog\/?p=526"},"modified":"2020-01-29T09:51:38","modified_gmt":"2020-01-29T13:51:38","slug":"syntax-highlighting-in-terminal","status":"publish","type":"post","link":"http:\/\/scott.sherrillmix.com\/blog\/programmer\/syntax-highlighting-in-terminal\/","title":{"rendered":"Syntax highlighting in terminal"},"content":{"rendered":"\n\nI was just quickly flipping through code on a terminal and got to thinking that it would be pretty handy to be able to syntax highlight when using head<\/code> or grep<\/code> without having to open an editor. Luckily, I remembered Pygmentize<\/a> was pretty handy when syntax highlighting in LaTeX<\/a> so I wondered if they had an equivalent output for terminal. It turns out they do (in standard or 256 color no less) so in a couple minutes I had a really short script for highlighting code at the terminal saved it to bin\/ccat<\/code> and was ready to go. Pygmentize really is impressive. Here’s the script if anyone else is looking to do the same:\n\n[bash]\n#!\/bin\/bash\nif [ ! -t 0 ];then\nfile=\/dev\/stdin\nelif [ -f $1 ];then\nfile=$1\nelse\necho “Usage: $0 code.c”\necho “or e.g. head code.c|$0”\nexit 1\nfi\npygmentize -f terminal -g $file\n[\/bash]\n\nIt just looks for stdin and if it doesn’t find it it looks for a file name or takes direct input. Obviously it requires Pygmentize (which is really easy to install<\/a> if you already have Python).","protected":false},"excerpt":{"rendered":"I was just quickly flipping through code on a terminal and got to thinking that it would be pretty handy to be able to syntax highlight when using head or grep without having to open an editor. Luckily, I remembered Pygmentize was pretty handy when syntax highlighting in LaTeX so I wondered if they had […]","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,5],"tags":[706,508,185,509,476,507],"_links":{"self":[{"href":"http:\/\/scott.sherrillmix.com\/blog\/wp-json\/wp\/v2\/posts\/526"}],"collection":[{"href":"http:\/\/scott.sherrillmix.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/scott.sherrillmix.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/scott.sherrillmix.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/scott.sherrillmix.com\/blog\/wp-json\/wp\/v2\/comments?post=526"}],"version-history":[{"count":13,"href":"http:\/\/scott.sherrillmix.com\/blog\/wp-json\/wp\/v2\/posts\/526\/revisions"}],"predecessor-version":[{"id":619,"href":"http:\/\/scott.sherrillmix.com\/blog\/wp-json\/wp\/v2\/posts\/526\/revisions\/619"}],"wp:attachment":[{"href":"http:\/\/scott.sherrillmix.com\/blog\/wp-json\/wp\/v2\/media?parent=526"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/scott.sherrillmix.com\/blog\/wp-json\/wp\/v2\/categories?post=526"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/scott.sherrillmix.com\/blog\/wp-json\/wp\/v2\/tags?post=526"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}