Tmux - work cases and tune up
Tmux - work cases and tune-up
- how to scroll rows in tmux terminal splited windows
- how to save sessions, layout, dirs and even progs state in tmux
- how to change horizontal split to vertical split in tmux
- how to add horizontal and vertical splits in tmux + key bindings
For those who don't aware what is it - this s terminal shell and you can install it for your distro like sudo apt-get install tmux (for Debian like Ubuntu etc.)
1. How to scroll rows in tmux terminal spitted windows
1 . To scroll rows within a specific
tmux split window (pane), you must first enter copy mode. This allows you to navigate and review the pane's scrollback buffer. Scrolling with Keyboard
- Enter copy mode: Press your
tmuxprefix (default is Ctrl+b), then immediately press the [ key. An indicator in the top right of the pane will show the current line number. - Scroll:
- Use the arrow keys (↑ and ↓) to move one line at a time.
- Use Page Up and Page Down (or Shift+Page Up/Shift+Page Down in some terminal emulators) to jump an entire page.
- Exit copy mode: Press the q key or Esc to return to normal mode.
Scrolling with Mouse
For those who prefer using the mouse wheel, you need to enable mouse mode in your
~/.tmux.conf configuration file. - Edit your
~/.tmux.conffile with an editor likenanoorvim. - Add the following line (for tmux versions 2.1 and up):For older versions, usebash
set -g mouse onset -g mode-mouse on. - Reload your
tmuxconfiguration using the command line within atmuxsession:- Press the prefix Ctrl+b.
- Type
:to enter thetmuxcommand prompt. - Type
source-file ~/.tmux.confand press Enter.
- Scroll: You should now be able to use your mouse scroll wheel directly within any pane, even in split windows. This also enables clicking to select panes and resize them with the mouse.
--------------------------------------
2 . To save a tmux layout,
the recommended method is to use the tmux-resurrect plugin, as tmux does not have a built-in feature for saving sessions to a file by default. Using the
tmux-resurrect PluginThe
tmux-resurrect plugin saves all session details, including window and pane layouts, current working directories, and even running programs, to a file that can be restored later. 1. Installation (using Tmux Plugin Manager - TPM)
- Install TPM first if you don't have it. Instructions can be found on the TPM GitHub page.
- Add the
tmux-resurrectplugin to your~/.tmux.conffile:bashset -g @plugin 'tmux-plugins/tmux-resurrect' - Run the plugin manager by pressing
prefix + I(capital I, whereprefixisCtrl+bby default) inside atmuxsession to fetch and source the plugin.
2. Saving the Layout
- To manually save your current
tmuxenvironment, pressprefix + Ctrl+s. - A notification message
'tmux environment saved'will briefly appear in the status bar.
3. Restoring the Layout
- To restore the last saved environment, press
prefix + Ctrl+r. - The saved sessions, windows, and pane layouts will be restored.
Alternative Methods
- Automatic Saving/Restoring: For continuous, automatic saving (e.g., every 15 minutes) and automatic restoration on startup, you can use the complementary
tmux-continuumplugin alongsidetmux-resurrect. Addset -g @plugin 'tmux-plugins/tmux-continuum'to your~/.tmux.confand pressprefix + Ito install it. - Scripting: You can also write a custom bash script that uses
tmuxcommands to recreate a specific, predefined layout. Thelist-windowscommand can display layout data that you can use in your scripts. - Tmuxinator/Tmuxp: For project-specific layouts defined in YAML files, tools like Tmuxinator or Tmuxp can be used to launch predefined workspaces with a single command.
--------------------------------------------------------------
3. how to change horizontal split to vertical split in tmux
You can change an existing horizontal split to a vertical split in tmux by cycling through the preset layouts with a keyboard shortcut or by using the
select-layout command. There is no direct command to "transpose" an existing single horizontal split to a single vertical one while keeping the same panes.Method 1: Cycle through preset layouts
The quickest method is to use the default keybinding to cycle through the available window layouts. This will rearrange your panes into one of the predefined configurations, including
even-horizontal and even-vertical. - Press your prefix key (by default,
Ctrl + B). - Press the spacebar (
Space).
Repeat this until you reach the desired vertical layout.
Method 2: Use a specific preset layout command
If you want to apply a specific layout immediately, you can use a command or a keybinding that targets a particular preset. The default bindings for these are often mapped to
Alt (or Meta) keys combined with numbers. - Press your prefix key (
Ctrl + B), release, and then pressAlt + 2to arrange panes in an even-vertical layout. - Alternatively, you can access the tmux command prompt:
- Press your prefix key (
Ctrl + B). - Press the colon key (
:). - Type
select-layout even-verticaland pressEnter orselect-layout even-horizontal.
- Press your prefix key (
Method 3: Move a pane to create a vertical split (for specific cases)
If you have a complex layout and want more control over which pane goes where, you can move specific panes. This is more involved and generally requires using the command line within tmux.
- Identify pane numbers: Press your prefix key and then
Qto briefly display pane numbers. - Use
swap-pane: For example, to swap the current pane's position with an "up" or "down" pane in the index order, press your prefix key followed by{or}.
For a full explanation of advanced options, including how to configure custom keybindings in your
~/.tmux.conf file, you can consult the tmux man page.--------------------------------------
4. how to add horizontal and verticl splits in tmux + key bindings
Tmux Split Pane Keybindings
- Vertical Split (Side-by-Side): Press
Ctrl+bthen%. - Horizontal Split (Top/Bottom): Press
Ctrl+bthen". - Close Current Pane: Press
Ctrl+bthenx. - Switch Between Panes: Press
Ctrl+bthen arrow keys. - Toggle Layouts: Press
Ctrl+bthenSpaceto cycle through layout arrangements.
Command Line Alternatives
You can also run these commands directly in your terminal or via the
tmux command prompt (Ctrl+b followed by :): - Vertical:
tmux split-window -v. - Horizontal:
tmux split-window -h.
Customizing Key bindings
To make splits more intuitive (e.g., using
| for vertical and - for horizontal), add these to your ~/.tmux.conf file: bash
creating new windows:
bind | split-window -h
bind - split-window -vchanging the layout (evenly - all windows will be in the same size!):bind H select-layout even-horizontal
bind V select-layout even-vertical
Comments