tmux Panes and Layouts
Goal
Within one window, you split panes, give them titles, zoom and synchronize them, and save the scrollback to a file. At the end you build a script that reproduces a development layout in one go.
Why it matters
Panes are more than "a feature that splits the screen". Putting an editor, a server and a log on one screen removes the cost of context switching, and synchronizing panes connected to several servers lets you run the same command everywhere at once. And what people often forget in practice is the working directory of a new pane: if you don't specify it, the pane opens in the home directory and you have to type cd again every time. Finally, tmux's -h does not mean "horizontal split" but left-right arrangement, and almost everyone gets confused by it once. When you talk, it is safer to say left-right / top-bottom.
Steps
- In the
panessession, create a window nameddevand split it into 2 or more panes. - Make the panes of the
devwindow exactly 3. - Set the titles of the three panes to
editor,serverandlogs. - Leave one pane of the
devwindow in the zoomed state. - In the pane whose title is
logs, keeptail -frunning. - In the
devwindow, turn on pane synchronization (synchronize-panes). - In one pane, print
labhub-pane-check, then runseq 1 100, and save the entire scrollback of that pane to/root/tmux/pane.log. The file must contain bothlabhub-pane-checkand100, and must have at least 100 lines. - Create
/root/tmux/dev.sh <세션이름>(the argument is the session name). It creates a session with the name given as the argument without attaching, and puts 3 panes in thedevwindow with the titleseditor/server/logs, and the working directory of theeditorpane must be/root/tmux.
Notes
- Set a pane title with
tmux select-pane -t 대상 -T 제목(target, then title). tmux capture-pane -p -S - -t 대상prints from the very beginning of the scrollback (target: the pane to capture).tmux split-window -c /경로sets the starting directory of the new pane (the path to start in). To inherit the current pane's path, use-c "#{pane_current_path}".- Common mistake 1: if you leave out
-S -in step 7, only the visible screen is captured and you won't reach 100 lines. - Common mistake 2: if you do other work in step 6 with synchronization turned on, the same command goes into every pane. Consider the order of the lab.
Split panes
In the panes session, create a window named dev and split it into 2 or more panes.
prefix + % splits left-right and prefix + " splits top-bottom. From the CLI, it is split-window.
Make a three-way split
Make the panes of the dev window exactly 3.
Split a pane you already split once more. Where it splits depends on which pane is active.
Give the panes titles
Set the titles of the three panes to editor, server and logs.
select-pane has an option for setting a title. You have to set it on each pane separately.
Zoom a pane
Leave one pane of the dev window in the zoomed state.
Toggle it with prefix + z. Leave it zoomed when grading happens.
Follow a log in the logs pane
In the pane whose title is logs, keep tail -f running.
Run tail -f in the pane whose title is logs. You can also specify the target pane and send it with send-keys.
Turn on pane synchronization
In the dev window, turn on pane synchronization (synchronize-panes).
It is a window option, so use setw. When it is on, your input goes to all panes at once.
Save the scrollback to a file
In one pane, print labhub-pane-check, then run seq 1 100, and save the entire scrollback of that pane to /root/tmux/pane.log. The file must contain both labhub-pane-check and 100, and must have at least 100 lines.
capture-pane by default captures only the visible screen. You need an option that sets the starting point to the very top of the scrollback.
Development layout script
Create /root/tmux/dev.sh <세션이름> (the argument is the session name). It creates a session with the name given as the argument without attaching, and puts 3 panes in the dev window with the titles editor/server/logs, and the working directory of the editor pane must be /root/tmux.
If you don't specify it, a new pane opens in the home directory. Look for the option that states the working directory explicitly.