Using tmux

Posted on Wednesday, January 21, 2015





I am familiar wiht gnu screen commands http://www.gnu.org/software/screen/ [1].  I am trying to transition to using tmux instead of gnu screen.  I need to familiarize myself with the commands tmux uses vs what gnu screen.  With that in mind I am going to explore and write down some common commands in tmux.








If you just want a list of command equivalent (screen -> tmux) just scroll to the bottom of this post, I made a table.





Start a tmux session, detach, list and re-attach.


I am running this on an Ubuntu 14.04 server using Tmux vs 1.9a
To start a new session run the following command


     > tmux





Here is the tmux default screen

To stop this session simply run the exit command


     > exit


This will close the tmux session


If you want to leave the tmux session up you need to detach from it.  To detach from it press Ctrl+b then press d.   (On screen it is Ctrl+a then press d)



After doing that you will see a message like this
"[detached (from session 0)]"

To list all sessions currerntly running, run the following command


     > tmux list-sessions


Or you can use the shorter version


     > tmux ls





In screen the command would be "screen -ls"



If there is only one session open to reattach to it run the following command.


     > tmux attach


Or the shorter version


     > tmux a


In Screen the command would be "screen -a"





Name a session and reattach to it


If you start multiple sessions how do you reattach to the correct one?




Here I have two sessions



The section before the colon contains the name.  The second session has the name "1"

To reattach to it run the following command


     > tmux attach -t 1


Screen's command would be"screen -r 1"


To create a session with the custom name "tail_log" run the following command.


     > tmux new -s tail_log








The session's name is in the lower left hand corner of tmux


To reattach to the named session run the following command.


     > tmux attach -t tail_log







Creating a new session and detaching from it


Here is an example of creating a session and automatically detaching from it.


     > tmux new -s tail_log_2 -d










Creating new windows in a session and moving between them


To create a new window in a session Ctrl+b then press c
To move between windows press Ctrl+b then n  (for next)

Each window has a number starting at 0 to go to a specific window press Ctrl+b then <number>  for example Ctrl+b then 0 goes to the first window.

To name a window press Ctrl+b then  "," Then enter in name of window




Renaming it to "First Window"









Tmux panes


You can display multiple panes (windows) in one tmux session.

Pressing Ctrl+b then %  will start up a new session and split the screen Vertically.







Pressing Ctrl+b then " will split the screen Horizontally
.





To move around the panes press Ctrl+b then o.  You can also use Ctrl+b then Up, Down, Left, or Right.

To close a pane run the exit command in the pane.




Cheat Sheet



Screen command
Tmux command
Open a new session
> screen
> tmux
Close a session
> exit
> exit
Detach from a session
Ctrl+a then d
Ctrl+b then d
Reattach to session
(if a single session)
> screen -a
> tmux a
List open session
> screen -ls
> tmux ls
Attach to a session by name
> screen -r session_name
> tmux a -t session_name
Create a named session
> screen -S tail_log
 > tmux new -s tail_log
Create a new session and detach from it
> screen -d -m -S tail_log
> tmux new -s tail_log -d

Create new window in session
Ctrl+a then c
Ctrl+b then c
Move to next window
Ctrl+a then space bar
Ctrl+b then n
Move to first window
Ctrl+a then 0
Ctrl+b then 0
Split Vertically
Ctrl+a then |
Ctrl+b then %
Split Horizontally
Ctrl+a then S
Ctrl+b then "






References
[1]        Gnu Screen
                        http://www.gnu.org/software/screen/

                Accessed 1/2015










This post is a part of and epic, the Tmux epic.


Epic Goal:   Learn how to use tmux to open 8 panes that each start to tail a different serverlog via ssh, all via a script.

No comments:

Post a Comment