Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rebind digits for normal mode in evil

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.

like image 591
user14416 Avatar asked May 15 '15 18:05

user14416


2 Answers

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.

like image 58
tarleb Avatar answered Oct 16 '22 12:10

tarleb


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"))
like image 36
Pandikunta Anand Reddy Avatar answered Oct 16 '22 13:10

Pandikunta Anand Reddy