Friday, November 06, 2009

Terminal Colors in PuTTY, Shell Colors, Syntax Highlighting in Emacs

Getting colors to show up in my PuTTY terminal window while navigating the file system in bash and editing a JavaScript file via emacs — that’s all I wanted to do. It took a bit of web research to find a solution, so I’ve decided to compile what I’ve found and post it here.

Enabling Colors in PuTTY

First, you need to make sure your PuTTY sessions are configured to display colors.

As indicated in the PuTTY documentation, you should start your PuTTY client and then under the Window > Colours settings category, enable Allow terminal to use xterm 256-colour mode.

Enabling Command-Line Shell Colors

Enabling shell colors essentially involve two components: (a) coloring the output of an ls command, and (b) coloring the shell prompt.

The easiest way to color the output of ls commmand is, as this forum thread points out, to put the following lines in your .profile or .bashrc file (or in the system-wide /etc/profile file).


For now, how to color the shell prompt is not covered here.

Enabling Syntax Highlighting in emacs

To get emacs to attempt to perform syntax highlighting, you must set the font-lock-mode of the buffer. The following explanation is an excerpt from the emacs manual:

font-lock-mode is the standard way to have Emacs perform syntax highlighting in the current buffer.

To automatically invoke font-lock-mode for all major modes, you can turn on global-font-lock-mode by including the following line in your `.emacs' file:

(global-font-lock-mode 1)

To test whether emacs now displays colors, start emacs and then run M-x list-colors-display — i.e., press Alt-x, type list-colors-display, and then hit Enter. You should see a list of all the colors emacs knows how to display.

Recognizing JavaScript Syntax in emacs

Although you’ve now enabled syntax highlighting emacs, it may not know which files contain JavaScript or what the language syntax is. In order for emacs to recognize JavaScript, you need to load an emacs mode file. After some trial-and-error with other JavaScript mode files, I found the javascript.el mode file, which was recommended by Edward O’Connor.

Download the file, and place it in your emacs load path — e.g., under /usr/local/share/emacs/site-lisp on FreeBSD). Then add the following lines to your .emacs file:

(autoload 'javascript-mode "javascript" nil t)
(add-to-list 'auto-mode-alist '("\\.js\\'" . javascript-mode))

If you’re like me and prefer indentation as a tab rather than four character spaces, you may want to edit the javascript.el file by modifying the javascript-indent-level line so that the level value is 8 instead of 4:

(defcustom javascript-indent-level 8
    "Number of spaces for each indentation step."
    :type 'integer
    :group 'javascript)


That’s it. Hope this was useful to some.

No comments:

Post a Comment