Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to move between visual lines and move past newline in evil-mode

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.

  1. 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.

  2. 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?

like image 890
kalj Avatar asked Jan 02 '14 12:01

kalj


3 Answers

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)
like image 91
kalj Avatar answered Oct 06 '22 12:10

kalj


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)
like image 29
xwl Avatar answered Oct 06 '22 13:10

xwl


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)
like image 39
PythonNut Avatar answered Oct 06 '22 13:10

PythonNut