2009-05-30

If you're like me, you'll usually have a lot of buffers open in emacs. To
quickly switch between them, you press C-x b (Ctrl-X and b), and type
the beginning of buffer name. This is made much more convenient with
e.g. ido (see Switching Buffers) or icicles. They allow you switch to a
buffer by typing just some characters appearing anywhere in the buffer name,
autocompletion etc.

The question for today is: what to do if am not searching for a buffer or
file, but for some emacs command?

Often, I search for a certain command, ie. the ones that come after
M-x. After typing M-x there's autocompletion if you know the beginning
of the command… but maybe I remember only it was something with string,
but exactly how? Was it replace-string or string-replace?

For these deep problems, there is smex (think: search-M-x). smex brings
ido-style completion to choosing commands.

As you can see, I have set the keybinding to M-X, that is (for most people)
Alt-Shift-x; of course you could override the normal M-x
('execute-extended-command', note the small x), but usually the normal
completion is fine.

Admittedly, smex serves a niche use-case, but I still use it a couple of
times a day.

[ Note that packages like icicles and anything can do similar things; they
do much more as well, and I haven't had the chance to play with those in
detail, so for the time being I stay with smex. ]

2009-05-26

It's sometimes nice to see the changes you've made to a file. If the file is
under version control, you can use the 'diff'-features of the version
control system of course; or you can use diff-buffer-with-file to compare
your buffer with the version on disk. That obviously only. works when you
haven't saved the file yet.

Anyway, a bit easier, straighforward way may be to use
highlight-changes-mode. With that mode, emacs can give a special color to
parts of the text that you have changed.

2009-05-21

Already in some previous entry I sang the praises about org-mode, the
emacs-mode that is such a nice, flexible way to organize your life. There is
so much in org-mode that it's quite hard to fit in here, but thankfully
org-mode is documented very well; there's not only the reference manual, there
is also a lot of user-provided documentation about how they use org-mode. A
nice recent example of that is Bernt Hansen's Org Mode - Organize Your Life In Plain Text!.

I use org-mode for time-management too - I'm using it in a rather simple
fashion compared to Bernt Hansen, but find it very useful. I also use
org-mode for writing webpages and blogs (like this one), and that is what
I'd like to discuss here.

Actually the first time I heard about this org-mode-thing people were raving
about, it was for taking notes. Curious as I am, the next time I needed to
take notes, I uttered M-x org-mode to try it out, and found how easy and
natural it is to write structured, semi-rich-text with org-mode. Some
examples:

Markup

I can write headings by starting lines with some stars, the number of stars
determining the heading level:

You can even insert formulae in the cells, turning this into a simple kind of
spreadsheet. Haven't used that yet though.

Blogging

Now, one of the things I use org-mode for is blogging; for that, I need to
convert the org-text into HTML; this is simple org-export-as-html (or C-c C-e). I can then copy the HTML into blogger.com or whatever (that might be
automatable). You can also export to plain-text, LaTex and other formats.

I found that it makes me a much more productive blogger if can use org-mode;
it's so much more convenient to write the mark-up than to write raw HTML. Now,
sometimes I might want still want to write some raw HTML, but that can be
easily done:

However, I only found out about the nicest trick very recently from the
org-mailing list.

In this blog, I often use blurbs of code; I'd like to show those blurbs with
the syntax-highlighting that emacs gives me. For that, I use the
htmlize-package. For example, when showing some Emacs-Lisp code, I would
copy that to an Emacs-Lisp buffer, then run htmlize-region on the code, and
finally copy the result back in a raw-html block (like the one for the <button>).

I wondered - could I not do that automatically? I could mark code in
org-mode as being 'Emacs-Lisp'-code (or Perl, or Python, or …), and when
I'd export the html, org-mode would go through the trouble of calling
htmlize-region on it and use that in its output. Sounded like a nice idea, I
asked for some advise on how to do it.

