Mac – evil-mode ‘>’ evil-shift-left loses selection

emacsevil

Is there any reason that evil-mode evil-shift-left loses the selection?

I.e., I'd like the ability to hit the '>' and '<' multiple times on the selection to shift text left and right.

Here's the implementation below. Anyway to add an advice to change this behavior?

evil-commands.el

(evil-define-operator evil-shift-right (beg end &optional count)
  "Shift text from BEG to END to the right.
The text is shifted to the nearest multiple of `evil-shift-width'
\(the rounding can be disabled by setting `evil-shift-round').
See also `evil-shift-left'."
  :type line
  (interactive "<r><vc>")
  (let ((beg (set-marker (make-marker) beg))
        (end (set-marker (make-marker) end)))
    (dotimes (i (or count 1))
      (if (not evil-shift-round)
          (indent-rigidly beg end evil-shift-width)
        (let* ((indent
                (save-excursion
                  (goto-char beg)
                  (evil-move-beginning-of-line nil)
                  (while (and (< (point) end) (looking-at "[ \t]*$"))
                    (forward-line))
                  (if (> (point) end) 0
                    (current-indentation))))
               (offset (- evil-shift-width (mod indent evil-shift-width))))
          (indent-rigidly beg end offset))))
    (set-marker beg nil)
    (set-marker end nil)))

Best Answer

  • This overloads the current behavior to re-select (found at djoyner/dotfiles/emacs/lisp/evil-config.el#L36-L40).

    ; Overload shifts so that they don't lose the selection
    (define-key evil-visual-state-map (kbd ">") 'djoyner/evil-shift-right-visual)
    (define-key evil-visual-state-map (kbd "<") 'djoyner/evil-shift-left-visual)
    (define-key evil-visual-state-map [tab] 'djoyner/evil-shift-right-visual)
    (define-key evil-visual-state-map [S-tab] 'djoyner/evil-shift-left-visual)
    
    (defun djoyner/evil-shift-left-visual ()
      (interactive)
      (evil-shift-left (region-beginning) (region-end))
      (evil-normal-state)
      (evil-visual-restore))
    
    (defun djoyner/evil-shift-right-visual ()
      (interactive)
      (evil-shift-right (region-beginning) (region-end))
      (evil-normal-state)
      (evil-visual-restore))
    
  • Related Question