2009-07-03

keyboard shortcuts

Emacs has many useful functions, but for the most efficient use you really need to make keybindings (as previously discussed) for the ones you use often. There's no shortage of possible shortcuts, but the number of easy-to-remember bindings is fairly limited.

I can remember bindings that I use all the time like M-w for copy (well, kill-ring-save), or M-q (fill-paragraph) to word-wrap a paragraph, even if they're not that mnemonic (interestingly, it can be hard to verbalize these bindings when someone ask me – but my fingers have no such trouble).

For functions that I use every minute, any weird key binding will do (as long as it is short). And functions that I use seldomly I can look up in a menu. The trouble is with those functions in between :)

Bindings that involve the arrow-keys are attractive, but I've found that e.g. org-mode binds many of those already. And in the olden days, the C-c-prefix was reserved for user-defined key bindings – but again, org-mode and other packages have overtaken that those. So what's left? Recently, I have been using the Windows-key a bit more. Under (my) X, it's bound to super, and emacs does not use it for anything else, so I have been using it to create a set of useful key bindings. I am trying to be a bit systematic, where super + lowercase letter starts some 'application':

(global-set-key (kbd "s-a") 'org-agenda-list) ;; Agenda
(global-set-key (kbd "s-n") 'org-todo-list)   ;; todo-list (nextactions)

;; program shortcuts
(global-set-key (kbd "s-b") 'browse-url)  ;; Browse (W3M)
(global-set-key (kbd "s-f") 'browse-url-firefox)  ;; Firefox...
(global-set-key (kbd "s-t") 'twitter-get-friends-timeline) ;; Twitter
(global-set-key (kbd "s-w") 'wl)            ;; Wanderlust

and super + uppercase opens a file / special buffer, for example:

(global-set-key (kbd "s-S") ;; scratch
  (lambda()(interactive)(switch-to-buffer "*scratch*")))
(global-set-key (kbd "s-E") ;; .emacs
  (lambda()(interactive)(find-file "~/.emacs")))
(global-set-key (kbd "s-G") ;; gtd.org
  (lambda()(interactive)(find-file "~/.emacs.d/org/agenda/gtd.org"))) 

super + arrow keys I now use for windmove (see here).

The windows-keys are not perfect of course – they don't work from the console (easily) or, ironically, on Windows. But they are mnemonic – I can remember s-t for twitter; contrast this with C-x 5 2 to create a new frame.

So, I wonder, does anyone have some clever scheme for their key bindings? I think I am getting much of the efficiency from emacs from the fact that I can do just about anything with some quick actions on the keyboard, so I'm very interested.

8 comments:

Paul R said...

C-c [a-Z] (this is C-c followed by a letter) is reserved to the user. Any mode using it should be fixed. And AFACT org-mode does not use it.

From que Info keymap node as of emacs 23 latest build :
" As a user, you can redefine any key, but it is usually best to stick
to key sequences that consist of `C-c' followed by a letter (upper or
lower case). These keys are "reserved for users," so they won't
conflict with any properly designed Emacs extension. The function keys
F5 through F9 are also reserved for users. If you redefine some
other key, your definition may be overridden by certain extensions or
major modes which redefine the same key."

djcb said...

@Paul R: thanks, that's a useful; C-c X is an alternative for s-X.

Michael Mrozek said...

I have C-x C-m as a group for my hotkeys; you can make it with (define-prefix-command 'my-hotkey-preifx) (global-set-key [(control x)(control m)] 'my-hotkey-prefix) and then just bind things as normal. I avoid using the win key since my window manager binds lots of hotkeys using it

piyo said...

Keyboard settings are a very personal journey, requiring much self-reflection. This is a long post, but I enjoyed writing it. Of course, YMMV.

For keybinding in Emacs, I use Control-Q as my main user map prefix key. It's easy to hit (as a Caps-as-Control disciple), and if I really need a Control-Q I can just press it twice, but really how often do I need it (C-Q C-Q C-j isn't much more mental energy)? I then add more prefix keys under there like "d" as in C-Q d {b,f,d} for ediff-{buffers,files,directories}, C-Q {C-R,r} for query-replace{-regexp,}.

I also bind Control-L as another prefix map key. Out of all the keys in my home row this one is less likely to conflict after redefinition. Since C-L was M-x recenter, I usually put visual related functionality there, like "recenter with this line at top", the winner keys (ESDF just like FPS games), setnu mode, scroll-bar-mode, menu-bar-mode, etc.

I remind myself of my bindings with C-h after the prefix key(s). Also I like to write my bindings in my setting file in qwerty order so that I know what is open and what can be set. Finally I make sure to put all bindings in one file like piyo-input.el.

I say ignore the C-c [a-Z]. I want the freedom to press the Control key for the rest of my sequence. Also after a decade of using Emacs, I am only finally starting to use the Function keys, namely F3 and F4. It's just too far to reach, though. Even the arrow keys are too far.

As an aside, I use Emacs inside of GNU Screen, and I set the GNU Screen key prefix to Control-T instead of the Emacs-clashing default Control-A. How often do I need to press Control-T? It is possible to define sub-prefix key maps in GNU Screen, and I have set for example C-T x {2,3} to split the screen just like Emacs.

I am mainly a Windows user and also use Linux mainly from ssh and Putty. I avoid using the Windows key for just Emacs, and besides, the key is not translated through a terminal emulator. I would not recommend becoming dependent on it for this usage. Under Windows, the Windows key is for system-level mapping solely for Microsoft's apps, not application-level key, so I don't understand how this is "ironic".

You can of course repurpose the Windows key to your own settings. Unfortunately it is not built in. I would recommend a system-level key macro program like AutoHotKey (AHK) so that you can define a lot more keybindings. One great one I made is Windows-Shift-V, which copies the text clipboard directly to Emacs via gnuclientw.exe. You can also redefine keys on a per-application basis, so you make your "Super-E" to work. Perhaps you have to make AHK capture the "Super-E" (which is in reality Windows-E or Open Explorer), which can send to Emacs another user defined key sequence (like the invisible F13 through F22 keys) which you bind to your function.

djcb said...

@Michael: good point about the WM
@piyo: ah, that is a very nice write-up.

I tried the function-keys for a while, but, as you say, they are to far away
for things you need all the time. And they're not very mnemonic for things
that I need only now and then. One exception is F7 for compilation (or
'eval-buffer', 'perl -c', etc.); I think it's the same as in VC++. There the
distance is not so important - I hit the key with force, then lean back in my
chair to see where it will lead me.

I am tempted to move to the "C-c X"-scheme - even though I am a rather
infrequent user of emacs on Win. "C-c" would at least work everywhere -
console, X, Win, Mac etc.

Jared said...

I use triples starting with <f2> as I don't have to press and hold control (super has the same advantages, nice tip), but I group the functions by the second key. e.g. f2-i is for all functions that insert something; f2-t is for my skeletons (templates); f2-s is for support functions; etc.

Anonymous said...

I like to use C-z as my personal prefix character. I used to hate fumbling a C-x ... key and having Emacs minimize itself or drop back to the shell. And I don't need that functionality often enough for it to merit taking up as valuable a piece of keymap real-estate as C-z. So I fixed that and solved my lack of free keys at the same time. Two birds with one stone. For the rare times when I actually want to suspend Emacs, I just use M-x.

Jonas Ă–ster said...

I remap the Caps Lock key to be Super and define a lot of my own key bindings using it. The best part is that I can have logic movement keys instead of C-f, C-b and friends. I have super-j and super-k for left and right character, shift-super-j and shift-super-k for word movement etc.