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.

  • Sounds like you're asking for someone to write you a package... – wasamasa Jan 8 '16 at 16:29
  • I was thinking st like this had to exist. Will try to do it myself then. – The Unfun Cat Jan 8 '16 at 19:18

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 ()
  (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.

  • Does not work in spacemacs, but accepting all the same, since I specified evil :) – The Unfun Cat Mar 20 '16 at 11:40
  • works for me in latest spacemacs, dev branch. – Meitham Apr 4 '16 at 15:12

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."
  (evil-goto-mark ?\[)
  (evil-goto-mark ?\]))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.