Это то, что я использую для поведения вставки Windows, которое позволяет мне визуально выбирать текст и вставлять содержимое буфера обмена, чтобы перезаписать то, что было выбрано, без повреждения содержимого буфера обмена в процессе.
Примечание. Код, представленный здесь, сокращен. Полный список с возможностью отладки, а также другие комментарии и отзывы см .: http://pastebin.com/aBEsm5mV
function! SmartCut()
execute 'normal! gv"+c'
if visualmode() != "\<C-v>" " If not Visual-Block mode
" Trim the last \r\n | \n | \r character in the '+' buffer
" Note: This messes up Visual-Block pasting.
let @+ = substitute(@+,'\(\r\?\n\|\r\)$','','g')
endif
endfunction
function! SmartCopy()
execute 'normal! gv"+y'
if visualmode() != "\<C-v>" " If not Visual-Block mode
" Trim the last \r\n | \n | \r character in the '+' buffer
" Note: This messes up Visual-Block pasting.
let @+ = substitute(@+,'\(\r\?\n\|\r\)$','','g')
endif
endfunction
function! SmartPaste()
let mode = 'gv'
let delete = '"_d'
let reg = '"+'
let currentLine = line(".")
let lastLineOfBuffer = line("$")
let selectionEndLine = line("'>")
let selectionEndLineLength = len(getline(selectionEndLine))
let nextLineLength = len(getline(currentLine + 1))
let selectionEndColumn = col("'>")
" If selection does not include or go beyond the last visible character of the line (by also selecting the invisible EOL character)
if selectionEndColumn < selectionEndLineLength
let cmd = 'P'
" If attempting to paste on a blank last line
elseif selectionEndLineLength == 0 && selectionEndLine == lastLineOfBuffer
let cmd = 'P'
" If selection ends after the last visible character of the line (by also selecting the invisible EOL character) and next line is not blank and not the last line
elseif selectionEndColumn > selectionEndLineLength && nextLineLength > 0 && selectionEndLine != lastLineOfBuffer
let cmd = 'P'
" If selection ends after the last visible character of the line (by also selecting the invisible EOL character), or the line is visually selected (Shift + V), and next line is the last line
elseif selectionEndColumn > selectionEndLineLength && selectionEndLine == lastLineOfBuffer
let cmd = 'p'
call append(selectionEndLine, "")
else
let cmd = 'p'
endif
if visualmode() != "\<C-v>" " If not Visual-Block mode
" Trim the last \r\n | \n | \r character in the '+' buffer
" Note: This messes up Visual-Block pasting.
let @+ = substitute(@+,'\(\r\?\n\|\r\)$','','g')
endif
try
execute 'normal! ' . mode . delete . reg . cmd
catch /E353:\ Nothing\ in\ register\ +/
endtry
" Move caret one position to right
call cursor(0, col(".") + 1)
endfunction
Использование:
" p or P delete to black hole register before pasting
vnoremap <silent>p :<C-u>call SmartPaste()<CR>
vnoremap <silent>P :<C-u>call SmartPaste()<CR>
" MiddleMouse delete to black hole register before pasting
nmap <MiddleMouse> "+p " Changes default behavior from 'P' mode to 'p' mode for normal mode middle-mouse pasting
vmap <silent><MiddleMouse> :<C-u>call SmartPaste()<CR>
imap <MiddleMouse> <C-r><C-o>+
" Disable weird multi-click things you can do with middle mouse button
map <2-MiddleMouse> <Nop>
imap <2-MiddleMouse> <Nop>
map <3-MiddleMouse> <Nop>
imap <3-MiddleMouse> <Nop>
map <4-MiddleMouse> <Nop>
imap <4-MiddleMouse> <Nop>
if os !~ "mac" " NOTE: MacVim provides Command+C|X|V|A|S and undo/redo support and also can Command+C|V to the command line by default
" Copy and paste for the command line
cmap <C-c> <C-y>
cmap <C-v> <C-r>+
" TODO: Is their a select-all for the command line???
vmap <silent><C-x> :<C-u>call SmartCut()<CR>
vmap <silent><C-c> :<C-u>call SmartCopy()<CR>
vmap <silent><C-v> :<C-u>call SmartPaste()<CR>
imap <C-v> <C-r><C-o>+
" Select-all support for normal, visual, and insert mode
" http://vim.wikia.com/wiki/Using_standard_editor_shortcuts_in_Vim
nmap <C-a> ggVG
vmap <C-a> ggVG
imap <C-a> <Esc>ggVG
" Save file support for normal, visual, and insert mode
" If the current buffer has never been saved, it will have no name,
" call the file browser to save it, otherwise just save it.
command -nargs=0 -bar Update if &modified |
\ if empty(bufname('%')) |
\ browse confirm write |
\ else |
\ confirm write |
\ endif |
\endif
nmap <C-s> :update<CR>
vmap <C-s> <Esc>:update<CR>V
imap <C-s> <C-o>:update<CR>
" Undo and redo support for normal, visual, and insert mode
nmap <C-z> <Esc>u
nmap <C-y> <Esc><C-r>
vmap <C-z> <Esc>uV
vmap <C-y> <Esc><C-r>V
imap <C-z> <Esc>uI
imap <C-y> <Esc><C-r>I
" Find and Find/Replace support for normal, visual, and insert mode
nmap <C-f> :promptfind<CR>
nmap <C-h> :promptrepl<CR>
vmap <C-f> <Esc>:promptfind<CR>
vmap <C-h> <Esc>:promptrepl<CR>
imap <C-f> <C-o>:promptfind<CR>
imap <C-h> <C-o>:promptrepl<CR>
endif