Current focus: wiki, mailing list, news, donate

Portrait photo of Ciaran

GNU Emacs tips

Emacs makes a computer serene - each marvellously accommodating command satisfies. Everyone may attempt creating superior enhancements, making all code super, ergo, most actions complete splendidly.

(I also have a page of useful shell command.)


Execute one lisp expression in the minibuffer

Simple: M-:

That gives you a prompt (Eval:), then, for example, if you want to see the lisp name of the current major mode (which is stored in the variable major-mode, you type:

(message "%s" major-mode)

And the output appears in the echo area (same place as the minibuffer).

This only lets you execute one expression, so typing in two expressions one after the other won't work, but you can put your two expressions inside another expression, like in this (pointless) example:

(progn (message "%s" major-mode) (message "%s" major-mode))

Relevant manual pages: , , , .

Using variables. Example: print increasing numbers

[M-: (setq x 1)]
Ligne numéro [C-u M-: x]
[M-: (setq x (+ x 1))]

Relevant manual pages: , , , .

Unbinding a key in a specific mode (unsetting a keybinding)


Relevant manual pages: , , , .

Aargh! My Gnus inboxes disappeared! What the fuck!?

(Note: I'm not a real Emacs expert, I'm just a poor sod that's had this problem three times and found no web pages to help him, so I'm writing what I know.)

The good news is, you'll get most of your mail back. The bad news is:

First: what happened? You probably accidently hit C-w, which calls gnus-group-kill-region. The region isn't visible in the Gnus Group buffer but it seems to cover the area from the top of the screen to wherever the cursor is ("point"). So C-w gets rid of all those inboxes. (I don't know why anyone thought this deserved a keyboard shortcut.)

Second: how do I undo this? The 'undo' function doesn't work. It'll just tell you that the buffer is read only. What's happened is, you've "unsubscribed" yourself from your inboxes. So you need to resubscribe. The U key (that's capital-u) can be used to resubscribe. You'll see a list of choices, and you just go through the list one-by-one and resubscribe to all your inboxes.

Lastly: to avoid this problem in the future, unbind the C-w key in the Gnus Group keymap. (I'm still working out how to do this...)

Make Gnus safe by unbinding its dangerous keys

Gnus has easily typed keybindings for commands that will ruin your inbox. C-w is a painful example. It runs gnus-group-kill-region which makes most of your inboxes disappear, and 'undo' just gives "Buffer is read-only: #<buffer *Group*>"

Non-greedy highlighting. Example: hiding ref tags while editing Wikipedia

Summary: use highlight-regexp and instead of .* use .*? - that's the non-greedy variant.

Example: Sometimes when I edit Wikipedia, I want to focus on the text without being distracted by the contents of the ref tags. Each paragraph is a single line of text, so if I highlight a regexp like <ref.*</ref> then emacs highlights from the first ref tag to the end of the last ref tag, and everything in between. If instead I highlight <ref.*?</ref> then Emacs highlights just from the first ref tag to the end of that ref tag, and again from the start of the second ref tag to the end of the second ref tag, but the text between the tags is left alone.

Relevant manual pages: , , , .


Join multiple lines

The join-line command (M-^) joins the current line to the line above, but sometimes you'll want to join all the lines in a selected region. This is especially useful in keyboard macros. Below are two easy hacks and one real solution. Here are three solutions:

One is to replace the line endings with a space:
M-x replace-string RET C-q C-j RET SPC RET
A disadvantage is that this won't delete any extra spaces that were at the start of the line.

A second, slightly messier method is to set a huge fill column, select the region, and "fill" everything onto one line:

M-1-0-0-0-0-0 C-x f
M-x fill-region

Or thirdly you can create a join-region command by putting this in your .emacs:

(defun join-region (beg end)
  "Apply join-line over region."
  (interactive "r")
  (if mark-active
          (let ((beg (region-beginning))
                        (end (copy-marker (region-end))))
                (goto-char beg)
                (while (< (point) end)
                  (join-line 1)))))

Sources: 1, 2, 3


List of commands to remember


External links

(Go: homepage of Ciarán O'Riordan)

Valid XHTML 1.1! Powered by GNU Made with Emacs Join FSF! (I'm member #8)

(I made the Emacs and GNU buttons. Feel free to do with them whatever you like.)

© Copyright 2013 Ciarán O'Riordan. Verbatim copying and distribution of this entire article is permitted in any medium, provided this notice is preserved. Distribution of modified versions of all or part of this article are permitted, provided that such works carry three things: (1) this copyright notice, (2) prominent notices stating the that it has been changed, and (3) information for how to obtain the original (such as a URL).