This is what i use (literally copied from my .vimrc) for Windows-style Control + X cut/Control + C copy/Control + V paste/Control + S save/Control + F find/Control + H replace behavior.
The smartpaste() function should contain what you seek, i.e. a way to paste over highlighted text without simultaneously yanking what was selected.
" Windows common keyboard shortcuts and pasting behavior {{{
" Uncomment to enable debugging.
" Check debug output with :messages
let s:debug_smart_cut = 0
let s:debug_smart_copy = 0
let s:debug_smart_paste = 0
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
if exists("s:debug_smart_cut") && s:debug_smart_cut
echomsg "SmartCut '+' buffer: " . @+
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
if exists("s:debug_smart_copy") && s:debug_smart_copy
echomsg "SmartCopy '+' buffer: " . @+
endif
endfunction
" Delete to black hole register before pasting. This function is a smarter version of "_d"+P or "_dp to handle special cases better.
" SOURCE: http://stackoverflow.com/questions/12625722/vim-toggling-buffer-overwrite-behavior-when-deleting
function! SmartPaste()
let mode = 'gv'
let delete = '"_d'
let reg = '"+'
" See :help '> for more information. Hint: if you select some text and press ':' you will see :'<,'>
" SOURCE: http://superuser.com/questions/723621/how-can-i-check-if-the-cursor-is-at-the-end-of-a-line
" SOURCE: http://stackoverflow.com/questions/7262536/vim-count-lines-in-selected-range
" SOURCE: https://git.zug.fr/config/vim/blob/master/init.vim
" SOURCE: https://git.zug.fr/config/vim/blob/master/after/plugin/zzzmappings.vim
let currentColumn = col(".")
let currentLine = line(".")
let lastVisibleLetterColumn = col("$") - 1
let lastLineOfBuffer = line("$")
let selectionEndLine = line("'>")
let selectionEndLineLength = len(getline(selectionEndLine))
let nextLineLength = len(getline(currentLine + 1))
let selectionStartColumn = col("'<")
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 exists("s:debug_smart_paste") && s:debug_smart_paste
echomsg "SmartPaste special case #1"
endif
" If attempting to paste on a blank last line
elseif selectionEndLineLength == 0 && selectionEndLine == lastLineOfBuffer
let cmd = 'P'
if exists("s:debug_smart_paste") && s:debug_smart_paste
echomsg "SmartPaste special case #2"
endif
" 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 exists("s:debug_smart_paste") && s:debug_smart_paste
echomsg "SmartPaste special case #3"
endif
" 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
" SOURCE: http://vim.wikia.com/wiki/Quickly_adding_and_deleting_empty_lines
" Fixes bug where if the last line is fully selected (Shift + V) and a paste occurs, that the paste appears to insert after the first character of the line above it because the delete operation [which occurs before the paste]
" is causing the caret to go up a line, and then 'p' cmd causes the paste to occur after the caret, thereby pasting after the first letter of that line.
" However this but does not occur if there's a blank line underneath the selected line, prior to deleting it, as the cursor goes down after the delete in that situation.
call append(selectionEndLine, "")
let cmd = 'p'
if exists("s:debug_smart_paste") && s:debug_smart_paste
echomsg "SmartPaste special case #4"
endif
else
let cmd = 'p'
if exists("s:debug_smart_paste") && s:debug_smart_paste
echomsg "SmartPaste default case"
endif
endif
if exists("s:debug_smart_paste") && s:debug_smart_paste
echomsg "SmartPaste debug info:"
echomsg " currentColumn: " . currentColumn
echomsg " currentLine: " . currentLine
echomsg " lastVisibleLetterColumn: " . lastVisibleLetterColumn
echomsg " lastLineOfBuffer: " . lastLineOfBuffer
echomsg " selectionEndLine: " . selectionEndLine
echomsg " selectionEndLineLength: " . selectionEndLineLength
echomsg " nextLineLength: " . nextLineLength
echomsg " selectionStartColumn: " . selectionStartColumn
echomsg " selectionEndColumn: " . selectionEndColumn
echomsg " cmd: " . cmd
echo [getpos("'<")[1:2], getpos("'>")[1:2]]
echo "visualmode(): " . visualmode()
echo "mode(): " . mode()
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
" NOTE: <C-u> removes the '<,'> visual-selection from the command line. See :h c_CTRL-u
vnoremap <silent> p :<C-u>call SmartPaste()<CR>
vnoremap <silent> P :<C-u>call SmartPaste()<CR>
" MiddleMouse delete to black hole register before pasting
nnoremap <MiddleMouse> "+p " Changes default behavior from 'P' mode to 'p' mode for normal mode middle-mouse pasting
" NOTE: <C-u> removes the '<,'> visual-selection from the command line. See :h c_CTRL-u
vnoremap <silent> <MiddleMouse> :<C-u>call SmartPaste()<CR>
inoremap <MiddleMouse> <C-r><C-o>+
" Disable weird multi-click things you can do with middle mouse button
" SOURCE: http://vim.wikia.com/wiki/Mouse_wheel_for_scroll_only_-_disable_middle_button_paste
noremap <2-MiddleMouse> <Nop>
inoremap <2-MiddleMouse> <Nop>
noremap <3-MiddleMouse> <Nop>
inoremap <3-MiddleMouse> <Nop>
noremap <4-MiddleMouse> <Nop>
inoremap <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
" SOURCE: https://opensource.apple.com/source/vim/vim-62.41.2/runtime/macmap.vim.auto.html
" NOTE: Only copy and paste are possible in the command line from what i can tell.
" Their is no undo for text typed in the command line and you cannot paste text onto a selection of text to replace it.
cnoremap <C-c> <C-y>
cnoremap <C-v> <C-r>+
" TODO: Is their a select-all for the command line???
" Cut, copy, and paste support for visual and insert mode (not for normal mode)
" SOURCE: http://superuser.com/questions/10588/how-to-make-cut-copy-paste-in-gvim-on-ubuntu-work-with-ctrlx-ctrlc-ctrlv
" NOTE: <C-u> removes the '<,'> visual-selection from the command line. See :h c_CTRL-u
vnoremap <silent> <C-x> :<C-u>call SmartCut()<CR>
vnoremap <silent> <C-c> :<C-u>call SmartCopy()<CR>
vnoremap <silent> <C-v> :<C-u>call SmartPaste()<CR>
inoremap <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
nnoremap <C-a> ggVG
vnoremap <C-a> ggVG
inoremap <C-a> <Esc>ggVG
" Save file support for normal, visual, and insert mode
" SOURCE: http://vim.wikia.com/wiki/Map_Ctrl-S_to_save_current_or_new_files
" 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
nnoremap <silent> <C-s> :update<CR>
" NOTE: <C-u> removes the '<,'> visual-selection from the command line. See :h c_CTRL-u
vnoremap <silent> <C-s> :<C-u>update<CR>V
" NOTE: <C-o> executes a normal-mode command without leaving insert mode. See :help ins-special-special
"inoremap <silent> <C-s> <C-o>:update<CR>
"
" <C-o> doesn't seem to work while also using the "Open the OmniCompletion menu as you type" code while the menu is visible.
" Doing "call feedkeys("\<C-x>\<C-o>", "n")" to perform omni completion seems to be the issue.
" However doing "call feedkeys("\<C-x>\<C-i>", "n")" to perform keywork completion seems to work without issue.
"
" Workaround will exit insert mode to execute the command and then enter insert mode.
inoremap <silent> <C-s> <Esc>:update<CR>I
" Undo and redo support for normal, visual, and insert mode
nnoremap <C-z> <Esc>u
nnoremap <C-y> <Esc><C-r>
" NOTE: <C-u> removes the '<,'> visual-selection from the command line. See :h c_CTRL-u
vnoremap <C-z> :<C-u>uV
vnoremap <C-y> :<C-u><C-r>V
inoremap <C-z> <Esc>uI
inoremap <C-y> <Esc><C-r>I
function! Find()
let wordUnderCursor = expand('<cword>')
if len(wordUnderCursor) > 0
execute 'promptfind ' . wordUnderCursor
else
execute 'promptfind'
endif
endfunction
function! Replace()
let wordUnderCursor = expand('<cword>')
if len(wordUnderCursor) > 0
execute 'promptrepl ' . wordUnderCursor
else
execute 'promptrepl'
endif
endfunction
" Find and Find/Replace support for normal, visual, and insert mode
nnoremap <C-f> :call Find()<CR>
nnoremap <C-h> :call Replace()<CR>
" NOTE: <C-u> removes the '<,'> visual-selection from the command line. See :h c_CTRL-u
vnoremap <C-f> :<C-u>call Find()<CR>
vnoremap <C-h> :<C-u>call Replace()<CR>
" NOTE: <C-o> executes a normal-mode command without leaving insert mode. See :help ins-special-special
inoremap <C-f> <C-o>:call Find()<CR>
inoremap <C-h> <C-o>:call Replace()<CR>
endif
" }}} Windows common keyboard shortcuts and pasting behavior
3Hey fyi, I've long used that
vnoremap p "_dP map
, and I've noticed that it doesn't work well for the last word/character in a line. I've gone back to thevnoremap p "0p
,vnoremap P "0P
, andset clipboard=unnamed
(for OSX) – Kache – 2014-08-01T06:40:33.0471
vnoremap p "_dP
removes white space on paste. Edit 1 works perfectly. – Vaclav Kasal – 2019-09-12T08:18:44.620