2

Emacs in -nw mode colors source code files, in this screenshot you see how emacs colors the .rb file I opened. Over the weeks/months I've gotten used to the coloring.

Despite how hard I try, there always comes a time when I'm not in emacs, and I'm doing a cat in the Terminal.app bash shell. So much like you can pipe a compact block of json to get nicely indented json, is it possible to pipe a .rb file to some scripts that runs Emacs and extracts the syntax coloring that Emacs uses?

enter image description here

6
  • 2
    Please clarify how this is relevant to Emacs. I guess what you're really asking is "how to use Emacs in a pipe to color my file". If so, please says it.
    – Stefan
    May 8, 2019 at 1:57
  • 3
    Yes, translate Emacs font-lock faces into ANSI escape code, see github.com/Lindydancer/e2ansi
    – xuchunyang
    May 8, 2019 at 8:04
  • You can install/use the package highlight from homebrew - see discussion on this page: unix.stackexchange.com/questions/267361/… - this package support colour customisation.
    – Ian
    May 8, 2019 at 12:47
  • I really don't understand why this question is put on hold -- I've voted for it to be reopened. Anyway, I asked myself the same thing a couple of years ago -- when I didn't find any package I wrote my own (github.com/Lindydancer/e2ansi as @ xuchunyang already posted). Any text Emacs can highlight, it can render using ANSI sequences so it can be viewed in a terminal, using the command e2ansi-cat. It integrates with pagers like moreand less so anything you view with them becomes highlighted automatically. May 9, 2019 at 19:24
  • really don't understand why this question is closed May 15, 2019 at 21:49

1 Answer 1

5

You can use the syntax coloring outside Emacs, two working examples are htmlize for the web and e2ansi for the terminal. For your need, you can use e2ansi-cat provided by e2ansi, here is a screenshot of it:

screenshot of cat and e2ansi-cat

By the way, if you use Eshell, you can easily get the syntax coloring:

(defun eshell/cat-with-syntax-highlight (filename)
  (let ((existing-buffer (get-file-buffer filename))
        (buffer (find-file-noselect filename)))
    (eshell-print
     (with-current-buffer buffer
       (font-lock-ensure)
       (buffer-string)))
    (unless existing-buffer
      (kill-buffer buffer))
    nil))

screenshot of cat-with-syntax-highlight

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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