Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to yank / delete and *not* overwrite clipboard #5750

Closed
hnry opened this issue Apr 11, 2016 · 18 comments
Closed

Ability to yank / delete and *not* overwrite clipboard #5750

hnry opened this issue Apr 11, 2016 · 18 comments

Comments

@hnry
Copy link

hnry commented Apr 11, 2016

Often I do things in this order ->

  1. Copy text outside emacs
  2. Switch back to emacs, delete some text
  3. Paste
    Problem is I get back the deleted text since it overwrites the clipboard.

I want to just keep the two things completely separate. vim commands/emacs will deal with only the ring buffer stuff, my clipboard is untouched. That way I can setup a explicit keybinding when I actually do want to copy over something to my clipboard like with c-c (win) or s-c (mac).

How can I do this? I tried in my .spacemacs file:

  (setq save-interprogram-paste-before-kill t)
  (setq x-select-enable-clipboard nil)

But it's not working for me. :(

@StreakyCobra
Copy link
Contributor

According to this thread the (setq save-interprogram-paste-before-kill t) solution should be the correct one, and Spacemacs is already doing it.

The second problem being that selecting a text to delete it yanks it to the system-clipboard. To avoid this you have to use (setq x-select-enable-clipboard nil) as you did.

With this you'll have to separate clipboards, one for emacs, one for the system. But as now the two clipboards are separated, emacs command will use the emacs one, meaning that to get the content of your system clipboard you'll have to explicitly specify the system one:

  1. Copy text A outside Emacs
  2. Switch to Emacs
  3. Select text B
  4. Delete the B text
  5. The Emacs clipboard contains B, the system clipboard contains A
  6. Paste the B from Emacs clipboard by pressing p, paste the A from system clipboard by pressing "*p.

So with the split activated, you'll need to use "* in front of p and y to explicitly use the system clipboard.


Note: It is possible to achieve what you want without this tweak by only selecting the text in Emacs (without deleting it):

  1. Copy text outside Emacs
  2. Switch back to Emacs
  3. Select some text (do not delete it)
  4. Paste

@hnry hnry changed the title Yanking or deleting does *not* overwrite clipboard Ability to yank / delete and *not* overwrite clipboard Apr 11, 2016
@hnry
Copy link
Author

hnry commented Apr 11, 2016

Changed title to be more specific

@StreakyCobra I saw that link and I understand the 2nd method you mentioned. Also there's "_ + command. So I mostly do one of those, don't delete / yank instead do it after or highlight over, or use the "_ followed by the yank key.

Can't seem to get your first method to work, when I do "*p I get "Indenting region...done" and not a paste.

If I could get your first method to work I think that would be great. Leaving all the vim commands to it's own emacs clipboard. And having the default system clipboard shortcuts be just for systems. And if your method worked then I could just bind ⌘V to it 😀

@StreakyCobra
Copy link
Contributor

Can you paste here the result of SPC h d s?

@hnry
Copy link
Author

hnry commented Apr 11, 2016

System Info

  • OS: darwin
  • Emacs: 24.5.1
  • Spacemacs: 0.105.18
  • Spacemacs branch: master (rev. c8c4ff3)
  • Graphic display: t
  • Distribution: spacemacs
  • Editing style: vim
  • Completion: helm
  • Layers:
(auto-completion emacs-lisp git markdown clojure ocaml javascript html python react version-control)

@StreakyCobra
Copy link
Contributor

Can you try "+y maybe? ("+p for pasting)

@hnry
Copy link
Author

hnry commented Apr 11, 2016

That pastes, it does the same as p right now, because I notice that even though I have
(setq x-select-enable-clipboard nil) set in .spacemacs under dotspacemacs/user-config when I yank things, it will still transfer to my system clipboard.

So even if "+p is working, it would just paste back what I yanked since it keeps going to my system clipboard whenever I yank?

@StreakyCobra
Copy link
Contributor

