10

Is it possible to rebind digits. That, for example, "5" is "$", and "%" is "5"?

In evil-maps.el digits are defined like this.

(define-key evil-motion-state-map "1" 'digit-argument)
(define-key evil-motion-state-map "2" 'digit-argument)
...

I tried the answer of @ChillarAnand

(add-hook 'evil-mode-hook 'evil-mode-bindings)

(defun evil-mode-bindings ()
  "Bind symbols to digits."
  (define-key key-translation-map (kbd "%") "5")
  (define-key key-translation-map (kbd "*") "8")
  )
(define-key evil-normal-state-map "5" 'evil-beginning-of-line)
(define-key evil-normal-state-map "8" 'evil-end-of-line)

But Shift-5 still does not behave like 5, the same is true for 8. Is it possible to fix it for the config above?

The same stands for @tarblet solution.

What I use as a test is a sequence Shift-5, G.

user14416
  • 2,922
  • 5
  • 40
  • 67
  • So you want `%` to map to `digit-argument` with a value of 5? – tripleee May 23 '15 at 10:30
  • Yes, I think so. That when I press `Shift-5` the editor behave like I pressed `5` in the default configuration. – user14416 May 23 '15 at 11:05
  • Forgot to add to the snippet. – user14416 May 29 '15 at 13:34
  • i think your key bindings are getting modified after this step, can you run `C-h k Shift-5` & `C-h k G` and make sure they are binded to `evil-beginning-of-line` & `evil-goto-line &optional COUNT` respectively? – Chillar Anand May 29 '15 at 16:01
  • @user14416 What seems to be the problem with my solution? Did you test the updated version (motion state → normal state)? – tarleb Jul 04 '15 at 19:23

2 Answers2

2

Quite a hacky solution, but it should do what you want:

(defun capslock-digit-argument-fn (digit)
  `(lambda (arg)
     (interactive "P")
     (setq last-command-event (+ ,digit ?0))
     (digit-argument arg)))

(define-key evil-motion-state-map "!" (capslock-digit-argument-fn 1))
(define-key evil-motion-state-map "@" (capslock-digit-argument-fn 2))
(define-key evil-motion-state-map "#" (capslock-digit-argument-fn 3))
(define-key evil-motion-state-map "$" (capslock-digit-argument-fn 4))
(define-key evil-motion-state-map "%" (capslock-digit-argument-fn 5))
(define-key evil-motion-state-map "^" (capslock-digit-argument-fn 6))
(define-key evil-motion-state-map "&" (capslock-digit-argument-fn 7))
(define-key evil-motion-state-map "*" (capslock-digit-argument-fn 8))
(define-key evil-motion-state-map "(" (capslock-digit-argument-fn 9))

It rebinds the variable which digit-argument looks at when trying to figure out which key was pressed. If you don't mind ) not behaving exactly like 0 (no jumping to beginning of line, only working as digit arg) you could set it as well.

tarleb
  • 19,863
  • 4
  • 51
  • 80
  • You solution does not work for example for sequend of `Shift-5`, `G`. – user14416 May 29 '15 at 14:28
  • Sorry, just noticed that you asked for normal-mode, not just motion mode. Please try replacing `evil-motion-state-map` with `evil-normal-state-map` in above code. – tarleb Jun 26 '15 at 09:06
2

Ofcourse, anything is possible in emacs :)

Add this piece of code to you config.

(add-hook 'evil-mode-hook 'evil-mode-bindings)

(defun evil-mode-bindings ()
  "Bind symbols to digits."
  (define-key key-translation-map (kbd "!") (kbd "1"))
  (define-key key-translation-map (kbd "@") (kbd "2"))
  (define-key key-translation-map (kbd "#") (kbd "3"))
  (define-key key-translation-map (kbd "$") (kbd "4"))
  (define-key key-translation-map (kbd "%") (kbd "5"))
  (define-key key-translation-map (kbd "^") (kbd "6"))
  (define-key key-translation-map (kbd "&") (kbd "7"))
  (define-key key-translation-map (kbd "*") (kbd "8"))
  (define-key key-translation-map (kbd "(") (kbd "9"))
  (define-key key-translation-map (kbd ")") (kbd "0")))

Whenever You enter evil mode, evil-mode-hook runs evil-mode-bindings function. This function binds, symbols to corresponding digits.

Update:

As @npostavs mentioned, You can also use this

(add-hook 'evil-mode-hook 'evil-mode-bindings)

(defun evil-mode-bindings ()
  "Bind symbols to digits."
  (define-key key-translation-map (kbd "!") "1")
  (define-key key-translation-map (kbd "@") "2")
  (define-key key-translation-map (kbd "#") "3")
  (define-key key-translation-map (kbd "$") "4")
  (define-key key-translation-map (kbd "%") "5")
  (define-key key-translation-map (kbd "^") "6")
  (define-key key-translation-map (kbd "&") "7")
  (define-key key-translation-map (kbd "*") "8")
  (define-key key-translation-map (kbd "(") "9")
  (define-key key-translation-map (kbd ")") "0"))
Chillar Anand
  • 27,936
  • 9
  • 119
  • 136