Wednesday, October 24, 2007

Pretty printing text using Emacs

Most editors will give you syntax colouring for languages which they support, typically Java, C++, and C and a gaggle of scripting languages. And a whole bunch of user supplied syntax files in each editor specific language file.


Emacs too provides such a feature but with a different twist where the things can be coloured on the fly. More importantly, any random buffer can be made to highlight any keyword based on regular expressions.

While working on Autosys scripts which had close to 100 odd steps it looked like the first screenshot.

To make it more readable, here's what I did

1. Colour the comment green
2. Colour the command shell script blue
3. Colour the Box name blue
4. Colour the condition as red

a. Start off by invoking M-x hi-lock-mode
b. hit C-x w l /\* RET hi-green-b and you get the second screenshot


c. hit C-x w l command: RET hi-blue-b
d. hit C-x w l box_name: RET hi-blue-b
e. hit C-x w l condition: RET hi-red-b

After doing the above, you should get something akin to the final screenshot. Now, that is something which is easier on the eyes (OK, bad colour choices) than the plain vanilla text file. In fact, nothing happens to the actual text file, only the emacs buffer containing the file is fontified.
In the Info manual, please see Emacs->Highlight Interactively to see other keybindings and colouring or font locking options.