08 December 2010

A few general emacs tips

To finish off the series on Emacs, this post outlines a few tips and tricks I have picked up over the years.

Javascript

If you are coding Javascript, the default 'js-mode' in Emacs could do with upgrading. What you really want is js2-mode. This mode checks the syntax of your Javascript as you type. I am no Javascript guru, but working with Javascript using js2-mode is like night and day than without it.

PLSQL Mode

I do a lot of Oracle PLSQL development. Once upon a time I used TOAD as my IDE. However, once you start using Emacs and get used to it, you want to use it for everything. Enter plsql-mode. This will syntax highlight plsql, and let you compile it with C-c c. Any errors are marked and displayed in a split window, and clicking on an error jumps you to the line of code causing it (standard emacs compile-mode behaviour).

Perl Mode

I found the default emacs Perl mode to be a bit clunky, but I cannot remember why. The improved cperl-mode is much better.

Keyboard Macros

These just blew my mind. You the story:

Here are a bunch of 2000 account numbers, can you query their status on the database please?

So to do this, you can write a program that opens a file, reads it a line at a time and runs an SQL query for each one.

Or

You could load all the accounts into a database table and join them to produce a report. To do this, for each account you need to take

1234567890

and turn it into

insert into tempAccounts values ('1234567890');

Enter keyboard macros. You 'record' a series of keystrokes for one line, and then have emacs repeat it for the rest of the lines. There is more information on the emacswiki. Once you know about this tool, repetitive changes to text files becomes a breeze.

Better Colors

I was never happy with the default emacs color scheme. There are quite a few good ones out there. First, install color-theme, then pick a color scheme you like. Personally, I like color-theme-tango

Sane Defaults

The number of times I have quit emacs by accident is just too many. I like it to prompt me and ask 'if I really want to quit':

; stops me killing emacs by accident!
(setq confirm-kill-emacs 'yes-or-no-p)

Once you get used to the emacs keyboard commands, you won't have much need for the menu and tool bars. They take up valuable screen space, so I get rid of them:

; turn off the tool and menu bars
(tool-bar-mode -1)
(menu-bar-mode -1)

Finally, the default splash screen that appears each time emacs loads can be a bit of a pain, so to get rid of it, simply add this to .emacs:

(setq inhibit-splash-screen t)

Fly Modes

Watch out for 'fly-make' modes. These highlight syntax errors in your code as you type it, without doing a full compile. It can be a real time saver. Ruby has a fly-make mode, so does Perl, so does Javascript (as part of js2-mode). If you are typing text, you can even have flyspell-mode, which I am using right now to type this!

blog comments powered by Disqus