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

yank to system clipboard #2222

Closed
dzhwinter opened this issue Jul 4, 2015 · 31 comments
Closed

yank to system clipboard #2222

dzhwinter opened this issue Jul 4, 2015 · 31 comments
Labels

Comments

@dzhwinter
Copy link

i use suggestion in #1504 , "*y for cut text into system clipboard, but it run in wrong behaviour. And pop up an message "x-set-selection: Symbol's funtion definition is void: x-own-selection-internal "
anyone have ideas?

@dzhwinter
Copy link
Author

emacs 24.5
ubuntu 14.04
spacemacs 1.01.3
nothing else in message buffer.
thanks advance

@dzhwinter
Copy link
Author

sorry but, is there any progress in yank context into X-clipboard?
i update my spacemacs to 0.103 bump v2.
It still just pop a message "x-set-selection: Symbol's funtion definition is void: x-own-selection-internal " and yank nothing in my clipboard.
i found what is going wrong.
i use emacs 24.5-nox in terminal (ssh to server ), of course emacs has no knowledge about X11 system clipboard.
As a result, unless use third tool like 'xclip' here. And i found someone has done that. just use xclip package.

@StreakyCobra
Copy link
Contributor

@dzhwinter Are you more lucky with the last spacemacs release about this?

Normally y and p use directly the system clipboard, IIRC, except if you are using emacs in terminal.

@dzhwinter
Copy link
Author

i have solved that.
I am using emacs in terminal. In fact, I my daily work, i always connect to server with ssh and I have no choice but emacs-nox (with GUI always failed and freeze window because of the unstable connection).
I use xsel with this config for clip and paste content in terminal. For who search here.

