Tmux - work cases and tune up

 Tmux - work cases and tune-up

  1. how to scroll rows in tmux terminal splited windows
  2. how to save sessions, layout, dirs and even progs state in tmux
  3. how to change horizontal split to vertical split in tmux
  4. 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
  1. Enter copy mode: Press your tmux prefix (default is Ctrl+b), then immediately press the [ key. An indicator in the top right of the pane will show the current line number.
  2. 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.
  3. 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. 
  1. Edit your ~/.tmux.conf file with an editor like nano or vim.
  2. Add the following line (for tmux versions 2.1 and up):
    bash
    set -g mouse on
    
    For older versions, use set -g mode-mouse on.
  3. Reload your tmux configuration using the command line within a tmux session:
    • Press the prefix Ctrl+b.
    • Type : to enter the tmux command prompt.
    • Type source-file ~/.tmux.conf and press Enter.
  4. 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 Plugin
The 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-resurrect plugin to your ~/.tmux.conf file:
    bash
    set -g @plugin 'tmux-plugins/tmux-resurrect'
    
  • Run the plugin manager by pressing prefix + I (capital I, where prefix is Ctrl+b by default) inside a tmux session to fetch and source the plugin. 
2. Saving the Layout
  • To manually save your current tmux environment, press prefix + 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-continuum plugin alongside tmux-resurrect. Add set -g @plugin 'tmux-plugins/tmux-continuum' to your ~/.tmux.conf and press prefix + I to install it.
  • Scripting: You can also write a custom bash script that uses tmux commands to recreate a specific, predefined layout. The list-windows command 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. 
  1. Press your prefix key (by default, Ctrl + B).
  2. 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 press Alt + 2 to arrange panes in an even-vertical layout.
  • Alternatively, you can access the tmux command prompt:
    1. Press your prefix key (Ctrl + B).
    2. Press the colon key (:).
    3. Type select-layout even-vertical and press Enter or select-layout even-horizontal . 
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. 
  1. Identify pane numbers: Press your prefix key and then Q to briefly display pane numbers.
  2. 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+b then %.
  • Horizontal Split (Top/Bottom): Press Ctrl+b then ".
  • Close Current Pane: Press Ctrl+b then x.
  • Switch Between Panes: Press Ctrl+b then arrow keys.
  • Toggle Layouts: Press Ctrl+b then Space to 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 -v
changing the layout (evenly - all windows will be in the same size!):
bind H select-layout even-horizontal
bind V select-layout even-vertical

Comments

Popular posts from this blog

Открываем порт для сервера Minecraft на роутере mikrotik (команда для терминала в WinBox)

Интересное о Формальдегиде