de Alexey Samoshkin

tmux în practică: buffer-ul scrollback

Diferența dintre bufferele scrollback terminal și tmux și modul de ajustare a modului copiere, scroll și selectarea mouse-ului a comportamentului tmux.

Aceasta este a treia parte a mea tmux în practică serie de articole.

De obicei, emulatoarele de terminal implementează un buffer scrollback, astfel încât să puteți explora ieșirea anterioară, atunci când se deplasează din vizualizare. tmux, ca și alte aplicații terminale cu ecran complet, cum ar fi vim, rulează în așa-numitul tampon de ecran alternativ al unui terminal părinte. Tamponul alternativ are dimensiuni exacte de lățime și înălțime ca dimensiune fizică a ferestrei.

Există mai multe efecte ale utilizării tamponului alternativ:

  • Orice ieșire, care depășește partea vizibilă a tamponului alternativ, se pierde. De îndată ce liniile ies din viziune, ele se pierd. Pentru a preveni pierderea istoricului, tmux implementează propriul buffer de scrollback „interior”. Consecința acestui lucru este că nu puteți lucra cu tmux inner scrollback la fel ca în mod obișnuit în terminal.
  • Orice ieșire produsă în interiorul tmux (același lucru este valabil pentru vim, nano, man, less și așa mai departe) nu se revarsă în istoricul scrollback al terminalului exterior. Când închideți aplicația cu ecran complet, reveniți la aceeași stare când ați lansat aplicația și nu mai vedeți ieșirea din interiorul aplicației.

În practică, dacă te obișnuiești să derulezi înapoi folosind ⌘↑ în iTerm și dacă aveți de gând să faceți același lucru în cadrul sesiunii tmux de rulare, veți controla și derula bufferul scrollback al iTermului exterior, mai degrabă decât bufferul scrollback interior tmux.

Soluția este de a utiliza controale specifice tmux pentru a accesa propriul buffer de derulare: Ctrl-b atunci [ to enter copy mode, use Down/Up arrows or PageDown and PageUp keys, q or Enter to exit copy mode.

Some people who find this annoying — configure tmux scrollback buffer to be allowed to be shown up in parent terminal scrollback buffer — so they can just use familiar scrolling controls. See this post. However, this solution is limited to having tmux session with 1 window and 1 pane only. And when you detach/close a tmux session, the parent terminal is polluted with tmux window’s output.

Personally, I use tmux scrollback without hacks above, but tweak it’s configuration to be more friendly and familiar.

First of all, I don’t like prefix,[ to enter copy mode. I’ve gotten used to ⌘↑ to start scrolling in iTerm, and I’ve added following root keybinding:

# trigger copy mode bybind -n M-Up copy-mode

Once you’re in copy mode, you can continue pressingM-Up to scroll 1 line up. The usual PageDown and PageUp controls are available to scroll by whole screen, and extra M-PageDown and M-PageUp to scroll by a half of screen (really convenient).

# Scroll up/down by 1 line, half screen, whole screenbind -T copy-mode-vi M-Up              send-keys -X scroll-upbind -T copy-mode-vi M-Down            send-keys -X scroll-downbind -T copy-mode-vi M-PageUp          send-keys -X halfpage-upbind -T copy-mode-vi M-PageDown        send-keys -X halfpage-downbind -T copy-mode-vi PageDown          send-keys -X page-downbind -T copy-mode-vi PageUp            send-keys -X page-up

Moreover, even when I’m inside tmux session, I can still continue using ⌘↑ and ⌘↓ to control the inner tmux scrollback buffer, rather that iTerm’s one. It’s possible by using custom iTerm profile with some keybindings overridden to trigger tmux actions. So ⌘↑ pressed in iTerm just sends M-Up keystroke to tmux session.

Read my previous part of “tmux in practice” series for more details: tmux in practice: iTerm2 and tmux integration.

Another tmux’s default I would prefer to change is the mouse wheel scroll. It scrolls by 5 rows, which feels like a big jump. Let’s reduce it to scroll by 2 rows:

# When scrolling with mouse wheel, reduce number of scrolled rows per tick to "2" (default is 5)
bind -T copy-mode-vi WheelUpPane select-pane ; send-keys -X -N 2 scroll-upbind -T copy-mode-vi WheelDownPane select-pane ; send-keys -X -N 2 scroll-down

Well, now let’s talk about copying text once you’re in copy mode. I get used to copy text using mouse. Let’s enable mouse support:

set -g mouse on

By default, when you select text with the mouse in tmux, it get’s copied to buffer, and you’re immediately dropped out of the copy mode. Your current scroll position is reset by the end of the output, and you’re put in a command prompt mode. Let’s see this in action:

tmux in practica buffer scrollback
Kicked off copy mode on mouse drag end

As you notice, each time I select text with the mouse, it kicks me off the copy mode. It’s really annoying. Usually when I’m stuck with some task, I tend to select some text here or there just to meditate (it helps me to focus on ?). Or you might want just to select some text to highlight for you colleague sitting next to you.

So let’s tweak this. We don’t want to be kicked off the copy mode. We don’t want the selection cleared on the mouse drag end event. Text from the selection can be copied on the mouse left click afterwards.

# Do not copy selection and cancel copy mode on drag end event# Prefer iTerm style selection: select, then mouse click to copy to bufferunbind -T copy-mode-vi MouseDragEnd1Panebind -T copy-mode-vi MouseDown1Pane select-pane ;  send-keys -X copy-pipe "pbcopy" ;  send-keys -X clear-selection

Let’s check out the result:

1611574264 580 tmux in practica buffer scrollback
Stay in copy mode and do not clear selection on mouse drag end event

To access the copy buffer items and paste the most recent item, use C-p and p:

bind p paste-bufferbind C-p choose-buffer

That’s it for today. Stay tuned. In the next part of “tmux in practice” series, we’ll talk about clipboard integration, and how to share text copied inside tmux with the system clipboard (both locally and when working remotely, on OSX and Linux).

BTW, you can see all those configuration tweaks in action, just checkout my tmux-config repo.

Trick

If you want to bypass tmux copy mode altogether and select text via iTerm, just hold <Opt> key while dragging you mouse.

shell — What exactly is scrollback and scrollback buffer? — Unix & Linux Stack Exchange — https://unix.stackexchange.com/questions/145050/what-exactly-is-scrollback-and-scrollback-buffer

tmux scrollback with iTerm2 • dan.carley.co — https://dan.carley.co/blog/2013/01/11/tmux-scrollback-with-iterm2/

tmux copy mouse selected text to clipboard automatically on mouse release — Stack Overflow — https://stackoverflow.com/questions/36815879/tmux-copy-mouse-selected-text-to-clipboard-automatically-on-mouse-release

keyboard shortcuts — tmux — scroll up/down with shift + page up/down into a pane — Super User — https://superuser.com/questions/702189/tmux-scroll-up-down-with-shift-page-up-down-into-a-pane

[question/request] copiere fără a selecta automat un panou? · Numărul # 1021 · tmux / tmux – https://github.com/tmux/tmux/issues/1021

ssh – Lăsarea tmux scrollback în terminal (iTerm2) – Stack Overflow – https://stackoverflow.com/questions/12865559/leaving-tmux-scrollback-in-terminal-iterm2

linie de comandă – Utilizați bara de derulare a terminalului cu tmux – Super User – https://superuser.com/questions/310251/use-terminal-scrollbar-with-tmux