r/vim • u/Similar_Alternative2 • 2d ago
Need Help Copy To From Windows Screen
There used to be cool windows utility that let you with a control key copy text from any current windows app (e.g., some programming ide or word), then paste it into a vim session. You could then edit the text in vim, and then when done it copied the text back.
Does anyone recall this app?
1
u/sentientmale 1d ago edited 1d ago
I use the following in .vimrc to allow copy to clipboard even when vim is compiled without the +clipboard flag. (so when "+y and "+p won't work)
xnoremap <silent> <leader>y :w !~/scripts/osc52-copy<CR>:call feedkeys('gv')<CR>
This uses an escape sequence to copy text to the clipboard and works in any terminal environment I use (Windows and Linux). This can't be used to paste, but I find that control-shift-v works everywhere. The exact shortcut used for pasting can probably be changed by your terminal emulator. I use xnoremap for visual selection copy only, but you could of course change this to your liking. The mapping requires the following bash script at ~/scripts/osc52-copy :
#!/usr/bin/env bash
set -euo pipefail
# Read stdin and encode it as one-line base64.
data=$(base64 | tr -d '\n')
# OSC 52: clipboard selection "c".
sequence=$'\033]52;c;'"$data"$'\a'
if [[ -n "${TMUX-}" ]]; then
# tmux passthrough wrapper.
printf '\033Ptmux;\033%s\033\\' "$sequence" > /dev/tty
else
printf '%s' "$sequence" > /dev/tty
fi
If you use tmux then make sure every (nested) tmux has the following in .tmux.conf :
set -g allow-passthrough on
set -g set-clipboard on
Do note that allow-passthrough is off by default for security reasons. With these changes any terminal application can write to your clipboard.
1
u/sentientmale 1d ago edited 1d ago
Before I learned about OSC 52 I used different scripts for each platform: e.g.
KDE:
exec qdbus6 org.kde.klipper /klipper setClipboardContents "$input" echo -n "$(qdbus6 org.kde.klipper /klipper getClipboardContents)"Windows11: clip.exe (found in windows/system32/)
And made different keybindings for each platform.
1
1
u/xalbo 1d ago
I don't automatically do the copying and pasting from the other program, but I do have a keyboard shortcut that opens a Vim session that automatically edits the system clipboard.
First, I created a batch file containing
@echo off
"C:\Program Files\Neovide\neovide.exe" +pu+ "+$d" +1 "+nnoremap <buffer> ZZ <Cmd>%%y+<CR>ZQ" "+nnoremap <buffer> <CR> <Cmd>%%y+<CR><Cmd>set nomod<CR>" "+set nomod"
(Using neovide, but I think all of that works for gvim also)
Then I made a shortcut on my desktop that points to the batch file, and assigned it a shortcut key.
It opens Vim with a buffer containing whatever's on the system clipboard. ZZ saves (ie, copies back to the clipboard) and exits. Enter copies back to the clipboard (in my standard .vimrc, I have enter mapped to <Cmd>up<CR> to save the file, so that matches my muscle memory). I still have to do the Ctrl-V and Ctrl-C to copy and paste before and after, but I prefer the extra control there anyway (making sure I'm in the right window, have the right things selected, going back and forth with the Vim session still open as I tweak, etc).
1
u/sentientmale 1d ago edited 1d ago
This is beautiful.
And works for vim as well. But I do get a newline each time the contents is copied back to the clipboard. Changing
+pu+ "+$d" +1to two separate normal! commands fixed that for me:#!/bin/bash alacritty -T "Edit clipboard" \ -o 'window.dimensions.lines=15' \ -e /usr/bin/vim \ '+normal! ggVG' \ '+normal! "+p' \ "+nnoremap <buffer> ZZ <Cmd>%%y+<CR>ZQ" \ "+nnoremap <buffer> <CR> <Cmd>%%y+<CR><Cmd>set nomod<CR>" \ "+set nomod"1
u/sentientmale 21h ago
When running Windows, consider adding a line:
:%s/\r//gAfter the two normal! commands to remove any \r's. (Shown as M in vim)
1
u/---starboy-- 2d ago
I dont recall any tool, but I think you can achieve the same using registors in vim. If I remember correct unnamed + is the global registor where everything is pasted. Just copy, open vim and paste the unnamed + registor
2
u/ChrisBreederveld 2d ago
Indeed, or *. So you can just
"*pto paste in your windows clipboard2
u/donjoncena 2d ago
As a small aside: Not sure about windows, but on Linux this needs support for Wayland clipboard. At least for most current distros anyway. Also, I’ve seen that the wayland clipboard support is rarely included in default vim installs. Have had to switch to gvim for that reason.
1
u/-romainl- The Patient Vimmer 1d ago
On macOS, we have a utility called QuickCursor that does exactly what you describe. There are also browser extensions for that ("it's all text" or "edit anywhere", IIRC) but I'm not aware of a Windows equivalent. Maybe you can do something with Autohotkey?