2

Currently when I click somewhere in the terminal with mouse=a enabled, Vim exits visual mode. How can I force Vim to remain in Visual mode, and thereby use mouse clicks (not drags) to create a selection from the line/location where visual mode was entered?


Edit: I came up with a set of remaps that solves this perfectly (earlier I posted an overly complex macro here; you should use this instead). Just put these in your .vimrc:

nnoremap v mzv
nnoremap V mzV
nnoremap <C-v> mz<C-v>
vnoremap <expr> <LeftMouse> '<LeftMouse>my`z' . :exe 'normal! ' . visualmode()<CR> . '`y'
1
  • Terminals have very different ways of dealing with the mouse (several simply do not deal it at all). xterm should work with mouse+Vim out of the box (most emulators behave like xterm if you export TERM=xterm), urxvt configured with URxvt*scrollTtyOutput: false should work as well.
    – grochmal
    Commented Jul 15, 2016 at 21:04

2 Answers 2

3

You could do this:

:xnoremap <LeftMouse> m`<LeftMouse>v``

This allows you to click on where you want to extend the visual selection to, rather than drag. It works best if you also

:set slm=
1
  • Thanks! Only issue was that this selects from the cursor to the mouse-click, instead of extending the current selection. My macro above does this.
    – Luke Davis
    Commented Jul 16, 2016 at 3:47
1

This behavior works fine on gVim if the mouse is enabled (:set mouse=a) and the 'selectmode' option is at the default value:

                                                *'selectmode'* *'slm'*
'selectmode' 'slm'      string  (default "")
                        global
                        {not in Vi}
        This is a comma separated list of words, which specifies when to start
        Select mode instead of Visual mode, when a selection is started.
        Possible values:
           mouse        when using the mouse
           key          when using shifted special keys
           cmd          when using "v", "V" or CTRL-V
        See |Select-mode|.
        The 'selectmode' option is set by the |:behave| command.

But it doesn't seems to work in my terminal. You can find the details in :help mouse-using. If I understand correctly your use case shouldn't work for some terminals, such as xterm:

In an xterm, with the currently active mode included in the 'mouse' option,
normal mouse clicks are used by Vim, mouse clicks with the shift or ctrl key
pressed go to the xterm.  With the currently active mode not included in
'mouse' all mouse clicks go to the xterm.

Note that what you are asking is explicitly discouraged in :help design-improved:

- Use the keyboard as much as feasible.  The mouse requires a third hand,
  which we don't have.  Many terminals don't have a mouse.
- When the mouse is used anyway, avoid the need to switch back to the
  keyboard.  Avoid mixing mouse and keyboard handling.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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