41

Upgraded here a few VM servers to Debian 9.

Now when using ssh, we cannot copy and paste between remote terminals.

The cursor seems to be doing the movements, and marking the text, albeit in a funnier/different way than the usual, but nothing gets copied other to the clipboard when doing command-C / command-V or copy and paste in the respective menu.

We also tried doing the mouse movements with Shift and other keyboard combinations, without positive results.

This is happening in OS/X, namely Sierra and El Capitan, and in Windows, using mobaXterm terminals too.

The situation is due to vim´s awareness of having a mouse.

Following other questions in Stack Overflow, I created /etc/vim/vimrc.local with set mouse="r" and set mouse="v; it did not work out well.

Finally setup up set mouse=""in the same file, with some moderate success.

However, it also does not work well 100% of the time. What else can be done?

4
  • Why would you want to turn mouse support off? On Windows I can copy and paste just fine in a putty session; what terminal emulator are you using on Mac? Oct 25, 2016 at 15:31
  • 3
    The regular Terminal+the ssh binary that comes by default; 2 more work mates complaining...They are headless virtual servers, all of them, only text mode. Not having cut&paste working properly is a big deal for us. Oct 25, 2016 at 15:32
  • I'd recommend using something like iTerm2, I think that's the only terminal emulator I used apart from the retro term one you can get in the Apple store, but I don't remember if that one supported copy and paste over ssh. You should still be able to copy screen selections, though. I do, however, faintly remember having problems when trying to copy crap from the terminal to the system, I wrote a bunch of functions in my .zshrc to help facilitate it, but I'm not sure if that's what you're really looking for. Oct 25, 2016 at 15:35
  • 3
    Maybe someone can file a request to revert this change (in debian?) to get back the mouse= mode.
    – steffen
    Aug 7, 2018 at 20:33

5 Answers 5

45

Solution: change mouse=a to mouse=r in your local .vimrc.

The problem with setting this in /usr/share/vim/vim80/defaults.vim as the accepted answer says, is that it will be overwritten on every update. I searched for a long time and ended up on this one: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=864074

LOCAL SOLUTION (flawed):
The first solution is to use local .vimrc files and set it there. So you could create a local .vimrc (~/.vimrc) for every user and set your options there. Or create one in /etc/skel so it will be automatically created for every new user you create.

But when you use local .vimrc files, you have to set all options there, because if there is a local .vimrc, the defaults.vim doesn't get loaded at all! And if there is no local .vimrc all your settings are being overwritten from defaults.vim.

GLOBAL SOLUTION (preferrable):
I wanted a global configuration for all users, which loads the default options and then adds or overwrites the defaults with my personal settings. Luckily there is an option for that in Debian: The /etc/vim/vimrc.local will be loaded after the /etc/vim/vimrc. So you can create this file and load defaults, preventing them from being loaded again (at the end) and then add your personal options:

Please create the following file: /etc/vim/vimrc.local

" This file loads the default vim options at the beginning and prevents
" that they are being loaded again later. All other options that will be set,
" are added, or overwrite the default settings. Add as many options as you
" whish at the end of this file.

" Load the defaults
source $VIMRUNTIME/defaults.vim

" Prevent the defaults from being loaded again later, if the user doesn't
" have a local vimrc (~/.vimrc)
let skip_defaults_vim = 1


" Set more options (overwrites settings from /usr/share/vim/vim80/defaults.vim)
" Add as many options as you whish

" Set the mouse mode to 'r'
if has('mouse')
  set mouse=r
endif

(Note that $VIMRUNTIME used in the above snippet has a value like /usr/share/vim/vim80/defaults.vim.)

If you also want to enable the "old copy/paste behavior", add the following lines at the end of that file as well:

" Toggle paste/nopaste automatically when copy/paste with right click in insert mode:
let &t_SI .= "\<Esc>[?2004h"
let &t_EI .= "\<Esc>[?2004l"

inoremap <special> <expr> <Esc>[200~ XTermPasteBegin()

function! XTermPasteBegin()
  set pastetoggle=<Esc>[201~
  set paste
  return ""
endfunction
3
  • 4
    This should be the accepted answer. Also from your linked bug report I think we can get away with source $VIMRUNTIME/defaults.vim.
    – Alasdair
    Mar 20, 2018 at 14:28
  • 1
    Finally managed to deploy this, superb, great work! +1 Jan 29, 2019 at 14:40
  • 2
    Still working in Debian 10, just using source /usr/share/vim/vim81/defaults.vim Jul 3, 2019 at 12:55
26

One way to cut vim from mouse awareness seems to be commenting out the configuration about the mouse.

In /usr/share/vim/vim80/defaults.vim I commented out the mouse specific detection as in:

" In many terminal emulators the mouse works just fine.  By enabling it you
" can position the cursor, Visually select and scroll with the mouse.
"if has('mouse')
"  set mouse=r
"endif

(in those vim configuration files, " is initiating a comment).

The change has allowed us to copy and paste again without any problems.

I do agree with the comments this is not the ideal solution, due to indeed being overwritten into any update unless the configuration file is protected (or diverted). At the time, and due to specifics either of a version of the package or of the configuration of the servers where I used to work, it was the only one that worked. As such, I am leaving this answer here, and it should only be used as a last resort solution.

2
  • 2
    IMO this is a bad answer! Encouraging users to edit a file handled by package management is likely to break things unexpectedly in the future. @I-GaLaXy-I answer is preferable. May 1, 2018 at 1:09
  • 1
    @JeremyDavis I do agree with you this is not the best solution. At the time, it was the only one that worked, for some unknown reason, and I do tried several alternatives. However, it more than one people is now telling me the other solution nowadays works, I am accepting it as the correct answer. I used diversions at the time, and the configuration file was not overwritten, however that is also not an ideal solution. May 1, 2018 at 12:09
15

The Vim documentation for the mouse option says

The xterm handling of the mouse buttons can still be used by keeping the shift key pressed.

2
  • 1
    Thanks for that. I've been using Vim for years and wasn't aware of that feature. I've edited your question to incorporate more of the formatting features that are available. Welcome to Unix & Linux! Jul 25, 2017 at 9:10
  • 1
    The problem is that I am using Terminal in Mac and my operators putty/mobaXterm in Windows...not xterm Jul 28, 2017 at 15:03
4

You can disable this without altering system files by creating /etc/vimrc.local and inserting the following:

set mouse=
5
  • In beta time, it did not work consistently, will check it out again, thanks. Aug 23, 2017 at 6:59
  • 3
    This does not work.
    – LtWorf
    Sep 26, 2017 at 7:31
  • See I-GaLaXy-I's answer above. You need to explicitly import defaults.vim and prevent it from being imported.
    – richard
    Feb 18, 2018 at 20:02
  • This works fine for me too, although I combined it with @I-GaLaXy-I's answer (i.e. edited/created /etc/vim/vimrc.local) May 1, 2018 at 1:11
  • worked fine for me while inside a debian 9 container ... just put it into ~/.vimrc Jun 15, 2018 at 17:10
3

Many people suggested :set mouse=a but I found that only :set mouse=r enables copy & paste in Vim on Debian 9. Good luck!

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .