How to scroll in tmux?

Dealing with the inability to scroll back in tmux can be frustrating, and improper attempts might even lead to the application and server hanging. Fortunately, there are two methods for scrolling in tmux: using the mouse and employing keyboard shortcuts. These techniques can be combined and easily configured in your .tmux.conf.

Before delving into the core of the article, let’s first understand how you can issue commands to tmux.

How to Issue Commands to tmux?

To issue commands, you can use the following three methods:

  1. Shortcuts:
    • tmux utilizes a prefix key for issuing commands. By default, this prefix key is CTRL+b. Tmux interprets the keystroke following the prefix as a tmux shortcut.
    • For example, to detach from a session using shortcuts, press the prefix key (CTRL+b) simultaneously and then press ‘d’ to detach.
  2. Command Mode:
    • To enter command mode in tmux, press the prefix key (CTRL+b) followed by ‘:’.
    • This action opens a command prompt at the bottom of the screen that accepts tmux commands.
  3. Command Line:
    • While using a tmux session, you can directly issue commands to the command lines within the sessions.
    • These commands typically start with tmux. For instance, you can use a command line to detach from a tmux session, as demonstrated earlier.

By understanding these methods, you can navigate tmux effectively, avoiding common pitfalls and ensuring a smooth experience.

How to scroll in tmux?

As mentioned, there are two methods for scrolling in Tmux, and you can configure both of them in your .tmux.conf file to use them simultaneously.

Using Your Mouse

Initially, scrolling back in Tmux using your mouse might not be enabled, leading you to explore this post for guidance. To enable mouse scrolling in Tmux, follow these steps:

  1. Press the prefix key or CTRL+b.
  2. Type the following command:

:set -g mouse on

This command activates scrolling using the mouse. If you want to make this setting permanent, add it to your config file without the colon before it. Tmux has two config files: /.tmux.conf for personal settings and /etc/tmux.conf for system-wide settings. It’s recommended to make changes only to the personal file (/.tmux.conf) to conveniently override any system-wide settings.

Using Keyboard Shortcuts

Tmux, by default, allows scrolling with keys. Simply press the prefix key and then the arrow keys to move around in your session. Since Tmux supports two text editors, namely emacs and vi, the keyboard shortcuts for scrolling differ slightly:

  • To test vim-keys or emacs keys in Tmux on your MacBook, use the following:

:setw mode-keys emacs (for emacs)

OR

:setw mode-keys vi (for vim)

Similar to enabling permanent scrolling with the mouse, if you want to make scrolling with your keyboard permanent, use set-window-option instead of setw in your personal config file (~/.tmux.conf).

If your server outputs a significant amount of data and you want to scroll back down to the extreme end without hanging your app while processing data, simply press Enter. This action, unlike other times, prevents your app from hanging during extensive data processing.

Key Combinations for vi in tmux:

Navigating in tmux in vi mode becomes efficient with specific shortcut keys once keyboard scrolling is enabled and made permanent.

Note: When we mention C+(some key), you should press and hold the C key and simultaneously press the other key mentioned. You are not supposed to press C, the plus (+) key, and the other key together. For example, when we say C+b, press and hold the C key and the b key simultaneously (without pressing the plus key, which is used for reference only).

  • To scroll half a page down, use C+d. Conversely, for scrolling half a page up, press C+u.
  • Move to the next page in tmux, while using vi, by pressing C+f.
  • To go back to the previous page, press C+b.
  • For scrolling down, use C+Down or C+e, and to scroll up, use C+Up or C+y.
  • To search again in tmux, press ‘n,’ and to search again in reverse, use ‘N’ (in Caps, of course).
  • To search backward in tmux, while in vi mode, press ‘?,’ and to search forward, use ‘/.’

Key Combinations for emacs in tmux:

Navigating in tmux in emacs mode becomes straightforward with specific shortcut keys once keyboard scrolling is enabled.

  • To scroll half a page down, use M+Down. Conversely, for scrolling half a page up, press M+Up.
  • Move to the next page in tmux, while using emacs, by simply pressing Page Down.
  • To go back to the previous page, press Page Up.
  • For scrolling down, use C+Down, and to scroll up, use C+Up.
  • To search again in tmux, press ‘n,’ and to search again in reverse, use ‘N’ (in Caps, of course).
  • To search backward in tmux, while in emacs mode, press C+r, and to search forward, use C+s.

Read About: Now.gg FNAF 

