

Mastering Key Bindings in Emacs - pdelgallego
http://www.masteringemacs.org/articles/2011/02/08/mastering-key-bindings-emacs/

======
larsberg
I have to admit that my current major Emacs trouble is moving between Linux
and OSX. Switching meta and hyper causes me major brain-fry (alt is
immediately left of the space bar on _every_ keyboard but those made by
Apple).

~~~
gphil
I use the following on my Mac to avoid this issue (I don't use a hyper key in
emacs):

    
    
      (setq mac-command-modifier 'meta)
      (setq mac-option-modifier 'none)
      (setq mac-pass-command-to-system nil)

------
sayemm
Steve Yegge recommends a few key bindings in his awesome post, "Effective
Emacs" - <http://sites.google.com/site/steveyegge2/effective-emacs>

------
prodigal_erik
The workflow stuff looks cool, but between swapping Caps_Lock with Control_L
and claiming M-x "involves scrunching your left hand up", I think most hackers
would benefit more from learning better touch typing, rather than moving their
contortions around the way the author does. You weren't meant to use the same
hand for the key and the modifier--that's why there's another set of modifiers
for your other hand, and why moving Control_L but not Control_R into easier
reach will tempt you into dangerous habits.

~~~
silentbicycle
Indeed.

Also, while it's really its own discussion, I think Emacs is more usable with
the Dvorak keyboard layout. Emacs's default keybindings pack a lot of
important keys into the left hand on Qwerty (C-x, C-c, C-v, C-r, C-s), while
with Dvorak they're distributed more evenly.

Remapping left-Alt to Ctrl is another option; that way, you can hold Ctrl with
your _thumb_ while you type.

------
__david__
Emacs never ceases to amaze me. I've been using it for 15 or so years and yet
had never run across the "kbd" macro or the "remap" event. Both seem very
useful.

------
pangram
What I would really like is an Emacs macro that would just insert the
appropriate keybinding code for use in an .emacs file -- there doesn't seem to
be one but it seems like it would be an obvious help. Or is this one of those
Emacs things that is obvious and everyone knows but me?

~~~
funptr
This one prompts you to type a literal key sequence (exactly like with
`describe-key', C-h k) and inserts it at the point in a representation that
the *-set-key functions can use. As written it's bound to to C-c k, and only
in the scope of elisp buffers.

    
    
        (defun insert-key-sequence (key-repr)
          "Reads a literal key sequence from the user
        and inserts a representation of it at point,
        suitable for `global-set-key' or `local-set-key'."
          (interactive "KKey sequence? ")
          (prin1 key-repr (current-buffer))
          (insert " "))
        
        (add-hook 'emacs-lisp-mode-hook 
                  #'(lambda () 
                      (local-set-key "\C-ck" #'insert-key-sequence)))
        
        (run-hooks 'emacs-lisp-mode-hook)
    

Unfortunately it doesn't seem to work for mouse chords.

~~~
silentbicycle
Nice. I tried to write a quick version of this for my response, but wasn't
aware of insert-key-sequence.

Edit: insert-key-sequence doesn't exist on Emacs 22.3.1, is that a more recent
addition?

Also, while not crucial, I prefer to add a my-X-mode-hook function and add
things to that, rather than adding several lambdas to the mode hook - it's
easier to modify them all after binding that way.

~~~
funptr
It's not part of emacs, I just wrote it as an example.

~~~
silentbicycle
Oh, right. Sorry, hadn't had my coffee yet.

I tried doing exactly that, but used (interactive "kKey: "), with _lowercase_
k, and it didn't work.

~~~
funptr
That seems to work also, what's the problem?

~~~
silentbicycle
Didn't work correctly with a few slightly-more elaborate key combinations I
tried. IIRC C-x M-. was one of them.

I much prefer formatting the keybindings like _kbd_ uses, anyway - do you know
offhand how to convert a keypress to that format w/out using C-h k?

~~~
funptr
Not offhand, but I looked it up and I think what you want is `key-
description'. It prints key sequences, so e.g. (key-description "\370") evals
to "M-x".

~~~
silentbicycle
Bingo. Thanks.

This should do what the original commenter wanted:

    
    
        (defun insert-keybinding (k)
          "Insert code for a keybinding."
          (interactive "kKey sequence: ")
          (insert (concat "(global-set-key (kbd \""
        		  (key-description k)
        		  "\") 'function-name)")))

------
rh32010
Thanks for this article, shows how much more there still is to learn about
emacs(at least for me). I will definitely use the site as a resource from here
on out.

