How to “copy to clipboard” in vim of Bash on Windows?

前端 未结 9 1028
执念已碎
执念已碎 2020-12-13 00:38

I used to use \"+y to copy text to system\'s clipboard, but it doesn\'t work in vim of Bash on Windows.

9条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-13 01:14

    Below are mappings

    • in normal mode cy, cyy and cY (=Clipboard Yank) that copy, respectively, the given text object, line(s), rest of the line from the cursor position to the clipboard, and
    • in visual mode Y that copies the visual selection to the clipboard,

    independent of whether Vim is started in WSL or not.

    nnoremap                    cy                   "+y
    nnoremap                    (mgu)   m`_
    onoremap                    (gu)        g_
    nnoremap                    (ggg)   g``
    nmap              cyy                  '(mgu)cy' . v:count1 . '(gu)' . '(ggg)'
    nmap                            cY                   cy(gu)
    
    xnoremap Y  "+y
    
    if has('unix') && executable('clip.exe')
        " From :help map-operator
        function! SendToClip(type, ...) abort
            if a:0
                " Visual mode
                normal! gv"+y
            elseif a:type ==# 'line'
                normal! '[V']"+y
            elseif a:type ==# 'char'
                normal! `[v`]"+y
            endif
    
            call system('clip.exe', @+)
        endfunction
    
        nnoremap  cy            :set operatorfunc=SendToClipg@
        xnoremap  Y             :call SendToClip(visualmode(),1)
    endif
    

提交回复
热议问题