reddit / emacs
I did some hack to integrate emacs in wsl2 with the windows host
shelper9527 · 2022-05-12 14:49
shelper9527 · 2022-05-12 14:53
although almost everything works with emacs on windows, it is really slow. starting time, magit, file io, etc. just not bearable. so i did some hacks here to use emacs@wsl2 to meet my needs of editing any files on windows.
there are still couple of things need to be fixed:
1. file drag and drop between emacs@wsl2 and windows file explorer ( i am using org-download to do that on windows, but that does not work for emacs@wsl2)
2. share images/screenshots in the clipboard between windows and emacs@wsl2
Hope i can get some help here.. :)
lifer0se · 2022-05-12 15:30
Looks very promising!! Will give it a go at my work pc, see if I can finally switch from windows nvim to my emacs linux config.
NihilistDandy · 2022-05-12 17:20
On Win 11 I'm currently using a pgtk build natively in WSL2. I haven't tried to get org-protocol working, but I also didn't need to set up an X server or anything. I even get a Start menu item for it, so it feels quite seamless.
rahilarious · 2022-05-12 17:20
the website is soo cool...How is your experience with hugo or other generators that you tried?
explore212 · 2022-05-12 17:23
There is also a good app openinwsl in ms store to open any file from windows in emacs (or other wsl programs). It also has file extension associations mapping.
shelper9527 · 2022-05-12 17:32
i would like emacs to open anything, openinwsl is quite tedious setup for every file extension
shelper9527 · 2022-05-12 18:15
i use hugo with papermod theme, but i customized it a little bit, see my forked repo [here](https://github.com/shelper/hugo-PaperMod)
rahilarious · 2022-05-12 18:44
i saw that..have u tried any other generators? how is the comparision?
shelper9527 · 2022-05-12 19:00
used to use nikola... hugo is the fastest, and i really like it. you change something you see the result immediately.
TheSnowIsCold-46 · 2022-05-13 00:55
I used this that I cobbled together with the help of another resource online and it works well for point 2. Added it to my literate config when I detect I'm running emacs on WSL
pardon the poor formatting on mobile. Note this only works with PowerShell 5 and below because PowerShell core doesn't have the get-clipboard function for data other than text.
```
(defun my/get-image-clipboard-wsl-powershell()
"to simplify the logic, use c:/Users/Public as temporary directoy, and move it into current directoy"
(interactive)
(let* ((powershell "/mnt/c/Windows/System32/WindowsPowerShell/v1.0/powershell.exe")
(file-name (format-time-string "screenshot_%Y%m%d_%H%M%S.png"))
(file-path-wsl (concat "~/GDrive/emacs/org-download-images/" file-name))
)
(shell-command (concat powershell " -command \"(Get-Clipboard -Format Image).Save(\\\"C:/Users/Public/" file-name "\\\")\""))
(rename-file (concat "/mnt/c/Users/Public/" file-name) file-path-wsl)
(insert (concat "[[file:" file-path-wsl "]]"))
(message "insert DONE.")
))))
```
shelper9527 · 2022-05-13 13:37
I modified your function a little bit,
```lisp
(defun win2wsl-clipped-image()
"use powershell to save the clipped image to wsl and load it to xclip"
(interactive)
(let* ((powershell "/mnt/c/Windows/System32/WindowsPowerShell/v1.0/powershell.exe")
(file-name "//wsl$/Ubuntu/home/username/tmp/clip_win2wsl.png")
(file-name-wsl "~/tmp/clip_win2wsl.png")
)
(shell-command (concat powershell " -command \"(Get-Clipboard -Format Image).Save(\\\"" file-name "\\\")\""))
(shell-command (concat "xclip -selection clipboard -t image/png -i " file-name-wsl))
)
)
```
but i have an issue here, my second shell-command never returns, got message saying: "Waiting for process to die...", and have to C-g to quit.
I printed out the concatenated command and run it in my shell, everything works... any idea why is that?
Satiatetheground · 2022-05-13 13:49
I'm using Windows 11, WSL2 and GWSL
For point 2 I found that [wl-paste](https://github.com/bugaevc/wl-clipboard) seems to share the windows clipboard. Taking a screenshot and then in WSL
wl-paste > test.bmp
will save it out. I pair it with convert from ImageMagick to create png
wl-paste | convert bmp:- test.png
I have this in a shell script I use with org-download:
#!/bin/sh
wl-paste | convert bmp:- $1
In emacs:
(setq org-download-screenshot-method "wlpaste.sh %s")
TheSnowIsCold-46 · 2022-05-13 14:13
I tried using xclip previously but I think there was an issue with it's ability to get the windows clipboard. It could get the clipboard for anything displayed in VcXsrv, but anything outside those bounds would fail.
However, in this case, I don't think you need the xclip command at all since it would essentially be doing the same thing as the first powershell command. Instead of using xclip to get the screenshot (which as mentioned above seems to have issues gathering it from the windows host), we use Powershell since we have access to the exe on the windows host. Then we save the clipboard object as a file, and basically copy/cut it to WSL into our Org directory (or wherever) with the rename command. Then if you wanted to have emacs insert it at the point of your cursor, that is what the `(insert (concat "[[file:" file-path-wsl "]]"))` does. If you have `org-inline-display-images` it will pop up in your file or just show the link if you dont.
So in this case, I am not sure you need the xclip command at all since you would essentially be replicating what the powershell command is doing. I have the same issue when trying to use xclip (hence why I went the powershell route), it will freeze and corrupt the image if I try to use xclip and I have to send CtrlC to get control back.
shelper9527 · 2022-05-13 14:19
it does not work on wsl2 @ win10 with gwsl, 'cus that is not based on wayland. I dont have win11, but if that works for you , i think that might be you are using wslg for win11 which supports wayland.
haidongji · 2022-05-13 14:28
Did the tool bar icons look right? I built my own on Manjaro KDE (Arch distro) with pgtk, but the icons don't look right. There are actually no icons, except text buttons of "Copy" and such.
Normally I don't care about them, as they are hidden in regular usage. But I'll be doing C/C++ work soon, and wanted to use those tool bar icons with gdb.
Also, there might be a prebuilt gtk version from the distro's package management system. I noticed emacs-gtk is available for Ubuntu. On my Manjaro, I ended up using emacs-gcc-wayland-devel-bin from an AUR, which looks better than the one I built and installed, with proper tool bar icons. Feedback on prebuilt GTK version is welcome, if you have any.
Thanks!
shelper9527 · 2022-05-13 14:32
yeah, I just dont know why something like `(shell-command "xclip -selection clipboard -t image/png -i /home/username/tmp/clip\_win2wsl.png")` cannot work with emacs-lisp but works if i just run the command in the shell in my WSL2.
I am trying to set this up 'cus that is the simplest and most intuitive way to use it.
shelper9527 · 2022-05-13 14:47
Did some hack, inspired by comments from u/TheSnowIsCold-46, here is what i wrapped up, it may take \~3 seconds (i dont know why it is so slow, but since i dont do screenshot that often, it should be ok).
**UPDATE** it is faster after adding `-noprofile` option to powershell, code updated too.
```
(defun win2wsl-clipped-image()
"use powershell to save the clipped image to wsl and load it to xclip"
(let* ((powershell "/mnt/c/Windows/System32/WindowsPowerShell/v1.0/powershell.exe")
(file-name "//wsl$/Ubuntu/home/username/tmp/clip_win2wsl.png")
(file-name-wsl "~/tmp/clip_win2wsl.png")
)
(shell-command (concat powershell " -noprofile -command \"(Get-Clipboard -Format Image).Save(\\\"" file-name "\\\")\""))
(call-process-shell-command (concat "xclip -selection clipboard -t image/png -i " file-name-wsl))
)
)
(advice-add 'org-download-clipboard :before #'win2wsl-clipped-image)
```
will update my blog post as well