(defun configlayer/init-xclip ()
  (use-package xclip
    :defer t
    :init
    (defun copy-to-clipboard ()
      "Copies selection to x-clipboard."
      (interactive)
      (if (display-graphic-p)
          (progn
            (message "Yanked region to x-clipboard!")
            (call-interactively 'clipboard-kill-ring-save)
            )
        (if (region-active-p)
            (progn
              (shell-command-on-region (region-beginning) (region-end) "xsel -i -b")
              (message "Yanked region to clipboard!")
              (deactivate-mark))
          (message "No region active; can't yank to clipboard!")))
      )

    (defun paste-from-clipboard ()
      "Pastes from x-clipboard."
      (interactive)
      (if (display-graphic-p)
          (progn
            (clipboard-yank)
            (message "graphics active")
            )
        (insert (shell-command-to-string "xsel -o -b"))
        )
      )

@robbyoconnor
Copy link
Contributor

It works fine for me without this change...

@nightscape
Copy link

@dzhwinter sorry for that silly question, Emacs noob here.
Where does one put that code snippet? At the end of ~/.spacemacs?
And does one have to activate it manually somehow? To which keys is it bound then?

@nightscape
Copy link

nightscape commented Apr 22, 2016

Ok, I think I can answer this myself now:
I put the above code snippet in .spacemacs in the section (defun dotspacemacs/user-config () ...) and added two lines from #2347

(defun dotspacemacs/user-config ()

    ; ...

    (defun copy-to-clipboard ()
      "Copies selection to x-clipboard."
      (interactive)
      (if (display-graphic-p)
          (progn
            (message "Yanked region to x-clipboard!")
            (call-interactively 'clipboard-kill-ring-save)
            )
        (if (region-active-p)
            (progn
              (shell-command-on-region (region-beginning) (region-end) "xsel -i -b")
              (message "Yanked region to clipboard!")
              (deactivate-mark))
          (message "No region active; can't yank to clipboard!")))
      )

    (defun paste-from-clipboard ()
      "Pastes from x-clipboard."
      (interactive)
      (if (display-graphic-p)
          (progn
            (clipboard-yank)
            (message "graphics active")
            )
        (insert (shell-command-to-string "xsel -o -b"))
        )
      )
  (evil-leader/set-key "o y" 'copy-to-clipboard)
  (evil-leader/set-key "o p" 'paste-from-clipboard)
)

Now when I press SPC there is a new o → +prefix entry in the menu which contains p → paste-from-clipboard y → copy-to-clipboard.
So SPC o y copies the selection to the system clipboard and SPC o p pastes the system clipboards contents at the current cursor position.
Nice!!

@qiuwei
Copy link

qiuwei commented Aug 24, 2016

@nightscape 's code snippets work great for me.

I only need to use pbcopy/pbpaste instead of xsel to make it work on a Mac.

Thanks.

@ninrod
Copy link

ninrod commented Oct 5, 2016

@qiuwei have you managed to integrate pbcopy within the above snippet? thanks in advance.

@qiuwei
Copy link

qiuwei commented Oct 6, 2016

@ninrod yes, pbcopy and pbpaste work great on my mac.

@ninrod
Copy link

ninrod commented Oct 6, 2016

nice. just for posterity's sake, if you want to make it work for mac, it is easy enough: (you could even query the system type and make it work anywhere).

(defun dotspacemacs/user-config ()

    ; ...

    (defun copy-to-clipboard ()
      "Copies selection to x-clipboard."
      (interactive)
      (if (display-graphic-p)
          (progn
            (message "Yanked region to x-clipboard!")
            (call-interactively 'clipboard-kill-ring-save)
            )
        (if (region-active-p)
            (progn
              (shell-command-on-region (region-beginning) (region-end) "pbpaste")
              (message "Yanked region to clipboard!")
              (deactivate-mark))
          (message "No region active; can't yank to clipboard!")))
      )

    (defun paste-from-clipboard ()
      "Pastes from x-clipboard."
      (interactive)
      (if (display-graphic-p)
          (progn
            (clipboard-yank)
            (message "graphics active")
            )
        (insert (shell-command-to-string "pbpaste"))
        )
      )
  (evil-leader/set-key "o y" 'copy-to-clipboard)
  (evil-leader/set-key "o p" 'paste-from-clipboard)
)

@andreicristianpetcu
Copy link

Can this be added to Spacemacs? I think this should be a builtin feature.

@aniketd
Copy link

aniketd commented Oct 19, 2016

This feature is a basic need for sure.

@TheBB
Copy link
Collaborator

TheBB commented Oct 20, 2016

Just make a PR!

@ghost
Copy link

ghost commented Dec 27, 2016

The snippet @ninrod posted has a little error in the copy-to-clipboard function, it calls pbpaste instead of pbcopy. The fixed version would be:

(defun dotspacemacs/user-config ()

    ; ...

    (defun copy-to-clipboard ()
      "Copies selection to x-clipboard."
      (interactive)
      (if (display-graphic-p)
          (progn
            (message "Yanked region to x-clipboard!")
            (call-interactively 'clipboard-kill-ring-save)
            )
        (if (region-active-p)
            (progn
              (shell-command-on-region (region-beginning) (region-end) "pbcopy")
              (message "Yanked region to clipboard!")
              (deactivate-mark))
          (message "No region active; can't yank to clipboard!")))
      )

    (defun paste-from-clipboard ()
      "Pastes from x-clipboard."
      (interactive)
      (if (display-graphic-p)
          (progn
            (clipboard-yank)
            (message "graphics active")
            )
        (insert (shell-command-to-string "pbpaste"))
        )
      )
  (evil-leader/set-key "o y" 'copy-to-clipboard)
  (evil-leader/set-key "o p" 'paste-from-clipboard)
)

@robbyoconnor
Copy link
Contributor

robbyoconnor commented Dec 27, 2016 via email

@ghost
Copy link

ghost commented Dec 27, 2016

That's weird, I don't know how that can be possible if it is calling pbpaste in both functions:

I changed this:

(shell-command-on-region (region-beginning) (region-end) "pbpaste")

to this:

(shell-command-on-region (region-beginning) (region-end) "pbcopy")

Just in case someone else has the same problem. I needed this for me because I was unable to copy/paste and the solution posted before wasn't working.

@robbyoconnor
Copy link
Contributor

I didn't have to do anything... What OS?

@ghost
Copy link

ghost commented Dec 27, 2016

MacOS using Terminal.


Here is the story:

Previously, using plain Emacs I just used cmd+c to copy from Emacs to other applications and cmd+v to paste from other applications, that was possible because of Terminal and not Emacs itself. But those weren't working with Spacemacs. c-y and m-y work only within Spacemacs but not with other applications.

I think it would work as before by disabling xterm-mouse-mode but I kind of like that feature now. So I had to find a way to copy/paste to/from other applications, and this is the thing that worked after changing the command mentioned in my previous comment. I also changed the keybindings to:

(global-set-key (kbd "M-w") 'copy-to-clipboard)
(global-set-key (kbd "C-y") 'paste-from-clipboard)

Since I just started to use Emacs I don't know if that will have side-effects, but is working well so far.


But maybe all this is because I'm doing something wrong, how do you do copy and paste usually?

@robbyoconnor
Copy link
Contributor

robbyoconnor commented Dec 27, 2016 via email

@andreicristianpetcu
Copy link

@jameskolce M-w and C-y seem totally non-intuitive. Y is associated with yank (copy) not paste and you have M- and C- for copy and paste why not C-M-c and C-M-p? Control+alt+c/p from copy/paste?

@ghost
Copy link

ghost commented Dec 27, 2016

@andreicristianpetcu I got those from this table of EmacsWiki:

image

Also, those are the commands I have been using in Emacs before installing Spacemacs.

I just checked the Yank term and Wikipedia says:

Yank, the name for the copy command in vi and the paste command in Emacs, two commonly used UNIX/Linux text editors

So perhaps you are associating it with Vi (or Vi mode) instead of Emacs?

C-M-c and C-M-P also make sense to me though, so I might consider changing at some point, although I also like that the default key combinations of Emacs work with the clipboard.

But as I said, I'm just starting to use Emacs and I still have a lot to learn, so I appreciate the suggestions.

@andreicristianpetcu
Copy link

Sorry for the misinformation. I never used vanilla Emacs and I started using Spacemacs from the start. I used Vim and Neovim before Spacemacs and that's where the confusion over yank comes from :) I see your point and your proposal sounds fine.

@ghost
Copy link

ghost commented Dec 27, 2016

Alright thanks, and no worries!

@quangv
Copy link

quangv commented Apr 17, 2018

fyi I could not get above to work (using Xshell on Windows 10, connecting to Ubuntu)

But this worked for me.

(xterm-mouse-mode -1)

https://stackoverflow.com/questions/37631440/spacemacs-using-the-mouse-to-copy-paste

@goetzc
Copy link

goetzc commented Sep 1, 2018

There is this other option from @gelisam, which works great:
#5750 (comment)

@cweill
Copy link
Contributor

cweill commented Oct 10, 2018

Try the xclipboard layer in the develop branch.

@drewLough
Copy link

drewLough commented Jan 27, 2019

I wasn't able to 'copy' (in emacs lingo: kill-ring-save) from the terminal (opened with M-') and paste into other programs (or buffers). However I was able to accomplish it by marking the desired text and use middle-mouse button. It pastes (ie. yanks) the marked text to the terminal prompt, but it also makes it paste-able both in and outside of emacs. hopefully this helps someone who was looking for a quick and dirty(?) way to copy/paste to-and-from the spacemacs terminal. I'm also using wayland, but I'm not sure if that makes a difference.

@bigfish
Copy link

bigfish commented Feb 10, 2019

in Ubuntu,
sudo apt install xsel

in .spacemacs, I added 'xclip' package to list of additional packages, &
(xclip-mode 1)
in dotspacemacs/user-config fn.

Now I can paste into emacs buffer from system clipboard with 'p' in evil mode (normal).
And anything I yank ('y') I can paste into other apps.

@biocyberman
Copy link

biocyberman commented Apr 9, 2019

Finally this itching problem is solved. This is working code with OS check for Linux and MacOS. I don't use Windows so I don't know how it works

(cond
 ;; OS X
 ((string-equal system-type "darwin") ; Mac OS X
  (progn
    (setq save-to-clipboard-cmd "pbcopy")
    (setq paste-from-clipboard-cmd "pbpaste")
    )
  )
 ;; Linux
 ((string-equal system-type "gnu/linux") ; linux
  (progn
    (setq save-to-clipboard-cmd "xsel -i -b")
    (setq paste-from-clipboard-cmd "xsel -o -b")
    )
  )
 )

(defun copy-to-clipboard ()
  "Copies selection to x-clipboard."
  (interactive)
  (if (display-graphic-p)
      (progn
        (message "Yanked region to x-clipboard!")
        (call-interactively 'clipboard-kill-ring-save)
        )
    (if (region-active-p)
        (progn
          (shell-command-on-region (region-beginning) (region-end) save-to-clipboard-cmd)
          (message "Yanked region to clipboard!")
          (deactivate-mark))
      (message "No region active; can't yank to clipboard!")))
  )

(defun paste-from-clipboard ()
  "Pastes from x-clipboard."
  (interactive)
  (if (display-graphic-p)
      (progn
        (clipboard-yank)
        (message "graphics active")
        )
    (insert (shell-command-to-string paste-from-clipboard-cmd))
    )
  )
(evil-leader/set-key "o y" 'copy-to-clipboard)
(evil-leader/set-key "o p" 'paste-from-clipboard)

@yanowitz
Copy link

yanowitz commented Jul 17, 2019

I've done this integration in a different way, dropping it here for posterity (also, I can't remember if I wrote this many years ago or got it from someone else). I want any cutting and yanking to work with pbcopy/pbpaste (on my mac). I'm new to Spacemacs, but I've used this with emacs (in tty mode) on my mac for years. YMMV. I prefer this to be seamless without my thinking about it.

Obviously, you can just wire up copy or paste if you don't want both. And I leave it as an exercise for the ready to extend this with xsel (see above)

  (defun dotspacemacs/user-config ()
;...
    (if (eq system-type 'darwin)
        (progn
          (defun mac-copy ()
            (shell-command-to-string "pbpaste"))

          (defun mac-paste (text &optional push)
            (let ((process-connection-type nil))
              (let ((proc (start-process "pbcopy" "*Messages*" "pbcopy")))
                (process-send-string proc text)
                (process-send-eof proc))))

          (setq interprogram-cut-function 'mac-paste)
          (setq interprogram-paste-function 'mac-copy)
          ))
  )

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

No branches or pull requests