Michael
Michael

Reputation: 1963

Emacs Evil keybinding with evil-visual-state-map not working

In Vim, I have this keybinding for navigation: noremap <M-j> 5j. This will work in normal, visual, and visual line modes.

I want to have the same behavior in Emacs, so I did:

(define-key evil-visual-state-map "\M-j" '(lambda () (interactive) (evil-next-line 5)))

(define-key evil-motion-state-map "\M-j" '(lambda () (interactive) (evil-next-line 5)))

It will work in normal and visual mode, but not in visual line mode (that is, uppercase V).

I'm new to Emacs, coming from Vim.

Upvotes: 2

Views: 999

Answers (2)

Linus Arver
Linus Arver

Reputation: 1368

If you have

(defun godown() (interactive) (evil-next-line 5)) (define-key evil-visual-state-map "\M-j" 'godown)

then you can do either just add

(evil-declare-motion 'godown)

OR you can use

(evil-define-motion godown () (interactive) ... )

to replace the defun + evil-declare-motion combo.

Source: https://bitbucket.org/lyro/evil/issues/395/cant-use-evil-next-line-in-a-script-in-a

Upvotes: 0

Ehvince
Ehvince

Reputation: 18375

looking at the source, let's do that:

(evil-define-motion myevil-next-visual-line (count)
"Move the cursor COUNT screen lines down, or 5."
:type exclusive
  (let ((line-move-visual t))
  (evil-line-move (or count 5))))

and

(define-key evil-visual-state-map "\M-j" 'myevil-next-visual-line)

Upvotes: 2

Related Questions