Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Utilizing emacs' or vim's syntax highlighter for command-line program?

I have a command-line program that spews JSON and YAML. By default it detects if pygments (pygmentize) is available and if it does, pass the output throught it to get a nice colorized output. However, pygments is not installed by default on most computers that this program will run on. But most computers will have either emacs or vim, however, does. Is there a way to get one of these editors to syntax-highlight some text using ANSI escape sequences and then output it again?

like image 331
Steven Haryanto Avatar asked Nov 15 '12 23:11

Steven Haryanto


People also ask

How do I get syntax highlighting in emacs?

5.43 How do I turn on syntax highlighting? font-lock-mode is the standard way to have Emacs perform syntax highlighting in the current buffer. It is enabled by default. With font-lock-mode turned on, different types of text will appear in different colors.

How do you highlight syntax?

Syntax highlighting is a feature of text editors that are used for programming, scripting, or markup languages, such as HTML. The feature displays text, especially source code, in different colours and fonts according to the category of terms.

What do you mean by syntax highlighter?

Syntax highlighting determines the color and style of source code displayed in the Visual Studio Code editor. It is responsible for colorizing keywords like if or for in JavaScript differently than strings and comments and variable names.

How do I highlight a command in bash?

There is no simple way to obtain syntax highlighting in GNU Bash (or GNU Readline), but it is in principle possible to implement your own line editor in Bash script by binding all the user inputs to shell functions using the builtin command bind -x 'BYTE: SHELL-COMMAND' .


4 Answers

as the editor can already do the ansi stuff, rather easy to make a screen capture of the editor, no?

script -qc "stty rows 10000
emacs -nw -visit YOURFILE.YAML -eval '(redisplay t)' -f kill-emacs
resize"

(redisplay is only needed for GNU FSF Emacs)

now clean up the capture

perl -p0E 's/\A(?s:.*)\e\[27m\r\n
\e\[A\n((?s).*?)
(?:\e\[K\n)+
\e.*\e\[27m\r$(?s:.*)\Z/$1/mx' < typescript

if you don't want the recording process visible on screen, you can wrap it in a hidden terminal with something like perl's IO::Pty

like image 92
Nei Avatar answered Oct 23 '22 05:10

Nei


Matthew Wozniski wrote a script called vimcat.sh that does this with Vim. His version is at https://github.com/godlygeek/vim-files/blob/master/macros/vimcat.sh. I've made a few modifications to it (if memory serves, the modifications allowed it to run on my Mac OS X system; I know the substitution of /dev/fd/9 for /proc/self/fd/9 had that purpose); see my gist at https://gist.github.com/4090959.

I believe both versions of the script have trouble with returning to default background color if Vim's highlighting changes the background.

like image 28
echristopherson Avatar answered Oct 23 '22 07:10

echristopherson


Like Emacs (cp. ataylor's answer), Vim can render a buffer with full syntax highlighting to HTML; see :help 2html.vim. You could probably re-use much of the plugin's code that goes through the buffer's syntax, and change it to render to ANSI escape sequences, but you'd have to re-implement all the rendering logic by yourself.

Though there are some plugins that employ Vim as a pager, I don't think it is possible to just use Vim to output the buffer with ANSI escape sequences. After all, Vim wants to retain control of the terminal, and clears it when exiting.

I'd suggest to look for another, dedicated solution outside Vim, although that means you need to install it.

like image 2
Ingo Karkat Avatar answered Oct 23 '22 05:10

Ingo Karkat


Emacs includes a function called htmlfontify that will convert a fontified buffer to HTML. You can use this in batch mode with a small elisp script to render a file as HTML. For example:

emacs -q --batch --file myfile.rb --eval '(progn (require (quote htmlfontify)) (htmlfontify-buffer) (set-buffer-modified-p t) (save-buffer))'
like image 1
ataylor Avatar answered Oct 23 '22 07:10

ataylor