let s:blockwise = 'blockwise visual' let s:visual = 'visual' let s:motion = 'motion' let s:linewise = 'linewise' let s:mac = 'mac' let s:windows = 'windows' let s:linux = 'linux' if exists('g:loaded_system_copy') || &cp || v:version < 700 finish endif let g:loaded_system_copy = 1 function! s:system_copy(type, ...) abort let mode = resolve_mode(a:type, a:0) if mode == s:linewise let lines = { 'start': line("'["), 'end': line("']") } silent exe lines.start . "," . lines.end . "y" elseif mode == s:visual || mode == s:blockwise silent exe "normal! `<" . a:type . "`>y" else silent exe "normal! `[v`]y" endif silent call system(s:CopyCommandForCurrentOS(), getreg('@')) echohl String | echon 'Copied to system clipboard via: ' . mode | echohl None endfunction function! s:system_paste() abort let command = PasteCommandForCurrentOS() put =system(command) echohl String | echon 'Pasted to vim using: ' . command | echohl None endfunction function! s:resolve_mode(type, arg) let visual_mode = a:arg != 0 if visual_mode return (a:type == '') ? s:blockwise : s:visual elseif a:type == 'line' return s:linewise else return s:motion endif endfunction function! s:currentOS() let os = substitute(system('uname'), '\n', '', '') let known_os = 'unknown' if has("gui_mac") || os ==? 'Darwin' let known_os = s:mac elseif has("gui_win32") let known_os = s:windows elseif os ==? 'Linux' let known_os = s:linux else exe "normal \" throw "unknown OS: " . os endif return known_os endfunction function! s:CopyCommandForCurrentOS() let os = currentOS() if os == s:mac return 'pbcopy' elseif os == s:windows return 'clip' elseif os == s:linux return 'xsel --clipboard --input' endif endfunction function! s:PasteCommandForCurrentOS() let os = currentOS() if os == s:mac return 'pbpaste' elseif os == s:windows return 'paste' elseif os == s:linux return 'xsel --clipboard --output' endif endfunction xnoremap SystemCopy :call system_copy(visualmode(),visualmode() ==# 'V' ? 1 : 0) nnoremap SystemCopy :set opfunc=system_copyg@ nnoremap SystemCopyLine :set opfunc=system_copyexe 'norm! 'v:count1.'g@_' nnoremap SystemPaste :call system_paste() if !hasmapto('SystemCopy') || maparg('cp','n') ==# '' xmap cp SystemCopy nmap cp SystemCopy nmap cP SystemCopyLine endif if !hasmapto('SystemPaste') || maparg('cv','n') ==# '' nmap cv SystemPaste endif