Bon Voyage

Using tmux on macOS Catalina 본문

설치

Using tmux on macOS Catalina

nangkyeong 2019. 12. 30. 00:21

Introduction

tmux is a terminal multiplexer.

It enables a number of terminals to be created, accessed, and controlled from a single screen. tmux may be detached from a screen and continue running in the background, then later reattached.

Using tmux, you can keep the remotely connected session even though you are not in front of your computer, which is convenient when you have to call it a day but there are still more things to be done.

Installation

brew install tmux

Start a session

tmux new -s <session_name>
  • Just typing tmux will by default start with one window and a single panel inside.
  • Any number of tmux instances may connect to the same session, and any number of windows may be present in the same session.
  • Once all sessions are killed, tmux exits.

Commands in tmux

All commands in tmux are triggered by a prefix key followed by a command key.

  • By default, tmux uses C-b as prefix key. (press the Ctrl and b keys at the same time.)
    • In this emacs notation C- means “press and hold the Ctrl key”
  • For example, C-b " means press ctrl + b then type "

Customizing commands

You can create your own configuration file to custom command key binding

By default, tmux loads the system configuration file from /usr/local/etc/tmux.conf, if present, then looks for a user configuration file at ~/.tmux.conf. You can check the detail of it in the manual page. Just type man tmux

# make your own configuration file, you can choose where to store the file
vim ~/.tmux.conf 
# then write your configuration...
# '.' before 'tmux' means it is hidden file

Example of tmux.conf file

Below is some of custom configuration! You can add or change bindings for the command

  • Note that all the command keys you bound, it should be coupled with C-b (prefix key)

      # split panes using | and -
      bind | split-window -h
      bind - split-window -v
      unbind '"'
      unbind %
    
      # reload config file (change file location to your the tmux.conf you want to use)
      bind r source-file ~/.tmux.conf
    
      # Enable mouse mode (tmux 2.1 and above)
      set -g mouse on

Here, command keys I've changed are:

  • splitting panes with | for vertical split, - for horizontal split.
  • reloading the config file by r
  • set mouse mode on (you can change pane size or choose windows with your mouse)

If you want more customization

  • You can even change the prefix key C-b as you prefer.

  • You can search for more configuration! The link below will help you with this.

 

Making tmux Pretty and Usable - A Guide to Customizing your tmux.conf

Customize the look and feel of tmux

www.hamvocke.com

 

Panes

Splitting panes commands

splitting a left and a right pane is **C-b %* , top and bottom is **C-b "** by default.
*(Custom keys are C-b | and C-b - respectively, if you have set the configuration file.)

Navigating and Closing the pane

  • navigation: C-b <arrow key>: ctrl + b + <arrow key>
  • full screen : C-b z
    • C-b z again to shrink it back to its previous size

 

Windows

Windows in tmux is like creating new virtual desktops.

Windows are presented on the status bar.

  • Creating new window: C-b c
  • Switching between windows:
    • C-b p (previous window)
    • C-b n (next window)
    • C-b <window number> (specific window)
  • Rename current window's name: C-b ,

Closing panes and windows

exit or just press ctrl + d

 

Session Handling

You can either kill get rid of the session when you're done with it or keep the session in the background for later reuse.

Detaching session

  • To detach your current session use C-b d, use C-b D to select which of your sessions you want to detach.
  • This will detach your session but will leave you’re doing in that session running in the background.
  • When you detach, you'll see the message that it's detached

Check running sessions

You can see the check running sessions with

tmux ls
tmux has-session [-t target_session]

Attaching session by session number

To connect to that session you start tmux again but this time tell it which session to attach to:

tmux attach -t <session_number> | <session_name>
  • For example, -t 0 that tells tmux to attach session 0.
  • "0" is the first part of your tmux ls output.

Attaching session by session name

You can give your sessions names or rename them as you prefer.

  • Create your next session with a preferred name: **tmux new -s <session_name>**
  • Rename your existing session : **tmux rename-session -t <session_number> <session_name>**

Then the next time you attach to that session you simply use **tmux attach -t <session_name>**

Killing sessions

tmux kill-session #kill the most recently created session
tmux kill-session -t <session_number> | <session_name>

or you can kill server, which will kill all the sessions on it.

tmux kill-server

 

Useful short-cuts

  • C-a : go to the beginning of the line
  • with prefix key, C-b
    • $ : Rename the current session.
    • & : Kill the current window.
    • ' : Prompt for a window index to select.
    • { or } : swap panes
    • alt (option) + : change pane size
    • ? : see all available commands

Check the full list of command keys on the link below:

 

tmux(1) - OpenBSD manual pages

tmux — terminal multiplexer tmux [-2Cluv] [-c shell-command] [-f file] [-L socket-name] [-S socket-path] [command [flags]] tmux is a terminal multiplexer: it enables a number of terminals to be created, accessed, and controlled from a single screen. tmux m

man.openbsd.org

 

 

References

 

tmux/tmux

tmux source code. Contribute to tmux/tmux development by creating an account on GitHub.

github.com

 

tmux(1) - OpenBSD manual pages

tmux — terminal multiplexer tmux [-2Cluv] [-c shell-command] [-f file] [-L socket-name] [-S socket-path] [command [flags]] tmux is a terminal multiplexer: it enables a number of terminals to be created, accessed, and controlled from a single screen. tmux m

man.openbsd.org

 

A Quick and Easy Guide to tmux

Become a master of tmux in a few minutes

www.hamvocke.com

Comments