Category: digital     |    Tags: emacs   linux  

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

Step 1: Xmodmap

    xmodmap -pke > ~/.Xmodmap
    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
xmodmap ~/.Xmodmap
It is possible that keycodes vary in your keyboard. If the above codes does not work after reloading, try using xev

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.

The following snippet depends on packages like general.el, helm, undo-tree, helm-swoop, avy, anzu, yasnippet. You may modify the packages as per your personal preferences
(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

Related Posts

Emacs - Adding clipboad.js to HTML exports
zsh: Parameter expansion cheatsheet
© 2019 - 2024 · Home ยท Theme Simpleness Powered by Hugo ·