@@ -70,6 +70,71 @@ nnoremap <C-w>j :wincmd s<cr>:wincmd k<cr>
7070nnoremap <C-w> k :wincmd s<cr>
7171nnoremap <C-w> l :wincmd v<cr> :wincmd l<cr>
7272
73+ " Using Windows clipboard inside WSL Ubuntu
74+ " This emulates an additional '"' register
75+ " I know... it's magical...
76+ " Note: requires paste.exe on the PATH
77+ let is_this_not_on_wsl = system (" uname -r | grep -q 'Microsoft' && echo $?" )
78+ if is_this_not_on_wsl == 0
79+ nnoremap <silent> ""y :set opfunc=WindowsYank<CR> g@
80+ vnoremap <silent> ""y :<C-U> call WindowsYank(visualmode(), 1)<CR>
81+ nnoremap <silent> ""p :call WindowsPaste('p')<CR>
82+ nnoremap <silent> ""P :call WindowsPaste('P')<CR>
83+ nnoremap <silent> ""gp :call WindowsPaste('gp')<CR>
84+ nnoremap <silent> ""gP :call WindowsPaste('gP')<CR>
85+ nnoremap <silent> ""]p :call WindowsPaste(']p')<CR>
86+ nnoremap <silent> ""[p :call WindowsPaste('[p')<CR>
87+ nnoremap <silent> ""]P :call WindowsPaste(']P')<CR>
88+ nnoremap <silent> ""[P :call WindowsPaste('[P')<CR>
89+ vnoremap <silent> ""p :call WindowsPaste('p', 1)<CR>
90+ vnoremap <silent> ""P :call WindowsPaste('P', 1)<CR>
91+ endif
92+
93+ function ! WindowsPaste (command , ... )
94+ " (1) Save original value of the unnamed register to restore later on
95+ let reg_save = @@
96+
97+ " (2) Load clipboard into register
98+ let @@ = system (' pushd /mnt/c/ > /dev/null && paste.exe && popd > /dev/null' )
99+
100+ " (3) Run whatever command was being run
101+ if a: 0
102+ exe " normal! gv" . a: command
103+ " Don't revert unnamed register if inside visual mode
104+ else
105+ exe " normal! " . a: command
106+ " Revert unnamed register
107+ let @@ = reg_save
108+ endif
109+ endfunction
110+
111+ function ! WindowsYank (type , ... )
112+ " (1) Save original values for the selection setting
113+ " and the unnamed register
114+ let sel_save = &selection
115+ let &selection = " inclusive"
116+ let reg_save = @@
117+
118+ " (2) Yank text to unnamed register
119+ if a: 0 " Invoked from Visual mode, use gv command.
120+ silent exe " normal! gvy"
121+ elseif a: type == ' line'
122+ silent exe " normal! '[V']y"
123+ else
124+ silent exe " normal! `[v`]y"
125+ endif
126+
127+ " (3) Send contents of @@ to Windows clip.exe
128+ " Note: I've included a pushd-to-windows-directory to suppress warning
129+ " because windows warns about failing to translate working directory
130+ " when current working directory is inside wsl.
131+ echo system (' pushd /mnt/c/ > /dev/null && clip.exe && popd > /dev/null' , getreg (' ' , 1 , 1 ))
132+
133+ " (4) Restore original settings and value of unnamed register
134+ let &selection = sel_save
135+ let @@ = reg_save
136+ endfunction
137+
73138
74139"
75140" Backup & Persistance
0 commit comments