Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Version 1.22

- BUG: When repeat.vim is not installed, the mappings do nothing. Need to :execute the :silent! call of repeat.vim to avoid that the remainder of the command line is aborted together with the call.
- Using separate autoload script to help speed up Vim startup.
  • Loading branch information...
commit 318182677386bf87921a9de3e2d75fbc417e852b 1 parent 46ba4bc
Ingo Karkat authored committed
View
83 README
@@ -4,17 +4,82 @@ DESCRIPTION
If you're like me, you occasionally do a linewise yank, and then want to
insert that yanked text in the middle of some other line (or vice versa).
The mappings defined by this plugin will allow you to do a character-, line-,
-or block-wise paste no matter how you yanked the text.
+or block-wise paste no matter how you yanked the text, both from normal and
+insert mode.
+
+Often, the register contents aren't quite in the form you need them. Maybe you
+need to convert yanked lines to comma-separated arguments, maybe join the
+lines with another separator, maybe the reverse: un-joining a single line on
+some pattern to yield multiple lines. Though you can do the manipulation after
+pasting, this plugin offers shortcut mappings for these actions, which are
+especially helpful when you need to repeat the paste multiple times.
SOURCE
Based on vimtip #1199 by cory,
-    http://vim.wikia.com/wiki/Unconditional_linewise_or_characterwise_paste
+ http://vim.wikia.com/wiki/Unconditional_linewise_or_characterwise_paste
+
+RELATED WORKS
+- whitespaste.vim (vimscript #4351) automatically removes blank lines around
+ linewise contents, and condenses inner lines to a single one. By default, it
+ remaps p / P, but this can be changed.
USAGE
-["x]gcp                 Paste characterwise (newline characters are flattened
-["x]gcP                 to spaces) [count] times.
-["x]glp                 Paste linewise (even if yanked text is not a complete
-["x]glP                 line) [count] times.
-["x]gbp                 Paste blockwise (inserting multiple lines in-place,
-["x]gbP                 pushing existing text further to the right) [count]
-                        times.
+["x]gcp, ["x]gcP Paste characterwise (newline characters and indent are
+ flattened to spaces) [count] times.
+
+["x]glp, ["x]glP Paste linewise (even if yanked text is not a complete
+ line) [count] times.
+
+["x]gbp, ["x]gbP Paste blockwise (inserting multiple lines in-place,
+ pushing existing text further to the right) [count]
+ times.
+
+["x]g,p, ["x]g,P Paste characterwise, with each line delimited by ", "
+ instead of the newline (and indent).
+
+["x]gqp, ["x]gqP Query for a separator string, then paste
+ characterwise, with each line delimited by it.
+
+["x]gQp, ["x]gQP Paste characterwise, with each line delimited by the
+ previously queried (gqp) separator string.
+
+["x]gup, ["x]guP Query for a separator pattern, un-join the register
+ contents, then paste linewise.
+
+["x]gUp, ["x]gUP Un-join the register contents on the previously
+ queried (gup) separator pattern, then paste
+ linewise.
+
+["x]gpp, ["x]gpP Paste with the first number found on or after the
+ current cursor column (or the overall first number, if
+ no such match, or the last number, if the cursor is at
+ the end of the line) incremented / decremented by 1.
+ Do this [count] times, with each paste further
+ incremented / decremented.
+
+CTRL-R CTRL-C {0-9a-z"%#*+/:.-}
+ Insert the contents of a register characterwise
+ (newline characters and indent are flattened to
+ spaces).
+ If you have options like 'textwidth', 'formatoptions',
+ or 'autoindent' set, this will influence what will be
+ inserted.
+CTRL-R , {0-9a-z"%#*+/:.-}
+ Insert the contents of a register characterwise, with
+ each line delimited by ", " instead of the newline
+ (and indent).
+CTRL-R CTRL-Q {0-9a-z"%#*+/:.-}
+ Query for a separator string, then insert the contents
+ of a register characterwise, with each line delimited
+ by it.
+CTRL-R CTRL-Q CTRL-Q {0-9a-z"%#*+/:.-}
+ Insert the contents of a register characterwise, with
+ each line delimited by the previously queried (gqp,
+ i_CTRL-R_CTRL-Q) separator string.
+CTRL-R CTRL-U {0-9a-z"%#*+/:.-}
+ Query for a separator pattern, un-join the contents of
+ a register, then insert it linewise.
+CTRL-R CTRL-U CTRL-U {0-9a-z"%#*+/:.-}
+ Un-join the contents of
+ a register on the previously queried (gup,
+ i_CTRL_R_CTRL-U) pattern, then insert it linewise.
View
128 autoload/UnconditionalPaste.vim
@@ -0,0 +1,128 @@
+" UnconditionalPaste.vim: Force character-/line-/block-wise paste, regardless of
+" how it was yanked.
+"
+" DEPENDENCIES:
+
+" Copyright: (C) 2006-2012 Ingo Karkat
+" The VIM LICENSE applies to this script; see ':help copyright'.
+"
+" Maintainer: Ingo Karkat <ingo@karkat.de>
+" Source: Based on vimtip #1199 by cory,
+" http://vim.wikia.com/wiki/Unconditional_linewise_or_characterwise_paste
+"
+" REVISION DATE REMARKS
+" 1.22.015 04-Dec-2012 Split off functions into autoload script.
+" 1.22.014 28-Nov-2012 BUG: When repeat.vim is not installed, the
+" mappings do nothing. Need to :execute the
+" :silent! call of repeat.vim to avoid that the
+" remainder of the command line is aborted
+" together with the call.
+" 1.21.013 02-Dec-2011 ENH: When pasting a blockwise register as lines,
+" strip all trailing whitespace. This is useful
+" when cutting a block of text from a column-like
+" text and pasting as new lines.
+" ENH: When pasting a blockwise register as
+" characters, flatten and shrink all trailing
+" whitespace to a single space.
+" 1.20.012 29-Sep-2011 BUG: Repeat always used the unnamed register.
+" Add register registration to enhanced repeat.vim
+" plugin, which also handles repetition when used
+" together with the expression register "=.
+" BUG: Move <silent> maparg to <Plug> mapping to
+" silence command repeat.
+" 1.11.010 06-Jun-2011 ENH: Support repetition of mappings through
+" repeat.vim.
+" 1.10.009 12-Jan-2011 Incorporated suggestions by Peter Rincker
+" (thanks for the patch!):
+" Made mappings configurable via the customary
+" <Plug> mappings.
+" Added mappings gbp, gbP for blockwise pasting.
+" Now requires Vim version 7.0 or higher.
+" 1.00.008 10-Dec-2010 Prepared for publishing; find out lowest
+" supported Vim version.
+" 007 15-May-2009 Now catching and reporting any errors caused by
+" the paste.
+" Now supporting [count], like the built-in paste
+" command.
+" 006 08-Oct-2008 Now removing newline characters at the end of
+" the text.
+" Now, the register type is not modified by an
+" unconditional paste command.
+" Now, multiple sequential newlines are converted
+" to a single space.
+" Refactored s:FlattenRegister() to s:Flatten().
+" 005 16-Jun-2008 Using :normal with <bang>.
+" 004 30-May-2007 Added <silent> to the mapping to avoid echoing
+" of the function invocation.
+" 0.03 13-May-2006 Changed mappings from <leader>.. to g.., as
+" this is easier to type (and 'g' often introduces
+" alternative actions (like 'j' and 'gj')).
+" 0.02 10-Apr-2006 Added flattening (replacing newlines with
+" spaces) for characterwise paste.
+" 0.01 10-Apr-2006 file creation from vimtip #1199
+
+function! UnconditionalPaste#HandleExprReg( exprResult )
+ let s:exprResult = a:exprResult
+endfunction
+
+function! s:Flatten( text )
+ " Remove newline characters at the end of the text, convert all other
+ " newlines to a single space.
+ return substitute(substitute(a:text, '\n\+$', '', 'g'), '\n\+', ' ', 'g')
+endfunction
+function! s:StripTrailingWhitespace( text )
+ return substitute(a:text, '\s\+\ze\(\n\|$\)', '', 'g')
+endfunction
+
+function! UnconditionalPaste#Paste( regName, pasteType, pasteCmd )
+ let l:regType = getregtype(a:regName)
+ let l:regContent = getreg(a:regName, 1) " Expression evaluation inside function context may cause errors, therefore get unevaluated expression when a:regName ==# '='.
+
+ if a:regName ==# '='
+ " Cannot evaluate the expression register within a function; unscoped
+ " variables do not refer to the global scope. Therefore, evaluation
+ " happened earlier in the mappings, and stored this in s:exprResult.
+ " To get the expression result into the buffer, use the unnamed
+ " register, and restore it later.
+ let l:regName = '"'
+ let l:regContent = s:exprResult
+
+ let l:save_clipboard = &clipboard
+ set clipboard= " Avoid clobbering the selection and clipboard registers.
+ let l:save_reg = getreg(l:regName)
+ let l:save_regmode = getregtype(l:regName)
+ else
+ let l:regName = a:regName
+ endif
+
+ try
+ let l:pasteContent = l:regContent
+ if a:pasteType ==# 'c'
+ if l:regType[0] ==# "\<C-v>"
+ let l:pasteContent = s:Flatten(s:StripTrailingWhitespace(l:regContent))
+ else
+ let l:pasteContent = s:Flatten(l:regContent)
+ endif
+ elseif a:pasteType ==# 'l' && l:regType[0] ==# "\<C-v>"
+ let l:pasteContent = s:StripTrailingWhitespace(l:regContent)
+ endif
+
+ call setreg(l:regName, l:pasteContent, a:pasteType)
+ execute 'normal! "' . l:regName . (v:count ? v:count : '') . a:pasteCmd
+ call setreg(l:regName, l:regContent, l:regType)
+ catch /^Vim\%((\a\+)\)\=:E/
+ " v:exception contains what is normally in v:errmsg, but with extra
+ " exception source info prepended, which we cut away.
+ let v:errmsg = substitute(v:exception, '^Vim\%((\a\+)\)\=:', '', '')
+ echohl ErrorMsg
+ echomsg v:errmsg
+ echohl None
+ finally
+ if a:regName ==# '='
+ call setreg('"', l:save_reg, l:save_regmode)
+ let &clipboard = l:save_clipboard
+ endif
+ endtry
+endfunction
+
+" vim: set ts=8 sts=4 sw=4 noexpandtab ff=unix fdm=syntax :
View
60 doc/UnconditionalPaste.txt
@@ -1,4 +1,4 @@
-*UnconditionalPaste.txt* Force character-/line-/block-wise paste, regardless of how it was yanked.
+*UnconditionalPaste.txt* Force character-/line-/block-wise paste, regardless of how it was yanked.
UNCONDITIONAL PASTE by Ingo Karkat
*UnconditionalPaste.vim*
@@ -21,36 +21,36 @@ or block-wise paste no matter how you yanked the text.
SOURCE *
-Based on vimtip #1199 by cory,
+Based on vimtip #1199 by cory,
http://vim.wikia.com/wiki/Unconditional_linewise_or_characterwise_paste
==============================================================================
USAGE *UnconditionalPaste-usage*
*gcp* *gcP* *glp* *glP* *gbp* *gbP*
["x]gcp Paste characterwise (newline characters are flattened
-["x]gcP to spaces) [count] times.
+["x]gcP to spaces) [count] times.
["x]glp Paste linewise (even if yanked text is not a complete
-["x]glP line) [count] times.
+["x]glP line) [count] times.
["x]gbp Paste blockwise (inserting multiple lines in-place,
["x]gbP pushing existing text further to the right) [count]
- times.
+ times.
==============================================================================
INSTALLATION *UnconditionalPaste-installation*
This script is packaged as a |vimball|. If you have the "gunzip" decompressor
-in your PATH, simply edit the *.vba.gz package in Vim; otherwise, decompress
+in your PATH, simply edit the *.vmb.gz package in Vim; otherwise, decompress
the archive first, e.g. using WinZip. Inside Vim, install by sourcing the
vimball or via the |:UseVimball| command. >
- vim UnconditionalPaste.vba.gz
+ vim UnconditionalPaste*.vmb.gz
:so %
-To uninstall, use the |:RmVimball| command.
+To uninstall, use the |:RmVimball| command.
DEPENDENCIES *UnconditionalPaste-dependencies*
-- Requires Vim 7.0 or higher.
-- repeat.vim (vimscript #2136) plugin (optional).
+- Requires Vim 7.0 or higher.
+- repeat.vim (vimscript #2136) plugin (optional)
==============================================================================
CONFIGURATION *UnconditionalPaste-configuration*
@@ -58,12 +58,12 @@ CONFIGURATION *UnconditionalPaste-configuration*
If you want to use different mappings (e.g. starting with <Leader>), map your
keys to the <Plug>UnconditionalPaste... mapping targets _before_ sourcing this
script (e.g. in your |vimrc|): >
- nmap <silent> <Leader>Pc <Plug>UnconditionalPasteCharBefore
- nmap <silent> <Leader>pc <Plug>UnconditionalPasteCharAfter
- nmap <silent> <Leader>Pl <Plug>UnconditionalPasteLineBefore
- nmap <silent> <Leader>pl <Plug>UnconditionalPasteLineAfter
- nmap <silent> <Leader>Pb <Plug>UnconditionalPasteBlockBefore
- nmap <silent> <Leader>pb <Plug>UnconditionalPasteBlockAfter
+ nmap <Leader>Pc <Plug>UnconditionalPasteCharBefore
+ nmap <Leader>pc <Plug>UnconditionalPasteCharAfter
+ nmap <Leader>Pl <Plug>UnconditionalPasteLineBefore
+ nmap <Leader>pl <Plug>UnconditionalPasteLineAfter
+ nmap <Leader>Pb <Plug>UnconditionalPasteBlockBefore
+ nmap <Leader>pb <Plug>UnconditionalPasteBlockAfter
<
==============================================================================
LIMITATIONS *UnconditionalPaste-limitations*
@@ -77,12 +77,18 @@ IDEAS *UnconditionalPaste-ideas*
==============================================================================
HISTORY *UnconditionalPaste-history*
+1.22 04-Dec-2012
+- BUG: When repeat.vim is not installed, the mappings do nothing. Need to
+ :execute the :silent! call of repeat.vim to avoid that the remainder of the
+ command line is aborted together with the call.
+- Using separate autoload script to help speed up Vim startup.
+
1.21 02-Dec-2011
- ENH: When pasting a blockwise register as lines, strip all trailing
whitespace. This is useful when cutting a block of text from a column-like
- text and pasting as new lines.
+ text and pasting as new lines.
- ENH: When pasting a blockwise register as characters, flatten and shrink all
- trailing whitespace to a single space.
+ trailing whitespace to a single space.
1.20 30-Sep-2011
BUG: Repeat always used the unnamed register. Add register registration to
@@ -92,23 +98,23 @@ repeat.vim version 1.0 (that hopefully makes it into upstream), which is
available at https://github.com/inkarkat/vim-repeat/zipball/1.0ENH1
1.11 06-Jun-2011
-ENH: Support repetition of mappings through repeat.vim.
+ENH: Support repetition of mappings through repeat.vim.
1.10 12-Jan-2011
-Incorporated suggestions by Peter Rincker (thanks for the patch!):
-- Made mappings configurable via the customary <Plug> mappings.
-- Added mappings gbp, gbP for blockwise pasting.
-- Now requires Vim version 7.0 or higher.
+Incorporated suggestions by Peter Rincker (thanks for the patch!):
+- Made mappings configurable via the customary <Plug> mappings.
+- Added mappings gbp, gbP for blockwise pasting.
+- Now requires Vim version 7.0 or higher.
1.00 10-Dec-2010
-Published, prompted by a related question on reddit.
+Published, prompted by a related question on reddit.
0.01 10-Apr-2006
-Started development, based on vimtip #1199 by cory.
+Started development, based on vimtip #1199 by cory.
==============================================================================
-Copyright: (C) 2006-2011 Ingo Karkat
-The VIM LICENSE applies to this script; see |copyright|.
+Copyright: (C) 2006-2012 Ingo Karkat
+The VIM LICENSE applies to this script; see |copyright|.
Maintainer: Ingo Karkat <ingo@karkat.de>
==============================================================================
View
135 plugin/UnconditionalPaste.vim
@@ -1,63 +1,70 @@
" UnconditionalPaste.vim: Force character-/line-/block-wise paste, regardless of
-" how it was yanked.
+" how it was yanked.
"
" DEPENDENCIES:
-" - Requires Vim 7.0 or higher.
-" - repeat.vim (vimscript #2136) autoload script (optional).
+" - Requires Vim 7.0 or higher.
+" - UnconditionalPaste.vim autoload script
+" - repeat.vim (vimscript #2136) autoload script (optional)
-" Copyright: (C) 2006-2011 Ingo Karkat
-" The VIM LICENSE applies to this script; see ':help copyright'.
+" Copyright: (C) 2006-2012 Ingo Karkat
+" The VIM LICENSE applies to this script; see ':help copyright'.
"
" Maintainer: Ingo Karkat <ingo@karkat.de>
-" Source: Based on vimtip #1199 by cory,
+" Source: Based on vimtip #1199 by cory,
" http://vim.wikia.com/wiki/Unconditional_linewise_or_characterwise_paste
"
-" REVISION DATE REMARKS
+" REVISION DATE REMARKS
+" 1.22.015 04-Dec-2012 Split off functions into autoload script.
+" 1.22.014 28-Nov-2012 BUG: When repeat.vim is not installed, the
+" mappings do nothing. Need to :execute the
+" :silent! call of repeat.vim to avoid that the
+" remainder of the command line is aborted
+" together with the call.
" 1.21.013 02-Dec-2011 ENH: When pasting a blockwise register as lines,
" strip all trailing whitespace. This is useful
" when cutting a block of text from a column-like
-" text and pasting as new lines.
+" text and pasting as new lines.
" ENH: When pasting a blockwise register as
" characters, flatten and shrink all trailing
-" whitespace to a single space.
-" 1.20.012 29-Sep-2011 BUG: Repeat always used the unnamed register.
+" whitespace to a single space.
+" 1.20.012 29-Sep-2011 BUG: Repeat always used the unnamed register.
" Add register registration to enhanced repeat.vim
" plugin, which also handles repetition when used
-" together with the expression register "=.
+" together with the expression register "=.
" BUG: Move <silent> maparg to <Plug> mapping to
-" silence command repeat.
+" silence command repeat.
" 1.11.010 06-Jun-2011 ENH: Support repetition of mappings through
-" repeat.vim.
+" repeat.vim.
" 1.10.009 12-Jan-2011 Incorporated suggestions by Peter Rincker
-" (thanks for the patch!):
+" (thanks for the patch!):
" Made mappings configurable via the customary
-" <Plug> mappings.
-" Added mappings gbp, gbP for blockwise pasting.
-" Now requires Vim version 7.0 or higher.
+" <Plug> mappings.
+" Added mappings gbp, gbP for blockwise pasting.
+" Now requires Vim version 7.0 or higher.
" 1.00.008 10-Dec-2010 Prepared for publishing; find out lowest
-" supported Vim version.
+" supported Vim version.
" 007 15-May-2009 Now catching and reporting any errors caused by
-" the paste.
+" the paste.
" Now supporting [count], like the built-in paste
-" command.
+" command.
" 006 08-Oct-2008 Now removing newline characters at the end of
-" the text.
+" the text.
" Now, the register type is not modified by an
-" unconditional paste command.
+" unconditional paste command.
" Now, multiple sequential newlines are converted
-" to a single space.
-" Refactored s:FlattenRegister() to s:Flatten().
-" 005 16-Jun-2008 Using :normal with <bang>.
+" to a single space.
+" Refactored s:FlattenRegister() to s:Flatten().
+" 005 16-Jun-2008 Using :normal with <bang>.
" 004 30-May-2007 Added <silent> to the mapping to avoid echoing
-" of the function invocation.
+" of the function invocation.
" 0.03 13-May-2006 Changed mappings from <leader>.. to g.., as
" this is easier to type (and 'g' often introduces
-" alternative actions (like 'j' and 'gj')).
+" alternative actions (like 'j' and 'gj')).
" 0.02 10-Apr-2006 Added flattening (replacing newlines with
-" spaces) for characterwise paste.
+" spaces) for characterwise paste.
" 0.01 10-Apr-2006 file creation from vimtip #1199
-" Avoid installing twice or when in unsupported Vim version.
+" Avoid installing twice or when in unsupported Vim version.
if exists('g:loaded_UnconditionalPaste') || (v:version < 700)
finish
endif
@@ -66,81 +73,17 @@ let g:loaded_UnconditionalPaste = 1
let s:save_cpo = &cpo
set cpo&vim
-function! s:HandleExprReg( exprResult )
- let s:exprResult = a:exprResult
-endfunction
-
-function! s:Flatten( text )
- " Remove newline characters at the end of the text, convert all other
- " newlines to a single space.
- return substitute(substitute(a:text, '\n\+$', '', 'g'), '\n\+', ' ', 'g')
-endfunction
-function! s:StripTrailingWhitespace( text )
- return substitute(a:text, '\s\+\ze\(\n\|$\)', '', 'g')
-endfunction
-
-function! s:Paste( regName, pasteType, pasteCmd )
- let l:regType = getregtype(a:regName)
- let l:regContent = getreg(a:regName, 1) " Expression evaluation inside function context may cause errors, therefore get unevaluated expression when a:regName ==# '='.
-
- if a:regName ==# '='
- " Cannot evaluate the expression register within a function; unscoped
- " variables do not refer to the global scope. Therefore, evaluation
- " happened earlier in the mappings, and stored this in s:exprResult.
- " To get the expression result into the buffer, use the unnamed
- " register, and restore it later.
- let l:regName = '"'
- let l:regContent = s:exprResult
-
- let l:save_clipboard = &clipboard
- set clipboard= " Avoid clobbering the selection and clipboard registers.
- let l:save_reg = getreg(l:regName)
- let l:save_regmode = getregtype(l:regName)
- else
- let l:regName = a:regName
- endif
-
- try
- let l:pasteContent = l:regContent
- if a:pasteType ==# 'c'
- if l:regType[0] ==# "\<C-v>"
- let l:pasteContent = s:Flatten(s:StripTrailingWhitespace(l:regContent))
- else
- let l:pasteContent = s:Flatten(l:regContent)
- endif
- elseif a:pasteType ==# 'l' && l:regType[0] ==# "\<C-v>"
- let l:pasteContent = s:StripTrailingWhitespace(l:regContent)
- endif
-
- call setreg(l:regName, l:pasteContent, a:pasteType)
- execute 'normal! "' . l:regName . (v:count ? v:count : '') . a:pasteCmd
- call setreg(l:regName, l:regContent, l:regType)
- catch /^Vim\%((\a\+)\)\=:E/
- " v:exception contains what is normally in v:errmsg, but with extra
- " exception source info prepended, which we cut away.
- let v:errmsg = substitute(v:exception, '^Vim\%((\a\+)\)\=:', '', '')
- echohl ErrorMsg
- echomsg v:errmsg
- echohl None
- finally
- if a:regName ==# '='
- call setreg('"', l:save_reg, l:save_regmode)
- let &clipboard = l:save_clipboard
- endif
- endtry
-endfunction
-
function! s:CreateMappings()
for [l:pasteName, pasteType] in [['Char', 'c'], ['Line', 'l'], ['Block', 'b']]
for [l:direction, l:pasteCmd] in [['After', 'p'], ['Before', 'P']]
let l:mappingName = 'UnconditionalPaste' . l:pasteName . l:direction
let l:plugMappingName = '<Plug>' . l:mappingName
execute printf('nnoremap <silent> %s :<C-u>' .
- \ 'silent! call repeat#setreg("\<lt>Plug>%s", v:register)<Bar>' .
+ \ 'execute ''silent! call repeat#setreg("\<lt>Plug>%s", v:register)''<Bar>' .
\ 'if v:register ==# "="<Bar>' .
- \ ' call <SID>HandleExprReg(getreg("="))<Bar>' .
+ \ ' call UnconditionalPaste#HandleExprReg(getreg("="))<Bar>' .
\ 'endif<Bar>' .
- \ 'call <SID>Paste(v:register, %s, %s)<Bar>' .
+ \ 'call UnconditionalPaste#Paste(v:register, %s, %s)<Bar>' .
\ 'silent! call repeat#set("\<lt>Plug>%s")<CR>',
\
\ l:plugMappingName,
Please sign in to comment.
Something went wrong with that request. Please try again.