444

I have installed the new iTerm 2. It asked me in a yellow bar at the top if I'd like to enable a mouse feature. Unfortunately, I don't remember the exact sentence anymore.

By accident I approved. Now when I use the scroll wheel on the mouse in iTerm, it doesn't scroll the up anymore, but instead it goes triggers the command history as if I had pressed the up-cursor.

Where I can toggle this option to turn it off again?

Jeff Schaller
  • 2,352
  • 5
  • 23
  • 38
Gambo
  • 4,790
  • 2
  • 14
  • 14
  • 7
    I assume that you use iTerm2.9, and the message you saw is "Do you want the scroll wheel to move the cursor in interactive programs like this?".However i can't find a interface to toggle it, do you still have the problem? – sel-fish Apr 15 '16 at 13:21
  • 1
    Yes but only when I'm logged into a server via ssh. I don't know how this could be connected. – Gambo May 07 '16 at 16:35
  • "only when I'm logged into a server via ssh", you mean that when you use iTerm2 for local operations, that will not happen? Do you use a different 'profile' when 'logged into a server via ssh' ? – sel-fish May 07 '16 at 17:27
  • There must be a terminal control character sent to turn Alternate Screen on and off. I'm thinking it's better long term (not specific to iTerm2 or vim or less) to learn what that control character is. It could be `ESC [ ? 1 0 4 9 l` or `ESC [ ? 1 0 4 7 l` or `ESC [ ? 47 l` (https://www.gnu.org/software/screen/manual/html_node/Control-Sequences.html) – dlamblin Feb 27 '18 at 05:28
  • 2
    this just started happening randomly for me.. frustrating when you're trying to look through logs – SgtPooki Oct 05 '18 at 17:19
  • 2
    It's a mode (alternate screen) for terminal emulation set by the remote to your terminal which is then left on and not set back if you exit the program setting it (like less, vim, or others) unexpectedly (eg disconnect ssh). The command `reset` will assert to set the initial conditions for your terminal again. I know it has a scary name, feel free to read the man page first. Likewise `tput rmcup` sets it off IF you're sure it's on. There's probably a way to ask for the state to be shown, but terminals get complicated. – dlamblin Apr 03 '20 at 22:42

9 Answers9

745

A few terminals, including iTerm2, have a feature where they change the behavior of the wheel mouse when a full-screen program such as vi, or screen or tmux is running. This happens when those programs use the alternate screen, to provide a useful function. Normally, when using the alternate screen in iTerm2, the wheel mouse acts like the scrollbar, scrolling the entire screen up/down. But when this feature is enabled, iTerm2 sends cursor up/down keys, making your command-history change.

As suggested in another comment, select the Preferences menu:

enter image description here

and in that, select the Advanced tab. Scroll down to the Mouse section,

enter image description here

and toggle the entry for

Scroll wheel sends arrow keys when in alternate screen mode

from Yes to No. You will have to restart iTerm2 for the change to take effect. (With iTerm2 v3.1.5 changes take effect without restarting.)

agc
  • 7,973
  • 2
  • 29
  • 50
Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
  • 2
    I tested it before writing the comment - just toggling the option was not enough. – Thomas Dickey Jun 17 '16 at 15:35
  • Might depend on the version. In 3.0.2, toggling is definitely sufficient. In fact, if you are in alternate screen mode and this setting is off and you try (and fail) to scroll with the mouse and subsequently press the up or down arrow key, you'll be prompted to turn this feature on right in the terminal window. – George Jun 18 '16 at 17:59
  • 7
    3.0.12 does not need to be restarted for this to take effect. – dhempler Dec 02 '16 at 21:01
  • 31
    // , I like this feature, and use it in Vim. How do I _exit_ alternate screen mode in a given terminal window? UPDATE: In the terminal that is in alternate screen mode, open a terminal program that activates the alternate screen mode. Exit that program cleanly. This temporarily ends the problem. I think this indicates that the problem was caused by exiting a program that used the Alternate Screen feature in an unusual way. – Nathan Basanese May 02 '17 at 15:25
  • 19
    Sir, you saved me from pure insanity. – B.K. Dec 15 '17 at 00:20
  • This is the preference for the correct and expected behavior WHEN IN THE ALTERNATE SCREEN. Instead of disabling it, you need instead to exit the alternate screen. – dlamblin Feb 27 '18 at 05:31
  • @NathanBasanese said: "open a terminal program that activates the alternate screen mode." for those who don't know which programs do this, *vi* is a program that does this for me (and it is most likely installed in most machines). – Felipe Valdes Oct 14 '19 at 10:03
  • 3
    This is a very aggravating behavior to be enabled by default. freaked me out – WestCoastProjects Feb 29 '20 at 06:18
  • 2
    This must be the worst single feature ever implemented in any software on any planet. – Paschalis Jun 24 '20 at 22:04
  • 1
    One of those ideas that sounds great on paper but no one actually wants in practice. But they have to make it default otherwise it will never get used and they will never find out how much users don't want it. – Sridhar Sarnobat Jul 19 '20 at 04:30
  • Sometimes the switch from scrollbar to up/down keys happens arbitrarily. – gone Dec 14 '20 at 05:51
  • 1
    This is useful in vim, but the fact that it hijacks scrolling when not in an alternate screen like the shell prompt makes it entirely too aggravating. Also, when you disable it, iTerm will immediately ask you to turn it back on when you scroll, so after disabling it you will have to also click “don’t ask again" – lbutlr Mar 03 '22 at 09:49
  • Ah thank you, happened to me when accessing a `screen` session. Very annoying indeed. – gaatjeniksaan Jan 23 '23 at 09:13
  • Just adding that in 3.4.19, `Preferences` wasn't there. It's `Settings...` – maazadeeb Mar 24 '23 at 16:24
109

If you are trapped in the scrolling history mode, you can escape by running vi and exiting. You can also choose another program that uses the alternate screen and then exit.

Scrolling the history with the mouse wheel should in theory never happen. It happens in practice because sometimes the alternate screen mode is not correctly switched back. The accepted answer solves the problem by deactivating a feature that is useful, namely scrolling in vi, less, ... with the mouse wheel or trackpad. You can keep the option from the advanced preferences:

Scroll wheel sends arrow keys when in alternate screen mode

set to yes. You will sometimes be trapped in the scroll history mode. But if you know how to escape, it's not a problem.

q0rban
  • 915
  • 11
  • 9
Ortomala Lokni
  • 56,620
  • 24
  • 188
  • 240
  • 3
    Thanks for the advice! This helped me out :) – jj172 Dec 21 '17 at 04:03
  • 1
    Thanks man! But entering and exiting vi didn't help in my case for some reason, but it worked after entering a man page and exiting it by pressing q – Frax Feb 05 '18 at 16:11
  • 2
    This was so helpful. Scrolling was all of a sudden adding `[[A^` and `[[B^` to the command line instead of scrolling the window. I knew this was not normal behavior. Prior to that I had left the computer in `nano` in an SSH shell that got disconnected when I shut my laptop. That abrupt exit was obviously the culprit. Simply opening a new tab in iTerm2 fixed this. – Colin Marshall Jun 06 '18 at 20:29
  • 1
    Unfortunately when using `screen` you cannot use this trick to exit alternate screen mode. So if you are using `screen` a lot (like me), disabling the feature altogether is a life saver. – mvreijn Nov 29 '18 at 08:24
  • I prefer this answer, because it is very useful to navigate in fullscreen apps with the trackpad, so disabling the feature in preferences was not a good option for me :) Thx for sharing this tip :) – Patrick Ferreira Apr 26 '19 at 09:22
