2010-07-30

some handy key bindings

Emacs offers many handy key bindings; every now and then I come across a new one, which has been hiding there somewhere for a decade or more… Here are some of my favorites – I'm listing those that are (a) often useful, (b) might not be known by everyone already (c) don't require any external packages or setup.

  • M-27 x gives you xxxxxxxxxxxxxxxxxxxxxxxxxxx; and, believe it or not, works also with different characters and numbers;
  • M-m jumps to the first non-whitespace character on the current line;
  • M-^ joins two lines into one – like vi(m)'s :join, except that point must be on the second line, not the first;
  • M-/ auto-completes based on words in all your buffers; there are more powerful alternatives, but this one does not require any setup;
  • C-h k followed by some key or key combination tells you what it does, C-h m describes the currently active modes, with their key bindings;
  • C-h f documents the current function, C-h v does the same for variables. C-h a gives you information about commands - for example to get date-related commands, press C-h a date. This will, however, also get you commands related to update; instead, you can use C-h a \bdate (because C-h a accepts regular expressions);
  • C-x C-o will delete all the empty lines around your current cursor position, except for one;
  • M-q re-aligns the current paragraph; I use it all the time when writing e-mails etc. (you might want to check out filladapt for a version that gives you a bit more smartness with indentations, lists etc.);
  • C-x 8 RET in a recent emacs version gives you an auto-completable list of special characters to insert. So if I need, say, the Yen-character, I type C-x 8 RET ye TAB and I get YEN SIGN, which RET will then insert: ¥. Note that the completion only works on the start of the character name, so if you'd want to include the α-character, you'd need to know that its UCS-name is GREEK SMALL LETTER ALPHA… (you can try *alpha or TAB the empty string, and search in the results buffer, but that's rather slow);
  • C-h l shows your last 300 key presses ('lossage'). Interesting to see, and it might be useful when defining keyboard macros.

What are your favorites? Please share them in the comments.

23 comments:

Anonymous said...

My favourites are the rectangle commands: C-x r k to kill the rectangle and C-x r t to add text.

Unknown said...

I have a small correction regarding the C-x 8 RET completion:

It is also possible to use * as wildcard. C-x 8 RET HEX*GRE*POW will get you the HEXAGRAM OF GREAT POWERand thusly great power!

a said...

Note: C-u M-^ mimic's vi's join.

M-q fills the current paragraph, not the region.

a said...

And M-/ (bound to hippie-expand) has to be my favorite binding.

djcb said...

@Moritz U: ah, didn't know that, thanks! corrected.
@BFW: thanks, corrected

Andy Chilton said...

Cool, a few good ones there I didn't know about. Thanks.

I tried C-x C-o and it is one I'll now remember. When I press it in amongst 5 white lines it does the right thing, but I expected that if I pressed it on a single white line it wouldn't actually do anything. Instead it removes it.

Do you know of a command such that if I press it on a single white line (e.g. that line has just whitepace on it) it won't compress the line but instead just remove the whitespace?

Anonymous said...

C-h b for showing all keybindings is also for interest.
C-down-mouse-3 to access the menubar as context menu.
C-down-mouse-1 to acces the buffer-menu.
C-M-SPC to mark symbols (sexp)
M-TAB complete-symbol (usefull for lisping)

Anonymous said...

My favorite is M-x viper-mode ;)

Nic said...

@Andy Chilton: try M-\ (delete-horizontal-space).

Laurent said...

A few more nice shortcuts:

C-M-f, C-M-b: forward-sexp, backward-sexp
M-SPC: just-one-space
C-c : winner-undo (use winner-mode)

John Tells All said...

I use the "repeat" one quite a bit:

C-u C-u C-u : -- creates a row of 64 colons to separate seconds of code. (64 = 4*4 *4)

C-u 50 : -- same, but 50 chars, to leave room for a label

On some versions the ":" is absorbed, so quote it: C-u 50 C-q :

Eoin said...

Is there a command to correct all of the indentation in a file?

When I get a horribly indented java code file from a student, I have to manually go to each line and hit TAB just so I can read it!

djcb said...

Eouin: try C-x h C-M-\

ie. mark-whole-buffer and then indent-region

Eoin said...

Works like a dream. Thank you djcb! :-)

Anonymous said...

The completion on C-x 8 RET appears to be more versatile even than Moritz U indicates. It seems to me that pressing TAB will attempt the following in sequence: (1) Find completions for the text such that the strings before and after the cursor remain intact. (2) Otherwise, find completions for the text based only on the string before the cursor.

So "alpha" TAB will find nothing, but "alpha" C-a TAB will find everything ending in "alpha", and "alpha" C-a C-f TAB will find only "APL FUNCTIONAL SYMBOL ALPHA", because that is the only name starting with "A" and ending with "LPHA".

Anonymous said...

Eoin: As well as the indent-region solution, remember that any time you find yourself repeating the same action on every line (or many lines) in a buffer, you can probably speed things up by using macros. Especially in conjunction with apply-macro-to-region-lines, in which case you would only need to type: F3 TAB F4, then mark the region (or C-x h to mark the whole buffer) and use M-x apply-macro-to-region-lines to indent every line.

Anonymous said...

Andy: This should also work if you don't happen to remember the other binding: C-a enter C-p C-x C-o

(That is, make sure that there actually is more than one line there...)

Anonymous said...

C-m

It always kind of annoyed me not beign able to get to the first character of a line (but not enough to bind some key to it). Thank you for these tips!

Sue D. Nymme said...

I use a million of'em. It's awesome that emacs does so much useful stuff right out of the box.

M-% is query-replace. Like replace-string, but it prompts you to confirm each replacement.

C-M-% is query-replace-regexp. It's the same thing, but with regular expressions. It's also the only default command I know of that requires four fingers. Impress your vi-using friends!

C-x n n "narrows" the buffer to the region (the text you've selected). Useful for doing global replacements on a limited part of the buffer. Get back to the whole buffer with C-x n w ("widen").

C-x ESC ESC repeats the last "complex" command (for example, a rectangle command).

C-x C-x exchanges point and mark; that is, moves to the other end of the text selection.

C-x = tells you about the character under the cursor.

M-u, M-l, and M-c uppercase, lowercase, or capitalize the word under the cursor -- but here's a little-known neat trick: Type M-- (meta-minus) first, and it'll do the same thing to the word *before* the cursor, *without* moving the cursor. Handy.

Andy Gimblett said...

On a Mac, I like this in my .emacs:

;; Font size up and down
(global-set-key [(super =)] 'text-scale-increase)
(global-set-key [(super -)] 'text-scale-decrease)

which allows me to scale text up & down using Cmd-plus and Cmd-minus.

(You could instead use, eg, mac-key-mode.el to get "Mac-like" bindings like this for free, and to use Cmd as meta instead of super, but that's way more modification than I want, and I like having super available.)

Travis Briggs said...

M-m -- I love you, I love you, I love you!

I've been using C-a ...

Anonymous said...

C-M-l display the current function as much as possible. There are times when a function is too big to fit in a window.

Anonymous said...

I bind C-x j to bury-buffer.

M-: to evaluate an expression in the minibuffer