Five minutes after asking, I got a reply – 'just use
#+BEGIN_SRC/#+END_SRC'. Wow – it was already there in org-mode, it's
even documented, but somehow I missed that. So, now I can write:

2009-05-18

This whole entry could be summarized as 'use M-x re-builder' to build your
regular expressions. But let's see if I can stretch that wisdom over a couple
of lines…

For searching and replacing, regular expressions ('regexps') are a very useful
tool. For example, see the entry about getting your ip-number. I am not
going to explain regexps here – there are plenty of good references about
them. Of course, emacs supports regexps - but it's not always so easy,
compaired to e.g. Perl. I am only providing some trivial examples here, please
see Steve Yegge's post on the regexp tricks possible with then-new Emacs 22 (I
can't remember ever needing that kind of regexp-pr0n in real life though…)

Back to regexps - on of the issues with regexps in Elisp is that they need
extra quoting, that is, lots of \-escape characters; regexps can be hard to
comprehend, and this does not help… Why the extra quoting? Let's look at a
simple example. Suppose we want to search for the word cat. And not
category or concatenate. The regular expression would then be \bcat\b.

In Perl you could write this as /\bcat\b\/ (in Perl you specify regexps by
putting them between /-characters).

Not so in Emacs-Lisp. On the Lisp-level, there are no regexps; there are only
strings and only the regexp functions understand their true nature. But
before the strings ever get those functions, the Lisp interpreter does what
it does best: interpreting. And when it sees \b, it interprets it as the
backspace-character.

To make it not do that, you'll need to pay the 'slash-tax' and write
something like:

(re-search-forward "\\bcat\\b")

Things can go ugly quickly from there - think of when you need search for
something with a backslash, like our regex \bcat\b itself; you'd need to do:

(re-search-forward "\\\\bcat\\\\b")

slash tax break

To make things even more interesting, in different contexts, different rules
apply. The above is all about regexps in strings in Emacs-Lisp. However,
things are different when you provide a string interactively.