67

You probably just need to get out of alternate console.

Try: tput rmcup

otrejni
  • 1,022
  • 7
  • 12
  • 2
    Great answer A+ – a2f0 Jun 14 '18 at 00:17
  • Much better answer - this lets you scroll the history while still being able to scroll while in vim. Thanks! – JonLuca Jun 13 '19 at 23:16
  • 1
    Can anyone add an explanation to this answer, what are tput and rmcup? I mean specifics, obviously the end result is that it stops the alternate console mode. – Tony Jul 07 '23 at 22:22
24

With iTerm2 you can also hold Option while using the scroll wheel to force window scrolling, in case you do want the wheel to continue to work in vim.

Kyle Smith
  • 2,300
  • 1
  • 17
  • 10
22

Preferences:Advanced:Mouse:Scroll wheel sends arrow keys when in alternate mode

Idiot
  • 221
  • 1
  • 3
  • 2
    I'm having this same exact issue described, but in the newest iTerm Mouse is not under the Prefences:Advanced. Just triggers, smart selection, and semantic history. Anyone know any other way to fix this? – mikeLspohn Jun 23 '16 at 15:55
5

The behavior you're seeing is the expected (and correct) behavior when in the alternate screen. The only problem you have is that you dropped into bash, or whatever, on the alternate screen, possibly while losing a connection or from a killed vim or less. EG you were in less +F /var/log/something and got disconnected.

You see someone has already written that the tput rmcup command will exit to the main screen.

I also went ahead and looked up some terminal control characters. It can be any of: ESC [ ? 1 0 4 9 l or ESC [ ? 1 0 4 7 l or ESC [ ? 47 l (gnu.org/software/screen/manual/html_node/Control-Sequences.html) with a preference to the first.

I'm generally successful in iTerm2 with echo -e '\e[?1049l' I actually don't find rmcup any easier to remember, restore memory of console up?.

dlamblin
  • 43,965
  • 20
  • 101
  • 140
1

I was having this same problem and none of the solutions I read and tried worked. What fixed my problem was turning Secure Keyboard Entry off and now when I use the mouse to scroll it in ssh and in terminal mode on macOS 10.13 it works like I expect it too!

Stan
  • 11
  • 1
1

If you are in a more recent version of MacOS (mine is Monterey 12.0.1) and don't see the "Mouse" option under preferences, you can find this option by going to "Terminal -> Preferences -> Profiles -> Keyboard tab" and then uncheck the box "Scroll alternate screen" as shown in the red box below:

enter image description here

psoo
  • 91
  • 1
  • 3
-1

An update in 2022

In Preferences -> Profiles -> Terminal enter image description here

uncheck the

  • "Save lines to scrollback in alternate screen mode" and
  • "Save lines to scrollback when an app status bar is present"
Lynne
  • 454
  • 7
  • 16