How to manage panes in tmux?

After creating a session in tmux, you’ll initially have a single window with one panel inside. To split the panel into two, press the prefix key and the % key. Pressing Ctrl+b % in tmux splits a single pane into two, a useful feature when you want outputs from multiple processes visible within a single window. Using the % key with the prefix key splits the pane vertically, while using the prefix key + “, splits the pane horizontally.

Now that you have split panes in tmux, let’s explore how to manage them. Apart from splitting, you can also resize, switch, zoom, force kill, and perform various other actions with the panes inside the window created in tmux.

  • To switch to another pane, press prefix + arrow key.
  • To resize the active pane, press prefix + ALT+arrow.
  • To zoom in on the active pane, press Prefix + z. To zoom out and exit the zoom mode, press Prefix + z again.
  • Use exit to close the active pane.
  • To move the pane above and below, use Prefix + k and Prefix + j, respectively.

If there is an unresponsive process in a pane that you wish to kill, use Prefix + x. To move the left pane, use Prefix + h, while to move the right pane in your tmux window, use Prefix + l. Using Prefix + q will display pane numbers, and using Prefix + o will allow you to jump or toggle to the other pane in the window.

To move all panes, rotating the window up, press Prefix + C-o, while to move all panes, rotating the window down, use Prefix + M-o. To switch to the next layout from a predefined layout, press Prefix + space. To switch to the main horizontal layout from a predefined layout, use Prefix + M-3. To switch to the main vertical layout from the predefined layout, use Prefix + M-2. To switch to the main vertical layout from a predefined layout, use Prefix + M-4, while to switch to a tiled layout from a predefined layout, press Prefix + M-5. To switch to an even-horizontal layout, use Prefix + M-1.

To move the divider up i lines (for a horizontal divider), use tmux bind-key k resize-pane -U [i]. To move the divider left i columns (for a vertical divider), use tmux bind-key k resize-pane -R [i]. To move the divider down i lines (for a horizontal divider), press tmux bind-key k resize-pane -D [i].

Use C-a C-up, C-a C-down, C-a C-left, C-a C-right to resize panes by 1 row/column. To resize panes by 5 rows/columns, use C-a M-up, C-a M-down, C-a M-left, C-a M-right.

To swap the current pane with the pane from the left, use Prefix + }, while to swap the current pane with the pane from the right, use Prefix + {. To go to the last used pane, use Prefix + ;, and to move the pane out of the current window in tmux, use Prefix + !.

How to handle sessions in tmux?

Once you’ve completed your tasks within a tmux session, you can terminate it by either exiting all the panes created inside or by keeping the session in the background for future use.

To detach from your current session, use the prefix key + d. If you prefer tmux to provide options for choosing sessions to detach, use the prefix key + D (note the Caps Lock). This keeps your session running in the background while you are detached from it.

You can reattach to a session later and resume your work. However, to reattach to a session, you need to identify which session you want to attach to first. To view a list of all sessions running in the background, use `tmux ls`. Use Prefix + s to display an interactive session list.

To switch to a previous session, use Prefix + (, while to switch to the next session, use Prefix + ). For more meaningful session names, create a new session using `tmux new -s database`. This creates a new session named “database.”

To rename your current session in tmux, use Prefix + $. To select the last session or the most recently used session in tmux, use Prefix + L. Finally, to destroy all sessions and kill all processes, use `tmux kill-server.”

How to manage windows in tmux?

Upon initiating a tmux session, a single window is automatically created. You can attach multiple windows to the same session, facilitating easy switching between them using various commands.

This functionality proves useful when multitasking and concurrently running multiple jobs. To create a new window, use Prefix + c. To switch to the previous window, use Prefix + p, and to switch to the next window, use Prefix + n.

For targeted window switching using its index number, employ Prefix + 0-9. For instance, to switch to the window with index number 5, use Prefix + 5. To close a window, simply press exit. To choose a window from an interactive list, use Prefix + w.

If dealing with an unresponsive window necessitates a force kill of all processes within it, use Prefix + &.

To horizontally split windows, use Prefix + %, and to vertically split windows, use Prefix + “.

When a window has a content alert, activity, or a bell, switching between windows requires pressing Prefix + M-n. To revert to a previous window with a content alert, activity, or bell, use Prefix + M-p.

By default, tmux names windows based on the process that spawned them. To rename a window, use Prefix + ,.

Leave a Comment