+ act on the primary clipboard, not the system clipboard (*), so it's a little bit different. I just wanted to see if using register was working, it seems so. For me it looks like an OS X issue. I read there are some issues regarding the different Emacs' build for OS X.

I can't help further, so trying to ping an OS X guy: @d12frosted

@hnry
Copy link
Author

hnry commented Apr 11, 2016

@StreakyCobra ok, thank you for your help so far though, much appreciated.

I'll also try it on other setup / systems. (Though I'm not sure how many people will want to set this up similarly in the end, doing one of the other methods mentioned seems reasonable if there's no easy solution, or maybe there already is, just my build is messed up 😆 ).

@punassuming
Copy link
Contributor

@hnry, try using the black hole register, it doesn't affect the kill ring. "_d

@hnry
Copy link
Author

hnry commented Apr 27, 2016

thanks for all the inputs

i guess it's time to close this now as it's not a bug or a feature request

@hnry hnry closed this as completed Apr 27, 2016
@gelisam
Copy link

gelisam commented Feb 21, 2017

In case anybody else wants this, I got it to work on my machine using

(defun dotspacemacs/user-config ()
  (setq x-select-enable-clipboard nil)
  (define-key evil-visual-state-map (kbd "s-c") (kbd "\"+y"))
  (define-key evil-insert-state-map  (kbd "s-v") (kbd "^R+"))
  (define-key evil-ex-completion-map (kbd "s-v") (kbd "^R+"))
  (define-key evil-ex-search-keymap  (kbd "s-v") (kbd "^R+")))

This separates the two clipboards, binds ⌘C to "yank to OS X clipboard", and binds ⌘V to "paste from OS X clipboard".

@jeffmacdonald
Copy link

@gelisam When I tried this.. ⌘v actually inserts the characters ^R+ I copied and pasted (oh the irony) from your comment.

@gelisam
Copy link

gelisam commented Apr 20, 2017

Note that the "^R" is a single character, produced by typing CTRL+V CTRL+R, not two characters produced by typing SHIFT+6 R.

sgeb added a commit to sgeb/spacemacs that referenced this issue Aug 3, 2017
This commit adds a new option `osx-use-separate-clipboards` to the `osx`
layer. When set to non nil, the layer treats the OSX pasteboard separate
from the spacemacs kill-ring. Default is `nil`, which is the same
behaviour as without this commit, hence not expecting any breaking
changes.

Fixes syl20bnr#6533 syl20bnr#5750
sgeb added a commit to sgeb/spacemacs that referenced this issue Aug 3, 2017
This commit adds a new option `osx-use-separate-clipboards` to the `osx`
layer. When set to non nil, the layer treats the OSX pasteboard separate
from the spacemacs kill-ring. Default is `nil`, which is the same
behaviour as without this commit, hence not expecting any breaking
changes.

Fixes syl20bnr#6533 syl20bnr#5750
sgeb added a commit to sgeb/spacemacs that referenced this issue Aug 3, 2017
This commit adds a new option `osx-use-separate-clipboards` to the `osx`
layer. When set to non nil, the layer treats the OSX pasteboard separate
from the spacemacs kill-ring. Default is `nil`, which is the same
behaviour as without this commit, hence not expecting any breaking
changes.

This commit also includes changes to the README file, in response to a
previously failing test (document must have a `Features:` section).

Fixes syl20bnr#6533
Fixes syl20bnr#5750
@RobertARandolph
Copy link

This seems to be linked from a couple of external places. Here's what you can copy directly to your config file to solve the OP:

(setq x-select-enable-clipboard nil)
(define-key evil-visual-state-map  (kbd "s-c") (kbd "\"+y"))
(define-key evil-insert-state-map  (kbd "s-v") (kbd "C-r +"))
(define-key evil-ex-completion-map (kbd "s-v") (kbd "C-r +"))
(define-key evil-normal-state-map  (kbd "s-v") (kbd "\"+p"))
(define-key evil-ex-search-keymap  (kbd "s-v") (kbd "C-r +"))

If you're reading this and not using spacemacs (such as me), also add:

