3

In emacs, I would like to rebind the top row of my keyboard [1...0] so that hitting an unmodified key results in its shifted equivalent. That is, typing 1234567890 would result in !"£$%^&*() being inserted in the buffer.

I am using emacs 24.1.1 in Windows Vista, with viper-mode enabled. I am doing some Common Lisp programming using slime. I use viper so that I can avoid using Ctrl and Shift too often as I can get a bit of emacs pinkie (RSI). Having started programming in lisp, I have found that hitting S-9 and S-0 to open and close parentheses is starting to take its toll.

By including the following in my start-up file, I can bind 9 to ( and vice-versa.

(defvar my-keymap
    (let ((map (make-sparse-keymap)))
    (define-key map (kbd "9") '(lambda () (interactive) (insert "(")))
    (define-key map (kbd "(") '(lambda () (interactive) (insert "9")))
    map))

(viper-modify-major-mode
    'lisp-mode
    'insert-state
    my-key-map)

This works well enough and is easily extended to the rest of the row, except that I would like to be able to toggle between the two modes without having to hold down shift (say, by toggling Caps Lock).

Is there any way to do this, or am I approaching it all wrong?

Phil Dukhov
  • 67,741
  • 15
  • 184
  • 220
laffoyb
  • 1,540
  • 3
  • 22
  • 35

1 Answers1

3

Here's an example I quickly hacked together, tested it in Emacs24 on Linux:

(setq viper-mode-key-mapping "custom")

(defvar custom-viper-keymap
    (let ((map (make-sparse-keymap)))
      (define-key map (kbd "9") '(lambda () (interactive) (insert "(")))
      (define-key map (kbd "(") '(lambda () (interactive) (insert "9")))
      map))

(defvar default-viper-keymap
    (let ((map (make-sparse-keymap)))
      (define-key map (kbd "9") '(lambda () (interactive) (insert "9")))
      (define-key map (kbd "(") '(lambda () (interactive) (insert "(")))
      map))


(defun switch-viper-mode-custom-keymap () 
  (interactive) 
  (if (string= viper-mode-key-mapping "default")
      (progn (setq viper-mode-key-mapping "custom")
         (viper-modify-major-mode 'lisp-mode 'insert-state custom-viper-keymap))
    (progn (setq viper-mode-key-mapping "default")
       (viper-modify-major-mode 'lisp-mode 'insert-state default-viper-keymap))))

(global-set-key [(control f1)] 'switch-viper-mode-custom-keymap)

When I have viper-mode activated, pressing CTRL-F1 switches the keyboard mapping from custom to normal.

raju-bitter
  • 8,906
  • 4
  • 42
  • 53
  • When modifying the key bindings in Emacs, I found this article very useful: http://www.masteringemacs.org/articles/2011/02/08/mastering-key-bindings-emacs/ – raju-bitter Aug 17 '12 at 18:05
  • Vi/Vim are very powerful, I just got into the Unix/Linux world using Emacs. Viper mode looks like a cool mode, easy to customize and well documented. No need to get into a vi/emacs holy war. :) – raju-bitter Aug 17 '12 at 23:21
  • I came to emacs the same way. If it weren't for the repetitive strain I would have stayed with it, but viper gives an excellent method of getting around using chords while still having emacs' awesome power and flexibility. – laffoyb Aug 17 '12 at 23:28