finding commands with smex

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.

After installing the smex package, you can put something like the following in your .emacs:

(setq smex-save-file "~/.emacs.d/smex.save") ;; keep my ~/ clean                                      
(require 'smex)                                                  
(global-set-key (kbd "M-X") 'smex)                                             

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. ]


tracking changes

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.

;; higlight changes in documents
(global-highlight-changes-mode t)
(setq highlight-changes-visibility-initial-state nil); initially hide

The last line tells me that the changes should not be visible unless I want to see them.

I defined a key binding (F6) so I can easily toggle between visible/invisible changes:

;; toggle visibility
(global-set-key (kbd "<f6>")      'highlight-changes-visible-mode) ;; changes
;; remove the change-highlight in region
(global-set-key (kbd "S-<f6>")    'highlight-changes-remove-highlight)

With this last keybinding S-<f6> (Shift-F6), I can remove the change-indication of the current region (selection). Here are some other useful keybindings to quickly jump between various changes:

;; alt-pgup/pgdown jump to the previous/next change

;; if you're not already using it for something else...
(global-set-key (kbd "<M-prior>") 'highlight-changes-next-change)
(global-set-key (kbd "<M-next>")  'highlight-changes-previous-change)

Another interesting thing you can do is M-x highlight-compare-with-file.

The only remaining problem with highlight-changes-mode is that the default colors are, well, hideous. But of course, that can easily be fixed by changing the faces:

(set-face-foreground 'highlight-changes nil)
(set-face-background 'highlight-changes "#382f2f")
(set-face-foreground 'highlight-changes-delete nil)
(set-face-background 'highlight-changes-delete "#916868")

Or adding to your color-scheme:

(highlight-changes ((t (:foreground nil :background "#382f2f"))))
(highlight-changes-delete ((t (:foreground nil :background "#916868")))) 

Now, with these color changes, the foreground stays the same, only the background changes a bit. I am using a dark theme, you might want to change the colors to fit in with your theme.

There are some more features - for example, to rotate through changes of different age. For such things I prefer to use a version control system, but you might want to check it out.

Tracking changes can be quite useful. And, unlike some word-processing software, emacs does not hide your highly embarrassing modifications somewhere in your document…


writing and blogging with org-mode

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:


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

* level 1 heading
  some text
** level 2 heading
   some more text
*** level 3 heading
    even more

org-mode helpfully gives them some different colors; by pressing TAB when I am on a heading, I can hide/show the lower levels and the contents. Nice!

It's also easy to get all the basic markup by decorating your words a bit:

This is *bold* /italics/ _underline_ [[http:/emacs-fu.blogspot.com][Emacs-Fu]]

In org-mode it looks like:

This is *bold* /italics/ _underline_ Emacs-Fu

There are many more things; for example, there's the table editor; I simply type |country|capital|<Enter>|-<Tab> and I get:

| country | capital |
|         |         |

Now, I fill in the names in the colums, ending each line with <Alt><Enter> (or M-RET), and org-mode takes care of making everything fit, so we get:

| country     | capital   |
| finland     | helsinki  |
| netherlands | amsterdam |

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


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:

   <button onclick="alert('you are!');">I feel lucky!</button> 

in the export HTML this will become: How cool is that?!

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:

#+BEGIN_SRC perl
   for (my $i = 0; $i != 10; ++i) {
        print "hello, world!\n";
  for (int i = 0; i != 10; ++i)
        std::cout << "hello, world!" << std::endl;

and in the export HTML, this will look like:

for (my $i = 0; $i != 10; ++i) {
      print "hello, world!\n";
for (int i = 0; i != 10; ++i)
      std::cout << "hello, world!" << std::endl;

I am impressed.

These were just some of the things I've discovered in the last few months of using org-mode, and I am only scratching the surface. Feel free to share your org-mode-writing tips in the comments :-)


building regular expressions

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.


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.


using the systems 'trash' when deleting files

A short tip today.

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.


getting your ip-address

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-address on 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:

(defun get-ip-address (&optional dev)
  "get the IP-address for device DEV (default: eth0)"
  (let ((dev (if dev dev "eth0"))) 
    (substring  ;; chop the final "\n"
          "ifconfig " dev
          "|grep 'inet addr'|sed 's/.*inet addr:\\(\\([0-9\.]\\)*\\).*/\\1/'"))
      0 -1)))
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:

(defun get-ip-address (&optional dev)
  "get the IP-address for device DEV (default: eth0)"
  (let ((dev (if dev dev "eth0"))) 
    (format-network-address (car (network-interface-info dev)) t)))
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:
(defun get-ip-address () 
  "Win32: get the IP-address of the first network interface"
  (let ((ipconfig (shell-command-to-string "ipconfig | findstr Address"))) 
    (string-match "\\(\\([0-9]+.\\)+[0-9]+\\)" ipconfig)
    (match-string 0 ipconfig)))
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.


remembering your position in a file

A little useful trick I recently discovered is SavePlace. Adding

(setq save-place-file "~/.emacs.d/saveplace") ;; keep my ~/ clean
(setq-default save-place t)                   ;; activate it for all buffers
(require 'saveplace)                          ;; get the package
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!).


saving history between sessions

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.


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:

(setq savehist-additional-variables    ;; also save...
  '(search-ring regexp-search-ring)    ;; ... my search entries
  savehist-file "~/.emacs.d/savehist") ;; keep my home clean
(savehist-mode t)                      ;; do customization before activate
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).