mail-alt git-squared

~/abhirag

Emacs-fu for the masses

First things first, I ❤ EmacsEmacs is the original extensible, customizable, self-documenting text editor., some people eat ice-cream to chase the blues away, some people binge watch Netflix, I tinker with my Emacs config. I have spent ungodly amount of time tinkering with Emacs, trying to extend it to execute my will. It's no longer just a code editor for me, it's a representation of my thoughts, it has a little bit of me in it, it's personal.

I’m using Linux. A library that Emacs uses to communicate with Intel hardware.

- Erwin, #emacs, Freenode.

The way of Emacs is not the only true way thoughI do use an IDE when using one makes sense and I have no qualms in dropping into vim over ssh., Emacs has it's quirks and some of them can make it extremely difficult for beginners to stick with it. The biggest quirk in my personal experience is the overriding of native keybindings. "Ctrl-c", "Ctrl-v", "Ctrl-a", "Ctrl-x" etc. are already commited to the muscle memory by most and overriding them can seem arcane. Cua-mode exists, precisely for this purpose but I have found it to be cumbersome in practice.

When dealing with Emacs, the answer to "Is there a better way?" is always "Yes", like everything else, keybindings in Emacs are also customizable, though while doing so we have to make sure that our newly defined keybindings don't conflict with the pre-defined ones. I use the following Emacs Lisp snippet to define custom keybindings:

(define-prefix-command 'custom-map)
(global-set-key (kbd "C-z") 'custom-map)
(define-key custom-map (kbd "C-a") 'mark-whole-buffer) ; Select-all.
(define-key custom-map (kbd "C-v") 'yank) ; Paste.
(define-key custom-map (kbd "C-x") 'kill-region) ; Cut.
(define-key custom-map (kbd "C-c") 'kill-ring-save) ; Copy.

    

"C-z" by default triggers suspend-frame and thus can be overriden without causing any problems. Putting this snippet in your Emacs file will make all these keybindings globally available i.e. available irrespective of the mode(s) you are in. In other words now you can Cut using "C-z C-x", Copy using "C-z C-c", Paste using "C-z C-v" and Select-all using "C-z C-a".

I can already hear the sound of pitchforks being sharpened. You asked for "Ctrl-c" and I gave you "Ctrl-z Ctrl-c", but with God-modeGod-mode is a global minor mode for entering Emacs commands without modifier keys. engaged "Ctrl-z Ctrl-c" gets converted to "zc" and these keybindings become much more convenient. Even without God-mode the presence of "C-z" as a customizable prefix opens up the potential for further customization. I have presented just four custom keybindings using the prefix "C-z", now it's your turn to go bananas with it.