Skip to content

Instantly share code, notes, and snippets.

@ibarland
Last active July 24, 2022 03:02
Show Gist options
  • Save ibarland/25822900b01d7cd195e037dbfda159e9 to your computer and use it in GitHub Desktop.
Save ibarland/25822900b01d7cd195e037dbfda159e9 to your computer and use it in GitHub Desktop.
copy/paste for evil (emacs/vim) users
; Improve how OS's "copy" interacts with evil mode.
; We want yank'd/kill'd text to be added to the system clipboard, w/o explicit visual text-selection (via mouse-drag, say).
;
; The problem:
; While 'select-enable-clipboard' works find for non-evil emacs users,
; it interferes with evil/vim's `evil-change` and relatives:
; E.g. you want to replace a couple words with the clipboard,
; so you try typing "c2w cmd-V" -- BUT the c2w itself changes the clipboard
; (if `select-enable-clipboard` is set), so you end up pasting in exactly the two words
; you wanted to replace.
;
; Solution: have "cmd-C" be the *only* way that explicitly adds to the clipboard -- the most recent kill/yank.
; (So you still have to type cmd-C after a yank/kill, but at least you don't have to visually select the text-to-copy.)
;
; Note that 'paste' doesn't need any modification from default behavior, and 'cut' is moot when using evil.
; License: cc0-1.0, no rights reserved
;
; If you have improvements, please reply to this gist:
; https://gist.github.com/ibarland/25822900b01d7cd195e037dbfda159e9
; Include the following to .emacs, following `(require 'evil)`, of course:
(setq select-enable-clipboard nil) ; We choose *not* to auto-unify emacs' killbuffer w/ OS's clipboard (nil)
(defun copy-current-kill-to-clipboard ()
(interactive)
(gui-set-selection 'CLIPBOARD (current-kill 0))
)
(define-key evil-normal-state-map [4194403] 'copy-current-kill-to-clipboard)
(define-key evil-insert-state-map [4194403] 'copy-current-kill-to-clipboard)
; [4194403] refers to <apple>-c -- is macOS specific??
; For other OS's, if needed: run global-set-key interactively (typing the keystroke),
; and then C-x ESC ESC to see the last command.
; Things that didn't help me (but cost me a few hours of attempts):
;
; - Referring to cmd-C as "s-c" rather than `[4194403]`:
; (setq mac-command-modifier 'super)
; This is OS-independent,
; …BUT it seems to un-bind other command-keys?
;
; - binding the key in the global map:
; (define-key (current-global-map) [4194403] 'copy-current-kill-to-clipboard)
; No effect?! Maybe since already bound in minor-mode osx-key ?!
;
; - Following a different, easy solution suggested by Alexander Shukaev https://emacs.stackexchange.com/a/15054/14154
; (setq evil-visual-update-x-selection 'ignore)
; This seems to be a solution for a different, but closely-related?, problem; it didn't help me.
; If you have improvements (or fixes for the preceding points), please reply to this gist:
; https://gist.github.com/ibarland/25822900b01d7cd195e037dbfda159e9
;
; License: cc0-1.0, no rights reserved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment