(Summary)
I defined a Emacs Lisp function and assign it to the keyboard. When I invoked the function with keyboard shortcut, it runs but it doesn't work correctly.
(Detail)
I'm using Emacs on WSL 2 (Windows Subsystem for Linux 2) with Ubuntu.
- Windows: Microsoft Windows [Version 10.0.19042.1348]
- Ubuntu: Ubuntu 20.04.3 LTS
- Emacs=GNU Emacs 26.3
- X Server=VcXsrv 1.20.8.1
I want to copy and paste from Emacs to Windows and vice versa, but I cannot make it easily.
When you use Emacs, you copy selected string with M-w and paste C-y via Kill Ring. When you use Windows, you copy selected string with Ctrl+C and paste Ctrl+v via Windows Clipboard.These two spaces are different, so you cannot copy string from Emacs and paste to Windows and vice versa.
As a quick workaround, I defined two functions:
(defun wsl-paste ()
"copy a string from Windows using powershell Get-Clipboard"
(interactive)
(insert (shell-command-to-string "powershell.exe -command 'Get-Clipboard'")))
(global-set-key (kbd "C-c v") 'wsl-paste)
(defun wsl-copy (start end)
"copy a string from Emscs using Windows' clip.exe"
(interactive "r")
(copy-region-as-kill start end)
(shell-command-on-region start end "clip.exe"))
(global-set-key (kbd "C-c c") 'wsl-copy)
When I hit C-c v in an Emacs buffer, then the function wsl-paste will be invoked, and the string in the Windows Clipboard will be inserted in the Emacs buffer safely.
But when I selected some string in an Emacs buffer and hit C-c c, the function wsl-copy will be invoked, but nothing will be copied in the Windows Clipboard.
The strange thing is that when I enter the command M-x wsl-copy, then the function works safely and the selected region will be copied in the Windows Clipboard.
I checked M-x describe-bindings and confirmed that wsl-copy is binded to C-c c.
I inserted (message "started...") and (message "ended...") at the start and end of the function for debugging and confirmed both message has been shown in the *Messages* buffer.
How can I invoke wsl-copy with C-c c?