Permalink
Browse files

Added ability to use Windows clipboard inside WSL

  • Loading branch information...
ErnWong committed Sep 17, 2017
1 parent 8316c72 commit 8314ec0c487c592982fe869fc0d1e35ecdcd2c25
Showing with 65 additions and 0 deletions.
  1. +65 −0 vimrc
65 vimrc
@@ -70,6 +70,71 @@ nnoremap <C-w>j :wincmd s<cr>:wincmd k<cr>
nnoremap <C-w>k :wincmd s<cr>
nnoremap <C-w>l :wincmd v<cr>:wincmd l<cr>
" Using Windows clipboard inside WSL Ubuntu
" This emulates an additional '"' register
" I know... it's magical...
" Note: requires paste.exe on the PATH
let is_this_not_on_wsl = system("uname -r | grep -q 'Microsoft' && echo $?")
if is_this_not_on_wsl == 0
nnoremap <silent> ""y :set opfunc=WindowsYank<CR>g@
vnoremap <silent> ""y :<C-U>call WindowsYank(visualmode(), 1)<CR>
nnoremap <silent> ""p :call WindowsPaste('p')<CR>
nnoremap <silent> ""P :call WindowsPaste('P')<CR>
nnoremap <silent> ""gp :call WindowsPaste('gp')<CR>
nnoremap <silent> ""gP :call WindowsPaste('gP')<CR>
nnoremap <silent> ""]p :call WindowsPaste(']p')<CR>
nnoremap <silent> ""[p :call WindowsPaste('[p')<CR>
nnoremap <silent> ""]P :call WindowsPaste(']P')<CR>
nnoremap <silent> ""[P :call WindowsPaste('[P')<CR>
vnoremap <silent> ""p :call WindowsPaste('p', 1)<CR>
vnoremap <silent> ""P :call WindowsPaste('P', 1)<CR>
endif
function! WindowsPaste(command, ...)
" (1) Save original value of the unnamed register to restore later on
let reg_save = @@
" (2) Load clipboard into register
let @@ = system('pushd /mnt/c/ > /dev/null && paste.exe && popd > /dev/null')
" (3) Run whatever command was being run
if a:0
exe "normal! gv" . a:command
" Don't revert unnamed register if inside visual mode
else
exe "normal! " . a:command
" Revert unnamed register
let @@ = reg_save
endif
endfunction
function! WindowsYank(type, ...)
" (1) Save original values for the selection setting
" and the unnamed register
let sel_save = &selection
let &selection = "inclusive"
let reg_save = @@
" (2) Yank text to unnamed register
if a:0 " Invoked from Visual mode, use gv command.
silent exe "normal! gvy"
elseif a:type == 'line'
silent exe "normal! '[V']y"
else
silent exe "normal! `[v`]y"
endif
" (3) Send contents of @@ to Windows clip.exe
" Note: I've included a pushd-to-windows-directory to suppress warning
" because windows warns about failing to translate working directory
" when current working directory is inside wsl.
echo system('pushd /mnt/c/ > /dev/null && clip.exe && popd > /dev/null', getreg('', 1, 1))
" (4) Restore original settings and value of unnamed register
let &selection = sel_save
let @@ = reg_save
endfunction
"
" Backup & Persistance

0 comments on commit 8314ec0

Please sign in to comment.