Emacs: Notes on keybindings in GNU+Linux
Pointless introduction
Emacs being a rather keyboard driven editor, tends to rely heavily on keyboard shortcuts. The non-conformity of keybindings in emacs is a rather tricky hurdle. While it is accepted that Emacs is practically an operating system and the true emacs-jedi can do everything in Emacs itself, the rest of mortals have to interact with other, normal softwares. Let us consider the simple tasks of cut, copy and paste.
Everyone and their grandmothers know that the shortcut for these are Ctrl+x, Ctrl+c and Ctrl+v. If you happen to flirt with terminals, then you need to remember that in most terminals the shortcuts are Ctrl+Shift+c and Ctrl+Shift+v. Now come to emacs, where the keybindings become Ctrl+w, Alt+w and Ctrl+y. The toll it takes on your muscle memory and frustration that ensues everytime you send SIGKILL instead of copying is no funny buisness.
Plan
We are going to set up Emacs such that we can use the all the standard keybindings like: Ctrl+a to Select all Ctrl+s to Save Ctrl+d to add bookmark Ctrl+f to find Ctrl+z to undo Ctrl+x to cut Ctrl+c to copy Ctrl+v to paste
Since Emacs heavily uses Ctrl and Alt keys, we need to avoid messing with these modifier keys. So here is the plan
- Use
xmodmap
to convert CapsLock to Hyper key. So for every other software except Emacs, CapsLock acts as Hyper. All other modifier keys remain at their default position and usage. - In Emacs and Emacs alone, swap the Hyper and Ctrl keys, so that when you press Ctrl button in keyboard, Hyper is sent to Emacs, while pressing CapsLock button sends Ctrl to Emacs.
- Finally set up keybindings like Hyper-x for cut, Hyper-c for copy in Emacs. Since Emacs is accepting Ctrl as Hyper, the actual keypress would be Ctrl+x, Ctrl+y etc
Step 1: Xmodmap
- Run the following command
xmodmap -pke > ~/.Xmodmap
- Open
~/.Xmodmap
. Add the following code to the file.
clear lock
clear mod1
clear mod2
clear mod3
clear mod4
clear mod5
keycode 66 = Hyper_L
add mod1 = Alt_L Alt_R Meta_L
add mod2 = Num_Lock
add mod3 = Hyper_L
add mod4 = Super_L Super_R
add mod5 = Mode_switch ISO_Level3_Shift
- Reload the xmodmap by running
xmodmap ~/.Xmodmap
Step 2: Emacs
Add the following snippet to your init.el
in Linux OS. If you are using Windows/Mac, find appropriate snippet from
ergoemacs.
(setq x-ctrl-keysym 'hyper) ;;In Emacs, treat Control key as hyper
(setq x-hyper-keysym 'ctrl) ;; In Emacs, treat Hyper Key(Caps_Lock) as Control
My Keybindings
I am using general.el for my keybindings. A subset of which looks as follows.
(use-package general
:defer 2
:init
(general-define-key
:states '(normal insert visual)
:keymaps 'override
"M-x" 'helm-M-x
"H-a" 'mark-whole-buffer
"H-s" 'save-buffer
"H-d" 'bookmark-set
"H-z" 'undo-tree-undo
Ctrl+Shift+z for Redo.
"H-x" 'kill-region
"H-c" 'copy-region-as-kill
"H-v" 'yank
"H-f" 'helm-swoop
"H-F" 'helm-projectile-ag
"H-o" 'fzf-projectile
"H-n" 'helm-find-files ;; Ctrl+n for new files
"H-w" 'kill-this-buffer ;; Ctrl+w kills buffer
"H-g" 'avy-goto-char-2
"H-h" 'anzu-query-replace
"H-H" 'anzu-query-replace-regexp
"H-<tab>" 'helm-mini
"H-*" 'bookmark-jump
"H-y" 'yas-insert-snippet
"H-`" 'helm-register
"H-M-`" 'point-to-register
))
13th Function Key
Another key in the keyboard I hardly ever find using is the Menu
key.
You know, that
key
smugly sitting in between Right AltGr and Right
Ctrl. I configured the Xmodmap so that it will act as
F13. Since no normal keyboard has an F13 key, no
shortcuts are assigned to this in any software. This gives us a array of
possibilities regarding keybindings devoid of headaches. Add the
following line to your ~/.Xmodmap
keycode 135 = F13
and reload your xmodmap by running
xmodmap ~/.Xmodmap
Right Alt as F13
If Menu
key is hard to reach for you, the Right Alt key is another
option to be morphed into F13. The ~/.Xmodmap
will look like this:
clear lock
clear mod1
clear mod2
clear mod3
clear mod4
clear mod5
keycode 66 = Hyper_L
keycode 108 = F13
add mod1 = Alt_L Meta_L
add mod2 = Num_Lock
add mod3 = Hyper_L
add mod4 = Super_L Super_R
add mod5 = Mode_switch ISO_Level3_Shift
and reload your xmodmap by running
xmodmap ~/.Xmodmap
Unrelated tip
Since you are in the business of modifying ~/.Xmodmap
, you may want to
know that it can be also used to change the scrolling using mouse to
natural
scrolling
like those seen in Mac, as opposed to the default reverse scrolling.
Here is the line to add to ~/.Xmodmap
.
pointer = 1 2 3 5 4 7 6 8 9 10 11 12
and reload your xmodmap by running
xmodmap ~/.Xmodmap