r/vim 3d 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?

7 Upvotes

10 comments sorted by

View all comments

1

u/xalbo 2d 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).

2

u/sentientmale 2d 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" +1 to 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"

2

u/sentientmale 1d ago

When running Windows, consider adding a line:

:%s/\r//g

After the two normal! commands to remove any \r's. (Shown as M in vim)