10

How do I visually select the last pasted text with evil-emacs?

gv reselects the last visual selection. I'd like a function/snippet that does the same for my last pasted text.

The Unfun Cat
  • 2,393
  • 16
  • 32

3 Answers3

6

In my .vimrc I have

nnoremap <leader>v '[V']

to do exactly this.

Amazingly, the sequence '[V'] works in evil just fine. To have a shortcut in Emacs, I wrote the following function:

(defun my/evil-select-pasted ()
  (interactive)
  (let ((start-marker (evil-get-marker ?\[))
        (end-marker (evil-get-marker ?\])))
        (evil-visual-select start-marker end-marker)))

The function can be bound to e.g. <leader>v using evil-leader:

(evil-leader/set-key "v" 'my/evil-select-pasted)

I'm relatively new to Emacs, so this might not be ideal, but it works for me.

How I got there:

C-h k ' shows that ' runs evil-goto-mark-line. Looking at the source in evil-commands.el shows that evil-goto-mark-line uses evil-goto-mark, which itself makes use of the evil-get-marker function. Looking at evil-states.el, found with C-h k V, leads to the evil-visual-select function.

Basil
  • 12,019
  • 43
  • 69
Andreas
  • 76
  • 1
  • 2
3

When using @andreas solution to select last pasted text and tried to change indentation on it and then repeating the indentation with . (evil-repeat) it gave me errors. I had to modify the function to be like this, which now works fine for me:

(defun evil-select-pasted ()
  "Visually select last pasted text."
  (interactive)
  (evil-goto-mark ?\[)
  (evil-visual-char)
  (evil-goto-mark ?\]))
Basil
  • 12,019
  • 43
  • 69
zzantares
  • 217
  • 1
  • 9
0

The function in answer of @Andreas didn't work correctly for my spacemacs config
I found a answer on reddit works more correct:

(defun nh-evil/select-last-paste ()
  (interactive)
  (when (not evil-last-paste)
    (user-error "No last paste to highlight"))
  (let ((beg (nth 3 evil-last-paste))
        (end (nth 4 evil-last-paste)))
    (evil-visual-select beg end)))

(define-key evil-visual-state-map (kbd "i v") 'nh-evil/select-last-paste)

Then you could do in evil normal state: viv (just like vig, vit, viw) to select just pasted region (with evil-paste-after or other related commands)

ri wen
  • 3
  • 3