0

I have been using vi or vim for many years however recently ( past year or two ) when I paste some highlighted text vim seems to paste highlighted text ( more often than not ) to random location in the file ... I paste by hitting mouse middle roller button and I think vim is poorly handling the paste by sensing a minute amount of roller action during the press down in the mouse middle roller button during the paste action

Here is my cat ~/.vimrc

unlet! skip_defaults_vim
source $VIMRUNTIME/defaults.vim


set ts=4
set ic

syntax on

"  to shut off paste indent
se paste


"  to disable paste moving mouse paste location
"  mouse=r
"   above fails:   E492: Not an editor command: mouse=r
"  mouse=v
"   above ALSO fails:   E492: Not an editor command: mouse=r



"  If this does not work, a common problem is not having ownership of your ~/.viminfo file. If this is the case, then sudo chown user:group ~/.viminfo
" Uncomment the following to have Vim jump to the last position when
" reopening a file
if has("autocmd")
 au BufReadPost * if line("'\"") > 1 && line("'\"") <= line("$") | exe "normal! g'\"" | endif
endif

Notice commented above two attempts to fix this

Please note this never happened before a year or two ago ... since then its almost useless to even use vim due to this reason ... Is there a way to tell vim to always ignore any roller actions from mouse middle roller button ( continue to sense the press of that roller yet ignore the roller )

I do not use any vim plugins

I am on Ubuntu 20.04 yet same vim bug happens on any Ubuntu for past many years

vim --version
VIM - Vi IMproved 8.1 (2018 May 18, compiled Feb 01 2022 09:16:32)
Included patches: 1-2269, 3612, 3625, 3669, 3741

In the mean time I am trying to always use shift + insert to paste instead of using the much more convenient mouse middle roller button press

1
  • 1
    the syntax to change options is :set mouse=r
    – D. Ben Knoble
    Jul 31 at 19:35

1 Answer 1

0

Not a direct answer to you question (it's instead a non-mouse work around), but I have a function in my .vimrc to paste from the system clipboard (e.g. the + and the * registers:

" paste from system clipboard
function! PasteFromRegister(reg, up_or_down, autoindent)
    set paste
    if a:up_or_down ==# 'up'
        call append(line('.')-1, '')
        execute "normal! k"
    elseif a:up_or_down ==# 'down'
        call append('.', '')
        execute "normal! j"
    endif
    execute 'normal! "'.a:reg.'p'
    if a:autoindent ==# 'autoindent'
        execute 'normal! =='
    endif
    set nopaste
endfunction

" paste from system CTRL-C clipboard
nnoremap <LEADER>p :call PasteFromRegister('+', 'same', 'noautoindent')<CR>
nnoremap <LEADER>]p :call PasteFromRegister('+', 'down', 'autoindent')<CR>
nnoremap <LEADER>[p :call PasteFromRegister('+', 'up', 'autoindent')<CR>
" paste from system highlighted clipboard
nnoremap <LEADER>P :call PasteFromRegister('*', 'same', 'noautoindent')<CR>
nnoremap <LEADER>]P :call PasteFromRegister('*', 'down', 'autoindent')<CR>
nnoremap <LEADER>[P :call PasteFromRegister('*', 'up', 'autoindent')<CR>

Bonus

To copy from my vim (which I always run in the terminal) to my system clipboard, I first yank it to the unnailed register, then call the following function (it works by temporarily pasting the contents at the bottom of the file - it's been in my .vimrc for a long time, now I wonder if there's a more elegant way to copy the contents of one register to another...):

" run a command, but put the cursor back when it's done
function! Preserve(command, is_func)
    " save last search, and cursor position.
    let _s=@/
    let l = line(".")
    let c = col(".")
    " Do the business:
    if a:is_func ==# 1
        execute a:command()
    else
        execute a:command
    endif
    " restore previous search history, and cursor position
    let @/=_s
    call cursor(l, c)
endfunction

" copy contents of unnamed register to system CTRL-C clipboard
nnoremap <silent> <LEADER>y :call Preserve("normal! Gp\"+dGu", 0)<CR>
            \ :echo 'copied to CTRL-C clipboard'<CR>
" copy contents of unnamed register to system highlighted clipboard
nnoremap <silent> <LEADER>Y :call Preserve("normal! Gp\"*dGu", 0)<CR>
            \ :echo 'copied to highlight clipboard'<CR>
2
  • execute "normal! j" can be just normal! j or even -; similar for other cases. Also, if you’re putting from a register, I don’t think setting paste has any effect (it’s specifically for when you actually paste from a clipboard using OS controls in insert mode and don’t want the text to trigger anything in Vim). So something like "*p'[='] should do fine, though you may need to adjust to paste linewise (perhaps :put * then '[=']?)
    – D. Ben Knoble
    Aug 1 at 2:32
  • Thanks @D.BenKnoble! This part of my .vimrc hails from a time when I was less informed (and if it 'worked' I would declare success and leave it at that :) I'll play around with it and update the answer soon. Thanks once again.
    – mattb
    Aug 1 at 7:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.