Reorder windows in tmux

To reorder windows in tmux, you can use following command

Press CTRL + B or whatever prefix you use, then press . (dot). This will ask you number/position where you need current window moved.

Another method is

This will give you tmux command promt. Now enter

This will move current window to position 0. This only work if position 0 is empty. If another window already present in position 0, use swap-window command.

This with swap current window with window in position 0.

Another useful command is

This will allow you to rename current tmux window.

Using shortcuts

You can edit .tmux.conf file and add following

Now you can use CTRL + SHIFT + LEFT OR RIGHT arrow to move windows.

See tmux

run same command on multiple tmux panes

If you want to run same command on multiple servers, you can use tmux synchronize-panes feature. This will be useful when you need to setup multiple servers, say install nginx/php/mysql on multiple servers.

You can create multiple panes in tmux. SSH into each of the servers from these panes. Now run

Now type

Whatever command you run on one of the pane will be executed on all panes. Be careful when you run server specific commands.

tmux create new window on current directory

When you start a new window or split pane on tmux, it always start with the directory from where you run tmux command.

I prefer it start with current working directory of active tmux window. To do this, edit file

Add

Auto Attach to tmux session on SSH

To start tmux and attach to existing connection on SSH in, edit file

Add

Method 2

You can use following command when connecting to remote sevrer.

SSH Agent forwarding with tmux

Most of the time i connect to remote virtial machine to SSH to other servers. This way i will always have a fixed IP, so i can white list my IP in firewall. I use tmux on this server, so even if i get disconnected, my connection to these servers won’t get disconnected. This is useful when you running some commands that take long to finish.

On this VPS, i don’t keep my SSH keys for security reason, instead i use SSH Agent forwarding with ssh -A option. From my PC, i connect to sshbox with command

If i start a new tmux session, i will be able to login to other servers using my SSH key. If i attach to pre extsing tmux session, my SSH key won’t work. This is because SSH Agent use an environment variable SSH_AUTH_SOCK, this point to a sock file. When you get disconnected, it get deleted.

To fix this problem, edit ~/.tmux.conf file

Add

Create file

with following content

~/.ssh/rc file get executed every time a user connect using SSH. It will set symlink to SSH_AUTH_SOCK location if SSH agent forwarding is enabled.

Install tmux on CentOS 6

tmux is a terminal emulator. It work like screen, but have more features.

CentOS 6 do not come with tmux, you need to enable rpmforge repository.

http://repoforge.org/use/

On CentOS 6 64 bit, run

tmux

CTRL+B % Split window vertically
CTRL+B “ Split window horizontally
CTRL+B c Create new window
CTRL+B d Detach tmux session
CTRL+B $ Rename session
CTRL+B n Next window.
CTRL+B p Previous window.
CTRL+B arrow keys switch between panes.

.tmux.conf

.tmux.conf file is used to configure tmux

Here is the .tmux.conf i use

https://gist.github.com/serverok/f8f729e2c22a5ab1fa6b0d82765bcddc

If you change this file, you need to restart the terminal or run

Scroll Mode in tmux

To scroll, you need to enter scroll mode by pressing

To exit scroll mode, press q