How to send-key to break out of scroll mode in tmux

tmux

I have a key-mapping in VIM that looks like this:

map <F5> :silent !tmux send-keys -t 0:0 C-m "python %" C-m<cr>

When I press F5, vim runs my current Python script in tmux session 0, window 0.
However, if window 0 is currently in a scroll state (i.e. Shift-PgUp or mouse scroll), tmux will run the command but will not scroll on new output.

How do I send a key to the tmux window that will break it out of scroll mode? (I want it to scroll on output)

Note: I normally get out of scroll-mode by pressing Esc, but I couldn't figure out how to send an Escape key using tmux send-keys. I tried sending C-[ but that didn't work.

Best Answer

You can send literal escape (and newline) with ANSI-C quoting (there's more about that here), e.g. to send escape to 0:0:

tmux send-keys -t 0:0 $'\e'

For your mapping, assuming I understand it correctly, you could do something like this:

map  <F5> :silent !tmux send-keys -t 0:0 $'\e'"python"$'\n'<CR>

Note that the escape will always be sent, so to work around the issue of the shell receiving the escape, you could send an extra newline:

map  <F5> :silent !tmux send-keys -t 0:0 $'\e'$'\n'"python"$'\n'<CR>
Related Question