10

I just noticed that my trackpad scroll works in Vim, inertia and all. I'd like to change this to work horizontally - so I can scroll up and down to make the cursor move left-and-right instead of holding down the right arrow key for huge amounts of time.

How can I map my scrolling to left/right movement?

  • 2
    Sidenote: if you're pressing the same movement key more than a few times in a row, you're probably doing it wrong (use wWeE$)} etc.), and if you're using arrow keys you're definitely using Vim wrong. – Doorknob Feb 4 '15 at 18:53
4

According to :help scroll-mouse-wheel the answer is no if you are using the Win32 GUI.

On the other hand, using X11 GUI or console vim with mouse support, the wheel sends key presses that you can remap.

So, you could get the behaviour you describe by

:nmap <ScrollWheelUp> h
:nmap <ScrollWheelDown> l

Note that I'm using nmap for mapping in normal mode.

To get similar behaviour in other modes, you can map to the direction instead of the normal key. E.g. for insert mode movement:

:imap <ScrollWheelUp> <Left>
:imap <ScrollWheelDown> <Right>

To make these default, stick them at the bottom of your vimrc.

  • I believe the OP is using OSX, so neither Win32 nor X11 (most likely). – Seth Feb 4 '15 at 20:18
  • 1
    Is there an answer for OSX? – bsmith89 Feb 4 '15 at 20:19
  • Alas, the vim documentation doesn't mention OSX and I don't have an OSX machine handy on which to try it. :-( – John O'M. Feb 4 '15 at 20:22
  • 1
    ah, I should have mentioned this - I'm running OS X, but SSHing into a Ubuntu server most of the time. – Undo Feb 4 '15 at 23:27
  • That's good news! If the vim on the other end of the ssh responds to the scroll events, then your terminal emulator is sending the scroll events through and the answer above should work for you, both locally running vim and remotely running vim. If you comment with the name of your terminal emulator, I'll add it to the answer. – John O'M. Feb 5 '15 at 3:13
4

I figured out a way to do that with Hammerspoon and clever usage of mapping.

The first part is configuring Shift + ScrollUp and Shift + ScrollDown to scroll horizontally. In order to do that, you have put the following code snippet in your .vimrc:

nnoremap <S-ScrollWheelUp> <ScrollWheelLeft>
nnoremap <S-2-ScrollWheelUp> <2-ScrollWheelLeft>
nnoremap <S-3-ScrollWheelUp> <3-ScrollWheelLeft>
nnoremap <S-4-ScrollWheelUp> <4-ScrollWheelLeft>
nnoremap <S-ScrollWheelDown> <ScrollWheelRight>
nnoremap <S-2-ScrollWheelDown> <2-ScrollWheelRight>
nnoremap <S-3-ScrollWheelDown> <3-ScrollWheelRight>
nnoremap <S-4-ScrollWheelDown> <4-ScrollWheelRight>

Note that we have to use <S-2-ScrollWheelUp> and the like because they are sent as keys to the terminal as well. I found no documentation about this (I figured this out by using Ctrl + V while scrolling in insert mode).

After that, we have to remap horizontal scrolls to Shift + vertical scrolls. To do so, we place the following snippet in Hammerspoon's init.lua

scrollBind = hs.eventtap.new({hs.eventtap.event.types.scrollWheel}, function(e)
  if is_active('iTerm2') then
    local horizontalOffset = e:getProperty(hs.eventtap.event.properties.scrollWheelEventDeltaAxis2)

    if horizontalOffset ~= 0 then
      hs.eventtap.scrollWheel({ 0, horizontalOffset }, {"shift"}, 'pixel')
      return true
    end
  end

  return false
end):start()

function is_active(program_name)
  local active_window_name = hs.window.focusedWindow():application():name()
  return active_window_name == program_name
end

We create the scrollBind variable just so the keystroke watcher isn't erased by the Lua's garbage collector.

  • That's a clever trick, to use ^v in insert mode to see the keys sent! I've also had trouble finding documentation, but lack a PC (and wheel mouse). I'm trying to simulate a wheel with a touch screen. I wonder if you happened to also discover the keys for horizontal scroll wheel. I expected them to be 66/67 (after 64/65 for up/down), and they are meant to be button numbers 6/7, but it doesn't seem to be working. Thanks! – hyperpallium Jul 28 '18 at 6:11
2

If you use MacVim on OS X, this will work with a trackpad:

set mouse=n " enable mouse in normal mode
set nowrap  " disable line wrapping (so there's something to scroll to)
  • I'm aware that OP's situation is different, but this is a valid answer for some users. – David Lord Feb 4 '15 at 23:51

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.