(setq x-select-enable-clipboard nil)

This will change super-c (cmd, win) to only copy to system clipboard with an active selection, and super-v (cmd, win) to always paste.

Changing/deleting/yanking text will not clobber your default clipboard.

@polothy
Copy link

polothy commented May 30, 2019

Maybe this will help someone... I couldn't get the above suggestions to work well, so what I did was enable the xclipboard layer (maybe develop branch only layer). Then disable using system clipboard with:

  (setq select-enable-clipboard nil)

That works great, but you can no longer paste while in insert mode, so you can add these lines:

  (global-set-key (kbd-mac-command "v") 'spacemacs/xclipboard-paste)
  (global-set-key (kbd-mac-command "c") 'spacemacs/xclipboard-copy)

This works as I had hoped so far. Couldn't figure out how to do cut (cmd+x), but I don't use it much. Was thinking you could bind it to spacemacs/xclipboard-copy and also evil-delete or similar, but I'm not sure how to call multiple functions properly.

@jZhangTk
Copy link
Contributor

I can't make the above solutions work for me all the time. Then I found this package https://github.com/rolandwalker/simpleclip. It hasn't failed on me so far.

@Patrick0308
Copy link

In case anybody else wants this, I got it to work on my machine using

(defun dotspacemacs/user-config ()
  (setq x-select-enable-clipboard nil)
  (define-key evil-visual-state-map (kbd "s-c") (kbd "\"+y"))
  (define-key evil-insert-state-map  (kbd "s-v") (kbd "^R+"))
  (define-key evil-ex-completion-map (kbd "s-v") (kbd "^R+"))
  (define-key evil-ex-search-keymap  (kbd "s-v") (kbd "^R+")))

This separates the two clipboards, binds ⌘C to "yank to OS X clipboard", and binds ⌘V to "paste from OS X clipboard".

It is not work in emacs -nw.

@LeaveNhA
Copy link

LeaveNhA commented Jan 20, 2024

In case anybody else wants this, I got it to work on my machine using

(defun dotspacemacs/user-config ()
  (setq x-select-enable-clipboard nil)
  (define-key evil-visual-state-map (kbd "s-c") (kbd "\"+y"))
  (define-key evil-insert-state-map  (kbd "s-v") (kbd "^R+"))
  (define-key evil-ex-completion-map (kbd "s-v") (kbd "^R+"))
  (define-key evil-ex-search-keymap  (kbd "s-v") (kbd "^R+")))

This separates the two clipboards, binds ⌘C to "yank to OS X clipboard", and binds ⌘V to "paste from OS X clipboard".

It is not work in emacs -nw.

I can confirm that this doesn't work under terminal. Any solution?

System Info 💻

  • OS: darwin
  • Emacs: 29.1
  • Spacemacs: 0.999.0
  • Spacemacs branch: develop (rev. 3d84b01)
  • Graphic display: nil
  • Running in daemon: nil
  • Distribution: spacemacs
  • Editing style: vim
  • Completion: helm
  • Layers:
(react ruby purescript phalcon sql
       (python :variables python-backend 'lsp python-lsp-server 'pylsp python-test-runner 'pytest)
       yaml fsharp remote-sync
       (typescript :variables node-add-modules-path t typescript-fmt-tool 'prettier)
       clojure html javascript
       (unicode-fonts :variables unicode-fonts-force-multi-color-on-mac t)
       (multiple-cursors :variables multiple-cursors-backend 'evil-mc)
       (osx :variables osx-option-as 'none)
       auto-completion better-defaults emacs-lisp prettier git helm csharp prolog markdown multiple-cursors org
       (xclipboard :variables xclipboard-enable-cliphist t)
       (shell :variables shell-default-height 30 shell-default-position 'bottom)
       syntax-checking version-control treemacs)
  • System configuration features: ACL GMP GNUTLS JSON LIBXML2 MODULES NOTIFY KQUEUE PDUMPER SQLITE3 THREADS TREE_SITTER ZLIB

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

10 participants