3

I know that question has been asked dozens of times now, but i have tried every possible suggestion i have found online - nothing worked.

I can't figure out how can I copy/paste from my Windows 10 to emacs(which is run through "Bash on Ubuntu on Windows"). If i copy text on my Windows i cant paste it to emacs by clicking right button of my mouse. I have tried pasting by using Shift+Insert, but it keeps toggling "overwrite mode" and not pasting anything. I have tried unbinding overwrite mode but i couldnt achieve it.

I have also tried simpleclip package and it shows me "Clipboard support not available" whenever I try running "M+x simpleclip-copy/paste/cut".

Can anyone please help me?

tinlyx
  • 1,276
  • 1
  • 12
  • 27
Ilja Leiko
  • 133
  • 1
  • 4
  • You are probably after the key binding `C-y`, which is the `yank` command to insert from the kill-ring in emacs. – theldoria Mar 03 '18 at 11:02
  • If you would like to have a more windows like key binding, you can try adding `(cua-mode 1)` to your `init.el`. – theldoria Mar 03 '18 at 11:04
  • I am aware of C-y, which is the 'yank' from emacs clipboard. But i want to paste from my Windows 10 OS clipboard. Killing and yank within emacs works fine. But i need to bind keyboard to paste from Windows 10 OS clipboard to emacs(replication mouse-2 click) – Ilja Leiko Mar 03 '18 at 11:22
  • You need something on the Windows side which serves the x-clipboard. I have some experience with the [Cygwin XWin](https://www.cygwin.com/) server and the [XMing](http://www.straightrunning.com/XmingNotes/) server which is more lightweight. Both servers have a bridge from the windows clipboard to the x-clipboard. For the XWin server you need the command line option `-clipboard`. You can switch on clipboard integration of XMing by some entry in the application menu (it is easy). If you manage to get clipboard support working in the X-server you get it for free in emacs (in my experience). – Tobias Mar 03 '18 at 11:51
  • ... Note, if you want to use the X-server you need `export DISPLAY=:0.0` in `wbash`. From my experience do not waste time on the terminal version of emacs but aim for the X11 version. I don't have much experience with the terminal version but there are again and again problem reports with that version here on [emacs.stackexchange.com](https://emacs.stackexchange.com/questions/tagged/terminal-emacs). If the last two comments really pin-point your problem I re-write them as an answer. But, at first I will wait and see if it is really a solution. – Tobias Mar 03 '18 at 12:01
  • Thank you, Tobias. Seems like terminal version of emacs is not what i should be starting my emacs journey with :D Will be switching to a windows version instead. Thanks – Ilja Leiko Mar 03 '18 at 12:08
  • This might be relevant: https://github.com/Microsoft/WSL/issues/1069#issuecomment-265864419 I have no experience with getting terminal emacs to work with clipboard, as I never had the need to set that up, but this binary might be relevant. –  Mar 03 '18 at 12:34
  • @IljaLeiko Does windows version mean the X11-version or ntemacs? I tried to use `ntemacs` and missed the linux tools in emacs quite soon. All stuff like `mingw` is not the real thing. So I switched to Cygwin and now try WSL already for a while. If you use a minimal setup with Cygwin XWin and synchronize the directory structure of wsl and cygwin (e.g., by symbolic links) you can even use the toolset of cygwin such as `cygpath` and `cygstart`. It is cool that you can start windows applications from WSL even with `call-process` or `start-process`! XMing setup is simpler on the other hand. – Tobias Mar 03 '18 at 12:53
  • What are your values of `interprogram-paste-function` and `save-interprogram-paste-before-kill`? Mine are `x-get-selection-value` and `t`, respectively. – Drew Mar 03 '18 at 16:29
  • I'm sure there's a way to do it, but my recommendation would be to run a native Emacs. I don't know if emacsclient will run cross-OS out of the box, but it would be easier than dealing with all the shortcomings of running a terminal Emacs under Windows. – Gilles 'SO- stop being evil' Mar 04 '18 at 09:40
  • @Gilles I am happily using `emacsclient-w32.exe` of cygwin over TCP with emacs server on WSL side. I very much appreciate the huge effort for Cygwin but starting processes under cygwin is slow. `magit` is not usable with large projects under Cygwin. WSL is better in that aspect. The problem is not a fault of the cygwin people but it is just intrinsic to Windows (an often discussed issue). I very much want Cygwin to live on since no outsider knows what the motivation of the Windows folks really is with WSL. From earlier experience it may be that they just want to crush something else with WSL. – Tobias Mar 04 '18 at 22:28

2 Answers2

2

To answer your question, try this:

  • mouse to the title bar (to the right of the red Ubuntu logo)
  • right click, and you should see a menu appear
  • mouse over Edit, move to the right, click on Paste

If you find it's doing unexpected indenting, you may want to put emacs in fundamental-mode (esc x fundamental-mode). This is inconvenient, so if you expect to do this often, you should use an X server such as MobaXTerm or XMing, and before running emacs you need to set the environment variable DISPLAY=":0"

Ben
  • 21
  • 3
2

This answer is adapted from a post on Reddit.

You can define your own wsl-copy and wsl-paste functions by putting the following in your Emacs init file (.emacs):

; wsl-copy
(defun wsl-copy (start end)
  (interactive "r")
  (shell-command-on-region start end "clip.exe")
  (deactivate-mark))

; wsl-paste
(defun wsl-paste ()
  (interactive)
  (let ((clipboard
     (shell-command-to-string "powershell.exe -command 'Get-Clipboard' 2> /dev/null")))
    (setq clipboard (replace-regexp-in-string "\r" "" clipboard)) ; Remove Windows ^M characters
    (setq clipboard (substring clipboard 0 -1)) ; Remove newline added by Powershell
    (insert clipboard)))

To bind them to C-c C-c and C-c C-v, respectively, add this:

; Bind wsl-copy to C-c C-v
(global-set-key
 (kbd "C-c C-c")
 'wsl-copy)

; Bind wsl-paste to C-c C-v
(global-set-key
 (kbd "C-c C-v")
 'wsl-paste)
Riggy
  • 21
  • 3
  • This looks very much like it should be added to GNU ELPA's `xclip.el` package. I thankfully don't use Windows so I can't do it myself, but if you're interested in helping me by testing my code, please contact me by email. – Stefan Jul 13 '20 at 14:50
  • @Stefan, I can't find your email on your profile. I've added my email address temporarily to my profile so you can email me. I would be happy to test xclip.el on Windows. – Riggy Jul 25 '20 at 23:25
  • It's monnier@iro.umontreal.ca – Stefan Jul 26 '20 at 00:46