Search code examples
neovimmousewheelgnome-terminal

Neovim in GNOME-terminal with mouse disabled moves cursor when scrolled


I'm using Neovim in the GNOME-terminal with the mouse= option set to disable all mouse features. This is the way I want it as I don't want the mouse moving my cursor in general.

When I use the mouse scroll wheel, whatever event the terminal is sending is moving the cursor up and down. I would rather have the scroll wheel activate real scrolling (like <C-y> and <C-e>), which is what happens if mouse=a. Note that I do NOT want to enable Neovim's mouse handling with mouse=a or similar.

How do I modify the behavior of Vim when it gets whatever event the terminal is sending when I use the mouse scroll wheel and Neovim's mouse handling is disabled?

Neovim 0.9.1 under Fedora.


Solution

  • This behaviour is caused by one's terminal emulator mapping the scroll functionality to the arrow keys. You can add the following lines to your configuration to disable all mouse interaction with Neovim (in certain modes):

    For Lua (.lua):

    vim.opt.mouse = ""
    vim.keymap.set({ "", "i" }, "<up>", "<nop>")
    vim.keymap.set({ "", "i" }, "<down>", "<nop>")
    

    To enable "real scrolling", simply change the right-hand side mapping (i.e. <nop>) to <c-y> and c-e for the <up> and <down> keys, respectively.

    For VimL (.vim)

    set mouse=
    noremap <Up> <nop>
    inoremap <Up> <nop>
    noremap <Down> <nop>
    inoremap <Down> <nop>
    

    To find what map modes exist, see :h map-table.


    Note: mapping a key to <nop> means that the key will no longer perform an operation. If you require the <up> and <down> keys to retain their original functionality, while still disabling the mouse scroll functionality, you'll likely have to look at modifying the behaviour of your terminal emulator.