It’s no secret that I use emacs, and that I’m a huge fan. Some time ago I started using the most recent stable release, emacs 23, on my Windows PC at work. Unfortunately Ubuntu, which I run at home, still provides emacs 22 in its package system. While emacs 22 is perfectly fine to get the job done, the mismatch does cause some small annoyances due the fact that I use the same .emacs config file and supporting files (http://hg.rooijan.za.net/addons/) on all my systems. (Some months ago I claimed that I was going to write about that system within weeks of making the claim. I still will do, soon, I promise. I don’t promise to define “soon” though).
While emacs could of course be compiled from source on my Ubuntu systems, this becomes a bit of a pain to maintain across upgrades and the like. Amongst other minor issues, json.el is not provided with emacs 22, but it is with emacs 23, which caused a warning to be issued when loading my config files on emacs 22. Fortunately, the magic of Ubuntu PPA’s (on which Jerith recently wrote more eloquently than I can) comes to the rescue: behold the Ubuntu Emacs Lisp PPA, from which one can install emacs23. One can also install emacs-goodies-el, which is a good collection of nifty emacs extras.
Beautiful things, PPA’s…
I have discovered that my poky little website (www.rooijan.za.net) hosts the top link in two related Google searches: “ack emacs” and “emacs ack”, both of which direct to a page listing the details of ack.el, which Voyager and I put together some time ago.
I’m just saddened that I didn’t put Google Ad Words or something like that onto it – I could have paid for most of a day of lunch at work (which is all of R8.50…)
More seriously, I didn’t have comments enabled, which I’ve now turned on – I wonder if I missed any kind of interesting discussion which may have ensued.
I haven’t written anything in a while, and this isn’t a long piece of work either. I’ve been very busy at work, but I figured I’d share a few emacs tricks I discovered while being busy that helped make me a little less busy. I have hyperlinked various emacs functions for further information:
A very useful command that reduces the whitespace around point to a single space, or no space at all if that makes more sense contextually. I edit a lot of Delphi code, with emacs set to use spaces instead of tabs and provide pretty indenting. While this works beautifully, it does have the drawback that I’m left with heaps of whitespace if I turn a multi-line statement into a single-line one, which auto-indenting obviously can’t fix. fixup-whitespace does the trick with a single keypress – wish I’d thought to look for something like it long ago.
Return a count for the number of lines matching a regex. Simple, but useful for analysing logs, and something I was settling down to write in Python before I checked to see if such a thing existed.
Very useful for taking structured notes which can then be output in a variety of formats if desired. Can do way more than just note-taking – many people pretty much use it to organise their lives.
emacs can be run in batch mode, by passing the –batch switch. In batch mode emacs does not expect to have a display device to work with, making it ideal for using in batch files or scripts. Passing an argument after the –batch switch will open that argument in a buffer and subsequent actions will be taken on that buffer. The buffer will of course not be visible, but it remains a fully functional emacs buffer.
Batch mode therefore allows you to use emacs to do tasks it is very good at, without needing to interact directly with emacs – great for passing some heavy lifting to emacs in various scripts.
There are a few useful switches to use in conjunction with –batch:
- -f ARG: Run elisp function ARG on the buffer
- –eval ARG: Evaluate elisp snippet ARG
For example, the following line in a Windows batch file will use emacs to properly indent a Delphi source file:
emacs --batch %1 -f delphi-mode --eval "(indent-region (point-min) (point-max) nil)" -f save-buffer
*(if emacs is your editor).
It’s fairly late at night, and I’m doing some desperately needed work coding. I’ve been using SVN to track my progress, as I do at work, and I’ve found myself using PSVN more and more. I’ve long had it installed, but never taken full advantage of it. It provides a central point within emacs to do just about anything you could want to do with an SVN-controlled directory, file or repo (or make new ones). It also hooks into ediff and various other bits of emacs magic. Rather than rehash things, here’s a link to an intro to it I found.
If you use emacs and SVN together even rarely, this is well worth a look.
I’ve been using Emacs for some time now, and I’ve grown to really appreciate it. One of the nifty features introduced in Emacs 22 is the ability to execute arbitrary elisp when replacing text using regular expressions (elisp is the Lisp dialect in which Emacs itself is written and through which it can be extended). I had reason today to use the feature for a silly little problem, and I was impressed, so here’s why.
I had a set of data where I had to replace a placeholder on each line with an integer, which incremented every four lines, starting at 2. i.e:
x line 1
x line 2
x line 3
x line 4
x line 5
x line 6
2 line 1
2 line 2
2 line 3
2 line 4
3 line 5
3 line 6
This would be quite tricky to solve with a normal regexp replace operation, but under Emacs I simply defined a new function to do the hard work:
(setq mod_id 1)
(defun four_inc (y)
(if (eq 0 (% y 4)) (setq mod_id (1+ mod_id)) mod_id)
I evaluated those two pieces of elisp in place in the scratchpad I was working in (another massively nifty Emacs capacity) and then ran a regular expression replacement, replacing x with \,(four_inc \#)). The \, tells Emacs to insert the result of calling the four_inc function into the replacement text, and \# is the number of replacements already done.
This is hardly earth-shattering stuff, but it’s the ability both to extend Emacs however one desires (by writing an increment-every-4-times function in this case) and the heaps of nifty features other people have already provided (executing arbitrary elisp code in a regexp replacement for example) that explains to a large degree why I like Emacs so much.