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.

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

3 Answers 3

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.

2
  • 1
    Does not work in spacemacs, but accepting all the same, since I specified evil :) Mar 20, 2016 at 11:40
  • works for me in latest spacemacs, dev branch.
    – Meitham
    Apr 4, 2016 at 15:12
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 ?\]))
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)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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