Thursday, August 25, 2016

An Emacs Friendly Caps Lock Configuration on Windows

While this may be obvious, I was pretty dang pleased with myself when I managed to turn the Caps Lock key on my Windows 10 computer into an emacs friendly Hyper key. Here's what I did:

Step 1. Use AutoHotKey to trivially map the Caps Lock key to the Windows Menu key, or as AutoHotKey calls it, the AppsKey.

;; Add this to your standard AutoHotKey configuration
CapsLock::AppsKey

Step 2. Use this elisp code to capture the Menu key from within emacs and map it to the Hyper modifier:

;; http://ergoemacs.org/emacs/emacs_hyper_super_keys.html
(setq w32-pass-apps-to-system nil)
(setq w32-apps-modifier 'hyper) ; Menu/App key

Step 3. Enjoy! I can now map any key binding using the H- modifier. Here's some code I added to my PHP setup:

(defun bs-php-mode-hook ()
  (local-set-key '[backtab] 'indent-relative)
  (local-set-key (kbd "<H-left>") 'beginning-of-defun)
  (local-set-key (kbd "<H-right>") 'end-of-defun)
  (auto-complete-mode t)
  (require 'ac-php)
  (setq ac-sources  '(ac-source-php ))
  (yas-global-mode 1)
  (setq indent-tabs-mode nil)
  (setq php-template-compatibility nil)
  (setq c-basic-offset 2))

The result: when I open up a PHP file, I can jump between function definitions by holding down Caps Lock and left or right arrow.

I feel like I just won the keyboard shortcut lottery!

2 comments:

  1. The Hyper key is mandatory.

    ReplyDelete
  2. The whole "pass modifiers to OS, or not" thing is powerful.

    ReplyDelete