Learn how to use Tmux

why use it?
https://unix.stackexchange.com/questions/479/keep-ssh-sessions-running-after-disconnection
another good alternative is to use nohup (instead of tmux esp. when you want to copy-paste) but with it remember to delete the log files. see
https://unix.stackexchange.com/questions/45913/is-there-a-way-to-redirect-nohup-output-to-a-log-file-other-than-nohup-out for how to direct the output to a file other than nohup.out

tmux list-sessions
tmux attach -t

in case its not clear, you run tmux on the server (not the client)

scrolling in tmux:
press Ctrl+b and then [
now you should be able to scroll up. Press q to quit scroll mode.
note you have to press Ctrl not Cmd
source: https://superuser.com/a/209608/84951

copy-paste in tmux or rather how to copy from tmux to system clipboard:
for this you have to use iterm2 and Go to iTerm2 > Preferences > “General” tab, and in the “Selection” section, check the box which says “Applications in terminal may access clipboard”. source: http://www.rushiagr.com/blog/2016/06/16/everything-you-need-to-know-about-tmux-copy-pasting/
1. Ctrl+b, [ Enter copy(?) mode like before.
2. Move to start/end of text to highlight.
3. Ctrl + space
4. Move to opposite end of text to copy.
5. On Mac use Esc+w
6. this should copy the text to system clipboard
source: https://unix.stackexchange.com/a/58765/108601

ChatGPT:

tmux (Terminal Multiplexer) is a powerful tool that allows you to manage multiple terminal sessions within a single terminal window. It’s especially useful for working on remote servers, multitasking, and organizing your workflow.

Here are the basics you need to know to get started with tmux:

1. Starting a tmux Session

  • To start tmux, simply type:
  tmux

This creates a new tmux session with a single window.

2. Key Concepts

  • Sessions: A session is the overall tmux instance. You can have multiple sessions running in tmux.
  • Windows: Within a session, you can create multiple windows. Each window is like a full-screen terminal.
  • Panes: You can split windows into multiple panes (like tiling windows), allowing you to see and work on multiple terminals at once.

3. Basic Commands

tmux commands are usually invoked by pressing Ctrl+b (the prefix), followed by a key to execute the command. Here are some essential commands:

Session Management:

  • Start a new session:
  tmux new -s session_name
  • List active sessions:
  tmux ls
  • Attach to an existing session:
  tmux attach -t session_name
  • Detach from the current session: (this keeps the session running in the background) Press Ctrl+b, then d.

Window Management:

  • Create a new window: Press Ctrl+b, then c.
  • Switch between windows: Press Ctrl+b, then a number (like 0, 1, etc.) to switch to a specific window. Or use Ctrl+b, then n to move to the next window, or p to move to the previous window.
  • Rename a window: Press Ctrl+b, then , to rename the current window.
  • Close a window: Press Ctrl+d (or just type exit in the terminal to close that window).

Pane Management:

  • Split window horizontally: Press Ctrl+b, then ".
  • Split window vertically: Press Ctrl+b, then %.
  • Navigate between panes: Press Ctrl+b, then the arrow keys (or o to cycle between panes).
  • Resize panes: Press Ctrl+b, then hold the arrow keys to resize the current pane.
  • Close a pane: Press Ctrl+d (or type exit).

4. Saving and Restoring Sessions

One of the great features of tmux is that you can detach from sessions, leaving them running in the background, and reattach later:

  • Detach from the current session: Press Ctrl+b, then d.
  • Reattach to a detached session:
  tmux attach -t session_name
  • Kill a session:
  tmux kill-session -t session_name

5. Customizing tmux

You can customize tmux by editing its configuration file, typically located at ~/.tmux.conf. Common customizations include setting the prefix to something other than Ctrl+b or changing the appearance of the status bar.

Here’s an example of setting Ctrl+a as the new prefix:

# Change prefix to Ctrl+a
unbind C-b
set-option -g prefix C-a
bind C-a send-prefix

After making changes, you can reload the configuration by running:

tmux source-file ~/.tmux.conf

6. Useful Commands

  • List all tmux key bindings: Press Ctrl+b, then ? to bring up the help screen with all available commands.
  • Kill a pane or window: Press Ctrl+b, then x to close a pane. Press Ctrl+b, then & to kill a window.

7. Session Persistence

One of the most powerful features of tmux is session persistence. Even if you close your terminal or get disconnected from a remote server, your tmux sessions will continue running in the background. You can reattach later and pick up right where you left off.


Summary of Commands:

  • Start tmux: tmux
  • Detach from a session: Ctrl+b then d
  • Reattach to a session: tmux attach -t session_name
  • Create a new window: Ctrl+b then c
  • Split pane vertically: Ctrl+b then %
  • Split pane horizontally: Ctrl+b then "
  • Navigate between panes: Ctrl+b then arrow keys

By mastering these basics, you’ll be able to manage multiple terminal sessions efficiently, especially when working with remote servers or complex projects. Let me know if you need more advanced tips!

This entry was posted in Software. Bookmark the permalink.

Leave a comment