Suppose you search through your buffer (with M-x isearch-forward-regexp or
C-M-s). Now, your input is not interpreted by the Lisp interpreter (after
all, it's just user input). So, you're exempt from the slash tax, and you can
use \bcat\b to match, well, \bcat\b.

re-builder

So, regexps can be hard, and Emacs-Lisp makes it somewhat harder. A natural
way to come up with the regular expression you need, is to use
trial-and-error, and this is exactly what isearch-forward-regexp and
friends do. But what about the slash-taxed regexps that you need in your Lisp
code?

The answer is M-x re-builder. I am sure many people are already using it,
but even if there were only one person that finds out about this through
this blog-post, it'd be worth it! And this is the whole trick here: whenever
you need a regexp in your code, put the kind of string it should match in
a buffer, and enter M-x re-builder.

re-builder will put some quotes in the minibuffer. You type your regexp
there, and it will show the matches in the buffer as you type. It even supports different
regex-syntaxes. By default, re-builder will help you with the
strings-in-Emacs-lisp kind of regexps; this is called the read-syntax. But you
can switch to the user-input regexps with C-c TAB string RET (yes, these are
called string here). There are some other possible syntaxes as well.

One final trick for re-builder is the subexpression mode, that you
activate with C-c C-e (and leave with q). You can than see what
subexpressions match (ie. if we can match cat, cut, cot etc., with
\\bc\\(.\\)t\\b, and the subexpression would then contain the middle
letter. re-builder automatically converts between the syntaxes it supports,
so you could use 'string-mode' as well, bc\(.\)t\b.

2009-05-15

When you delete files and directories in Emacs 23 (say, with dired), instead
of losing the files until the end of times (or at least until the
singularity), you can move them to the 'trashcan', by whatever name that rose
comes in your system, 'Trash' or 'Recycle Bin'…

To enable this, put the following in your .emacs:

(setq delete-by-moving-to-trash t)

There is one problem - a bug? I am using Ubuntu 9.04, which follows the
Freedesktop Trash Spec; it moves delete file in ~/.local/share/Trash,
together with some metadata, so it can restore the file to their original
location. However, emacs follows some older convention, ie. to move the file
to ~/.Trash, and without any metadata.

You can partially fix this by making ~/.Trash a symlink to
~/.local/share/Trash/files/, but of course that does not get you the
metadata.

2009-05-12

A friend asked me how to retrieve your IP-number in emacs; he needs it to
figure out in which of various networks he lives, to update proxy settings and the like. He
had found a decade-old function get-ip-addresson the internet, and
thought (correctly!) that it can't be that hard.

So, fortunately, it wasn't too hard to do it a bit better, esp. with all the
improvements in emacs in the last ten years.

The somewhat-ugly-but-it-works solution is to use the output of the ifconfig
tool:

Now, calling (get-ip-address "eth0") or (get-ip-address "lo") in your scripts
will get you the IP-number. Obviously, this only works on systems that have
this ifconfig, and is also vulnerable to small changes in the output. Don't even ask about IPv6.

The solution does give us a nice example of using shell-command-to-string,
which is really useful function to integrate with all kinds of external tools;
the difference with decade-old get=ip-address is striking.

However, we can do even better in these modern times. More recent versions of
emacs provide nice networking functionality:

All fine - but unfortunately, this does not work on Windows; there is no such
thing as network-interface-info there, not even in the latest Emacs
incarnations. Is there nothing else we can do on Windows? Well… we can go
back to the first solution (using shell-command-to-string, and see if we can
use it with Windows' ipconfig tool. Something like:

The Windows version does not support choosing the interface; I'll leave that
as an excercise to a reader with some more Win-fu; that same reader might also
have a solution that does not involve ipconfig, but uses some Win32-API. And
of course, that user is invited to help the emacs development team to add a
Win32 version of network-interface-info.

to your .emacs will make emacs remember where you were in a file, the last time you opened ('visited') it; when you re-open it, it jumps right back to where you
were. The set-default enables saveplace (which is buffer-local) for all
buffers.

There are some more customizations possible, but they seem less useful to me.

SavePlace is the kind of nice convenience that make emacs so nice… at the
same time, it shows that so many of these small conveniences can go unnoticed
for years (saveplace has been part of Emacs since version 19.19 of 1993!).

2009-05-07

I am back from my little holiday, fully re-energized etc.; Before I went, I
asked Emacs-Fu-readers to share their favorite .emacs-tricks; and I was very
pleasantly surprise with so many excellent tricks, many of which I did not
know about. Thanks!!

So, in some future entries I´ll go through the ones I found particularly
interesting – however, look for yourself through all these nice tips. Of
course, I won't simply copy what you could already read, – I'm not that
lazy – but instead, I'll try to give a bit of extra background, and try to
make these tips even more useful.

savehist-mode

The first great new trick mentioned in the dot-emacs trickery-post was
savehist-mode (thanks Valvo).

With savehist-mode, you can save your minibuffer history (ie. the commands
you gave). It's the simple alternative to more complex session-saving
mechanisms like desktop-save-mode and (many!) friends.

save-hist-mode lets you can customize quite a few things, so my setup now
looks like this:

As shown, you should do the customizations before enabling savehist-mode,
or they will be ignored.

The savehist-additional-variables customization particularly interesting;
you can put any (printable) variable in the list, ie. you could put
kill-ring in there, to retrieve your old kill ring (the clipboard) when you
restart emacs again.

Note: if you want emacs to remember your recently-used files between sessions,
you can take a look at recentf.

Also, note that I am setting savehist-file to ~/.emacs.d/savehist. In
general, I'll try to put all my emacs files there, instead of in my
home-directory. This makes it a bit easier to back up things and keep my ~/
clean. For historical reasons, I still have my ~/.emacs, but I could use
~/.emacs.d/init.el instead (great tip by Steve Purcell).