Skip to content

Instantly share code, notes, and snippets.

Last active September 6, 2021 06:37
Show Gist options
  • Save chikamichi/7ac8c98fa589bcde5147016f02899870 to your computer and use it in GitHub Desktop.
Save chikamichi/7ac8c98fa589bcde5147016f02899870 to your computer and use it in GitHub Desktop.
Prevent highlighting text from scrolling down (exiting copy-mode) in tmux
# Optional but convenient: use C-b v to paste the tmux buffer.
bind v paste-buffer
# Do not exit from copy-mode when selecting text.
# @see
# Note: the setting might be renamed MouseDragEndX.
# Depending on whether you activated tmux or vi keybindings (I'm using vi mode):
#bind -temaics-copy MouseDragEnd1Pane copy-selection -x
bind -tvi-copy MouseDragEnd1Pane copy-selection -x

Requires tmux 2.1 or greater. Find your tmux version with tmux -V. If need be, it's quite easy compiling tmux.

A quite recent, easy to miss improvement to tmux is the ability to define a custom behavior for mouse release while in copy-mode.

Copy-mode is this temporary mode you activate (default key bindings) with Ctrl-b + [ and exit from with Ctrl-b + ]. While in copy-mode, you may scroll up and down. You may also enter copy mode directly by scrolling up if you've activated mode-mouse and the proper settings on tmux < 2.1, or if you are using the scroll-copy-mode plugin on tmux 2.1+).

While in copy-mode, if you ever click or highlight and release text, tmux will by default exit from copy-mode, and scroll down back to the prompt. I find this behavior to be very annoying, for I mostly highlight text to help me think through a problem, or to showcase something to someone while pairing; so do some other folks. I'd much rather decide when I want to actually exit from copy-mode and, maybe, paste what I've just selected. YMMV :)

A new setting was introduced in tmux that lets you prevent tmux from scrolling back to normal-mode on mouse release. Refer to the .tmux.conf snippet for details. Thank tmux!

Note: The overall workflow is not perfect though, for if you are in normal-mode (that is, caret is blinking in the prompt) and highlight some text then release, tmux will enter copy-mode as expected but will not leave it to go back to normal-mode, which means you have to press either a key binding or q/Enter to be able to type again (and, maybe, paste your last selection). I can sure live with that just fine, but if you agree going back to normal-mode right away in that case would be the best/least-surprising workflow, feel free to hop on tmux/tmux#337.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment