Difference between revisions of "Key Bindings"

From WikEmacs
Jump to: navigation, search
(Change key bindings)
 
(18 intermediate revisions by 9 users not shown)
Line 1: Line 1:
Emacs ships with a bunch of Cheatsheets or Reference Cards for popular packages. These are located in the <tt>refcards/</tt> subdirectory of {{Variable|data-directory}}.
+
In Emacs, almost any keystroke, optionally combined with one or more modifier keys, or any sequence of such keystrokes, can be "bound" to any [[command]].
  
Third-party reference cards exist:
+
== Modifier keys naming convention ==
  
* TBC
+
{| class="wikitable"
 +
|-
 +
! Modifier key
 +
! Abbr.
 +
! Remarks
 +
! Example
 +
! Action
 +
|-
 +
| Alt
 +
| A
 +
| Relatively seldom used in Emacs, because Meta is often "logically" mapped to the "physical" <tt>Alt</tt> key.
 +
| A-x
 +
| Type "x" while holding down the (non-Meta) Alt key
 +
|-
 +
| Control
 +
| C
 +
|
 +
| C-x C-f
 +
| While holding down Ctrl, type first "x" then "f". (This runs the command <tt>find-file</tt>).
 +
|-
 +
| Hyper
 +
| h
 +
| Often not mapped to any key (by default).
 +
| h-x
 +
| Type "x" while holding down hyper
 +
|-
 +
| Meta
 +
| M
 +
| Often mapped to <tt>Alt</tt> on your keyboard
 +
| M-x find-file RET
 +
| Either:
 +
* type "x" while holding down Meta, or
 +
* type "Esc" then "x"
 +
(M-x activates the [[minibuffer]])
  
You can add, remove and change key bindings for any command, either globally or locally to a mode, interactively (and only for the current session) via <tt>M-x global-set-key</tt> and <tt>M-x local-set-key</tt>, or persistently using simple [[Emacs Lisp]] code in your <tt>init.el</tt> or <tt>.emacs</tt> file which is loaded when Emacs first starts up.
+
Then type "find-file" in the minibuffer, then Return.
 +
|-
 +
| Shift
 +
| S
 +
|
 +
| S-x
 +
| Type "x" while holding down Shift
 +
|-
 +
| Super
 +
| s
 +
| On OS X this is the Command ⌘ key.
 +
On other systems often not mapped to any key by default, or to <tt>Window</tt> or <tt>Menu</tt> if present
 +
| s-x
 +
| Type "x" while holding down super
 +
|-
 +
|}
  
[[Category:Tweak Key Bindings]]
+
By convention, when multiple modifiers are specified, they go in alphabetical order: {{Keys|C-M-x}} not {{Keys|M-C-x}}.
 +
 
 +
== Printable cheat sheet for popular modes ==
 +
Emacs ships with cheat sheets of common and useful key bindings.
 +
The {{File|pdf}} files are located in the {{Directory|refcards}} subdirectory of {{Variable|data-directory}}.
 +
 
 +
== Change key bindings ==
 +
 
 +
You can add, remove and change key bindings for any command by using the Emacs Lisp function <code>global-set-key</code> or <code>local-set-key</code>.
 +
 
 +
; {{Command|global-set-key}} or {{Command|local-set-key}}
 +
: Hit the key sequence you want to bind
 +
: Type the command to bind to that key sequence
 +
: Invoke {{Keys|C-x ESC ESC|repeat-complex-command}}. The output you see is the full lisp command which does the binding.  You can copy this to your {{EmacsConfigFile}}.
 +
 
 +
'''Notes:'''
 +
 
 +
* <code>global-set-key</code> bind keys globally (i.e. <code>global-mode-map</code>), which means other modes would override key bindings set by <code>global-set-key</code>.
 +
 
 +
* <code>local-set-key</code> bind keys in the current buffer [[major mode]], which affects all buffers sharing the mode.
 +
 
 +
== External Links ==
 +
 
 +
* [http://www.masteringemacs.org/articles/2011/02/08/mastering-key-bindings-emacs/ Mastering Keybindings in Emacs]
 +
 
 +
[[Category:Customization]]
 +
[[Category:Key Concepts]]

Latest revision as of 23:00, 10 May 2012

In Emacs, almost any keystroke, optionally combined with one or more modifier keys, or any sequence of such keystrokes, can be "bound" to any command.

Modifier keys naming convention[edit]

Modifier key Abbr. Remarks Example Action
Alt A Relatively seldom used in Emacs, because Meta is often "logically" mapped to the "physical" Alt key. A-x Type "x" while holding down the (non-Meta) Alt key
Control C C-x C-f While holding down Ctrl, type first "x" then "f". (This runs the command find-file).
Hyper h Often not mapped to any key (by default). h-x Type "x" while holding down hyper
Meta M Often mapped to Alt on your keyboard M-x find-file RET Either:
  • type "x" while holding down Meta, or
  • type "Esc" then "x"

(M-x activates the minibuffer)

Then type "find-file" in the minibuffer, then Return.

Shift S S-x Type "x" while holding down Shift
Super s On OS X this is the Command ⌘ key.

On other systems often not mapped to any key by default, or to Window or Menu if present

s-x Type "x" while holding down super

By convention, when multiple modifiers are specified, they go in alphabetical order: [C-M-x] not [M-C-x].

Printable cheat sheet for popular modes[edit]

Emacs ships with cheat sheets of common and useful key bindings. The pdf files are located in the refcards/ subdirectory of data-directory.

Change key bindings[edit]

You can add, remove and change key bindings for any command by using the Emacs Lisp function global-set-key or local-set-key.

M-x global-set-key or M-x local-set-key
Hit the key sequence you want to bind
Type the command to bind to that key sequence
Invoke [C-x ESC ESC]. The output you see is the full lisp command which does the binding. You can copy this to your Emacs configuration file.

Notes:

  • global-set-key bind keys globally (i.e. global-mode-map), which means other modes would override key bindings set by global-set-key.
  • local-set-key bind keys in the current buffer major mode, which affects all buffers sharing the mode.

External Links[edit]