aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTomasz Łaziuk2015-07-19 10:48:53 +0200
committerChris Toomey2015-08-16 21:11:27 -0400
commit750002ade00e2b8264bb131906a605e49985d812 (patch)
treef0de50979aab7a412b562dcff3f27fb10bd24b87
parentaefaff602daead79cf3cc448503e2ad0b2995bee (diff)
downloadvim-system-copy-750002ade00e2b8264bb131906a605e49985d812.tar.bz2
Add support and mapping for pasting from clipboard
-rw-r--r--README.md9
-rw-r--r--plugin/system_copy.vim49
2 files changed, 51 insertions, 7 deletions
diff --git a/README.md b/README.md
index e7f6b5e..f87b360 100644
--- a/README.md
+++ b/README.md
@@ -1,7 +1,7 @@
System Copy
===========
-System copy provides vim mappings for copying text to the os specific
+System copy provides vim mappings for copying / pastying text to the os specific
clipboard. Most people will be happy just setting their Vim clipboard to the
system clipboard, but I find that doing so pollutes my clipboard history.
Instead, this plugin creates a unique mapping that explicitly pulls content
@@ -10,8 +10,9 @@ from Vim into the system clipboard.
Usage
-----
-System copy provides a mapping to copy to the system clipboard. It accepts a
-motion or can be used with a visual selection.
+System copy provides a mapping to copy to the system clipboard using a motion
+or visual selection. It also provides a mapping for pasting from the system
+clipboard.
The default mapping is `cp`, and can be followed by any motion or text
object. For instance:
@@ -21,6 +22,8 @@ object. For instance:
In addition, `cP` is mapped to copy the current line directly.
+The sequence `cv` is mapped to paste the content of system clipboard.
+
Clipboard Utilities
-------------------
diff --git a/plugin/system_copy.vim b/plugin/system_copy.vim
index 80a58f0..2c6bc7c 100644
--- a/plugin/system_copy.vim
+++ b/plugin/system_copy.vim
@@ -2,6 +2,9 @@ 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
@@ -22,6 +25,12 @@ function! s:system_copy(type, ...) abort
echohl String | echon 'Copied to system clipboard via: ' . mode | echohl None
endfunction
+function! s:system_paste() abort
+ let command = <SID>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
@@ -33,23 +42,55 @@ function! s:resolve_mode(type, arg)
endif
endfunction
-function! s:CopyCommandForCurrentOS()
+function! s:currentOS()
let os = substitute(system('uname'), '\n', '', '')
- if has("gui_mac") || os == 'Darwin'
- return 'pbcopy'
+ let known_os = 'unknown'
+ if has("gui_mac") || os ==? 'Darwin'
+ let known_os = s:mac
elseif has("gui_win32")
- return 'clip'
+ let known_os = s:windows
+ elseif os ==? 'Linux'
+ let known_os = s:linux
else
+ exe "normal \<Esc>"
+ throw "unknown OS: " . os
+ endif
+ return known_os
+endfunction
+
+function! s:CopyCommandForCurrentOS()
+ let os = <SID>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 = <SID>currentOS()
+ if os == s:mac
+ return 'pbpaste'
+ elseif os == s:windows
+ return 'paste'
+ elseif os == s:linux
+ return 'xsel --clipboard --output'
+ endif
+endfunction
+
xnoremap <silent> <Plug>SystemCopy :<C-U>call <SID>system_copy(visualmode(),visualmode() ==# 'V' ? 1 : 0)<CR>
nnoremap <silent> <Plug>SystemCopy :<C-U>set opfunc=<SID>system_copy<CR>g@
nnoremap <silent> <Plug>SystemCopyLine :<C-U>set opfunc=<SID>system_copy<Bar>exe 'norm! 'v:count1.'g@_'<CR>
+nnoremap <silent> <Plug>SystemPaste :<C-U>call <SID>system_paste()<CR>
if !hasmapto('<Plug>SystemCopy') || maparg('cp','n') ==# ''
xmap cp <Plug>SystemCopy
nmap cp <Plug>SystemCopy
nmap cP <Plug>SystemCopyLine
endif
+
+if !hasmapto('<Plug>SystemPaste') || maparg('cv','n') ==# ''
+ nmap cv <Plug>SystemPaste
+endif