In the 'normal' state of evil-mode
, cursor movement is emulating the standard behavior of vim. I want to make it more similar to the standard emacs behavior in the following two ways.
I want vertical movement to take place within visual lines, rather than logical lines. I.e. if a line is wrapped, pressing j
or <down>
should move to the next part of the same line.
I want horizontal movement to not stop at newlines. I.e. if the cursor is at the end of a line, pressing l
or <right>
should move to the next line.
How can I achieve this?
I found how to solve problem 2: It turned out there was a variable evil-cross-lines
. Setting this to t
simply solved that problem.
Together with the fix for vertical movement by PythonNut, here is the full solution:
;; Make movement keys work like they should
(define-key evil-normal-state-map (kbd "<remap> <evil-next-line>") 'evil-next-visual-line)
(define-key evil-normal-state-map (kbd "<remap> <evil-previous-line>") 'evil-previous-visual-line)
(define-key evil-motion-state-map (kbd "<remap> <evil-next-line>") 'evil-next-visual-line)
(define-key evil-motion-state-map (kbd "<remap> <evil-previous-line>") 'evil-previous-visual-line)
; Make horizontal movement cross lines
(setq-default evil-cross-lines t)
The remap solution would make `dj' like combinations fail. Below advice solution still keeps right behavior for `dj':
(defun evil-next-line--check-visual-line-mode (orig-fun &rest args)
(if visual-line-mode
(apply 'evil-next-visual-line args)
(apply orig-fun args)))
(advice-add 'evil-next-line :around 'evil-next-line--check-visual-line-mode)
(defun evil-previous-line--check-visual-line-mode (orig-fun &rest args)
(if visual-line-mode
(apply 'evil-previous-visual-line args)
(apply orig-fun args)))
(advice-add 'evil-previous-line :around 'evil-previous-line--check-visual-line-mode)
I can't seem to get right movement working. C-h k l
says right-char
.
;; kill two birds with one stone using remap: arrow keys and h,j,k,l
(define-key evil-normal-state-map (kbd "<remap> <evil-next-line>") 'evil-next-visual-line)
(define-key evil-motion-state-map (kbd "<remap> <evil-previous-line>") 'evil-previous-visual-line)
(define-key evil-motion-state-map (kbd "<remap> <evil-next-line>") 'evil-next-visual-line)
(define-key evil-normal-state-map (kbd "<remap> <evil-previous-line>") 'evil-previous-visual-line)
(define-key evil-normal-state-map (kbd "<remap> <evil-backward-char>") 'left-char)
(define-key evil-motion-state-map (kbd "<remap> <evil-forward-char>") 'right-char)
(define-key evil-normal-state-map (kbd "<remap> <evil-backward-char>") 'left-char)
(define-key evil-motion-state-map (kbd "<remap> <evil-forward-char>") 'right-char)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With