21

Setup:

GUI version GNU Emacs 25.0.50.1 (x86_64-w64-mingw32) of 2015-07-25 on KAEL Compiled from EmacsW64.

Package Evil, using the latest version of Evil from MELPA

Windows 7 x64 bit.

Situation:

Tested with empty Emacs configuration with only Evil enabled. I copy some text from Windows application. Then I paste it into Emacs inside normal mode with p. I see the text.

Then I switch to the Windows application, select another text, copy it. Switch back to Emacs, visually select the text, and paste inside visual mode. But the text inside the region will not be replaced with the text from the Windows clipboard.

This is not with the default Vim. How could I configure it, that the visually text gets replaced by the latest copy action, in this case the Windows clipboard?

7
  • Probable duplicates: Add operating system clipboard to kill ring, How to access os clipboard using Emacs + Evil.
    – Dan
    Commented Aug 21, 2015 at 21:52
  • 1
    I'm aware of both topics, but I think it's more Evil related than clipboard. The clipboard function works fine inside normal mode. And not for visual mode.
    – ReneFroger
    Commented Aug 21, 2015 at 21:53
  • Ah, I see what you're saying. For what it's worth, it's not Windows-specific: it also does not yank from the OS clipboard in visual state on Linux (Fedora, if it matters).
    – Dan
    Commented Aug 21, 2015 at 21:55
  • Thanks for the update, I changed the title to not be OS-specific.
    – ReneFroger
    Commented Aug 21, 2015 at 23:24
  • Is this just a plain Evil bug? Any chance you can report it?
    – PythonNut
    Commented Aug 22, 2015 at 0:52

1 Answer 1

30

There is no bug here. Since I was also annoyed with this behavior, I just read Evil code to find out why this is happening. So, here is a straight copy/paste of the well-commented one-liner from my Emacs configuration that fixes this issue:

;; Imagine the following scenario.  One wants to paste some previously copied
;; (from application other than Emacs) text to the system's clipboard in place
;; of some contiguous block of text in a buffer.  Hence, one switches to
;; `evil-visual-state' and selects the corresponding block of text to be
;; replaced.  However, one either pastes some (previously killed) text from
;; `kill-ring' or (if `kill-ring' is empty) receives the error: "Kill ring is
;; empty"; see `evil-visual-paste' and `current-kill' respectively.  The
;; reason why `current-kill' does not return the desired text from the
;; system's clipboard is because `evil-visual-update-x-selection' is being run
;; by `evil-visual-pre-command' before `evil-visual-paste'.  That is
;; `x-select-text' is being run (by `evil-visual-update-x-selection') before
;; `evil-visual-paste'.  As a result, `x-select-text' copies the selected
;; block of text to the system's clipboard as long as
;; `x-select-enable-clipboard' is non-nil (and in this scenario we assume that
;; it is).  According to the documentation of `interprogram-paste-function',
;; it should not return the text from the system's clipboard if it was last
;; provided by Emacs (e.g. with `x-select-text').  Thus, one ends up with the
;; problem described above.  To solve it, simply make
;; `evil-visual-update-x-selection' do nothing:
;; (fset 'evil-visual-update-x-selection 'ignore)

The last sentence is the answer to the question of how to "configure it, that the visually text gets replaced by the latest copy action, in this case the Windows clipboard?"

(fset 'evil-visual-update-x-selection 'ignore)

Enjoy.

6
  • Thanks Alexander, I also enjoyed your compiled Harroogan's Emacs. Nice find, I marked your answer as the right one. As it seems, I can only upvote and mark it as right without any bounty. Which is sad, you really deserved it. :-)
    – ReneFroger
    Commented Aug 26, 2015 at 6:35
  • 1
    Man this bug! It annoyed me to that degree that I switched back to vim just to get rid of this behaviour. I searched everywhere and also reported to the evil tracker but sadly no-one responded to my issue. Alexander, you are my hero!
    – patchrail
    Commented Sep 3, 2015 at 9:18
  • 1
    NICE! This should be a default in spacemacs. Commented Oct 24, 2015 at 4:16
  • It seems sneaky how this behavior is on by default. Took me a while to realize what was happening, and the fact that there is no way to disable it via setting a variable. This workaround works well :) Commented Jun 8, 2016 at 3:09
  • This has been annoying me for way too long. Seems like an undesirable default. No?
    – wpcarro
    Commented Nov 9, 2021 at 5:36

Your Answer

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

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