tmux

#tmux basic concept
![Screen Shot 2014-08-03 at 12.50.59 PM.png](https://qiita-image-store.s3.amazonaws.com/0/49501/726a7bfe-53c1-15d1-4d70-acf2f4a3e32a.png)


#command
## general
### attach existing session

```tmux attach -t <settion name>```

### detach current session

```prefix + d```

###reload configuration file again

```prefix + :source-file ~/.tmux.conf```

###List all key bindings

```prefix + ?```

##session
###Detach the current session.

```prefix + d```

###Select a new session for the attached client interactively.

```prefix + s```

![Screen Shot 2014-08-03 at 1.03.10 PM.png](https://qiita-image-store.s3.amazonaws.com/0/49501/67e2b8df-5fc5-afa4-3608-8ba064faa03b.png)

###Kill last joined session.

```tmux kill-session```

##window
###Create a new window

```prefix + c```

###Change to the previous window

```prefix + p```

###Change to the next window

```prefix + n```

###Choose window from a current session

```prefix + w```

![Screen Shot 2014-07-25 at 0.29.48.png](https://qiita-image-store.s3.amazonaws.com/0/49501/f8a8c110-9b06-74c9-0877-ba0ae515140e.png)


##pane
###Split specific pane

- "bash" is window name
- "0" is pane name
- split "0" pane on "bash" window horizontally

```
tmux split-window -t "bash.0" -h
```

###Split the current pane horizontally.

```prefix + "```

![Screen Shot 2014-08-03 at 5.21.02 PM.png](https://qiita-image-store.s3.amazonaws.com/0/49501/82687edd-041d-af7b-6af9-3e4cec8a6ef9.png)

###Split the current pane vertically.

```prefix + %```

![Screen Shot 2014-08-03 at 5.22.31 PM.png](https://qiita-image-store.s3.amazonaws.com/0/49501/e94e9058-d1cd-ed74-0648-eda0d8c01b12.png)

###Select the next pane in the current window.

```prefix + o```

###Break the current pane out of the window.

```prefix + !```

###Move pane from the specifed window.

```join-pane -s :<window name>```

###Move pane to the specifed window.

```join-pane -t :<window name>```

###Show the time.

```prefix + t```

![Screen Shot 2014-08-03 at 1.22.53 PM.png](https://qiita-image-store.s3.amazonaws.com/0/49501/4f5eb136-ca3d-5324-0ff1-9cf91a265ffa.png)

#tmux.conf sample

```.tmux.conf
set -g mode-mouse on
set -g mouse-resize-pane on
set -g mouse-select-pane on
set -g mouse-select-window on

bind -n M-Space next-window
bind y send-keys 'set-window-option synchronize-panes' Enter

## unbind default prefix key
unbind-key C-b

## make Ctrl+j prefix key
set-option -g prefix C-j
bind-key C-j send-prefix

# synchronize shortcut key
bind e setw synchronize-panes on
bind E setw synchronize-panes off
```

#tmux tips
## share session with other operator
http://readystate4.com/2011/01/02/sharing-remote-terminal-session-between-two-users-with-tmux/

##tmuxlogin.sh
Open multiple tmux panes, each logged into a different server
https://github.com/azat/scripts/blob/master/tmuxlogin.sh

``` tmuxlogin.sh -w session -t window server1 server2 server3 server4```

↓
![Screen Shot 2014-08-03 at 5.03.06 PM.png](https://qiita-image-store.s3.amazonaws.com/0/49501/a7b0c823-14e1-6622-cee0-c928ee16a40c.png)


##cmatrix
![Screen Shot 2014-07-21 at 18.20.00.png](https://qiita-image-store.s3.amazonaws.com/0/49501/e6849403-1361-7fd1-a832-984c83a715f9.png "Screen Shot 2014-07-21 at 18.20.00.png")

http://qiita.com/yuyuchu3333/items/77889084a1d19223f2b1

```~/.tmux.conf
set -g lock-after-time 600
set -g lock-command "cmatrix -s -b"
```

#tmux installation
##source installation

```bash
# get source file
wget http://downloads.sourceforge.net/tmux/tmux-1.8.tar.gz
wget -k --no-check-certificate https://github.com/downloads/libevent/libevent/libevent-2.0.21-stable.tar.gz

# libevent installation
tar zxvf libevent-2.0.21-stable.tar.gz
./configure --prefix=/home/toripiyo 2>&1 | tee _configure.log
make 2>&1 | tee _make.log
make install 2>&1 | tee _make_install.log

# copy behavior settings
## Copy from tmux buffer to system buffer (clipboard)
http://www.rushiagr.com/blog/2016/06/16/everything-you-need-to-know-about-tmux-copy-pasting/
Go to iTerm2 > Preferences > “General” tab, and in the “Selection” section, check the box which says “Applications in terminal may access clipboard” if it isn’t checked.

# tmux installation
DIR="$HOME/lib"
./configure CFLAGS="-I$DIR/include" LDFLAGS="-L$DIR/lib" --prefix=/home/toripiyo/tmux-1.8 2>&1 | tee _configure.log
make 2>&1 | tee _make.log
make install 2>&1 | tee _make_install.log
```

# copy behavior settings
## Copy from tmux buffer to system buffer (clipboard)
http://www.rushiagr.com/blog/2016/06/16/everything-you-need-to-know-about-tmux-copy-pasting/
Go to iTerm2 > Preferences > “General” tab, and in the “Selection” section, check the box which says “Applications in terminal may access clipboard”

# tmux trouble shooting
## tmux cannot open atom from console

```
[17:38:13 - 15-02-01] /Users/toripiyo/program/rails/housmart/db % atom seeds.rb
LSOpenURLsWithRole() failed for the application /opt/homebrew-cask/Caskroom/atom/latest/Atom.app with error -10810.
```

1. upgrade reattach-to-user-namespace.

```
[18:59:14 - 15-02-01] /Users/toripiyo % reattach-to-user-namespace -l
warning: unsupported new OS, trying as if it were 10.6-10.8
fatal: _vprocmgr_move_subset_to_user failed
[18:59:20 - 15-02-01] /Users/toripiyo % brew upgrade reattach-to-user-namespace
==> Upgrading 1 outdated package, with result:
reattach-to-user-namespace 2.3
==> Upgrading reattach-to-user-namespace
==> Downloading https://github.com/ChrisJohnsen/tmux-MacOSX-pasteboard/archive/v2.3.tar.gz
######################################################################## 100.0%
==> Patching
==> make
?  /usr/local/Cellar/reattach-to-user-namespace/2.3: 6 files, 48K, built in 6 seconds
```

2. add following configuration on .tmux.conf
http://befool.co.jp/blog/jiska/upgrade-to-yosemite-after-that/

```
# Fix LSOpenURLsWithRole() error on OS X. Requires reattach-to-user-namespace
# # to be installed.
set-option -g default-command "which reattach-to-user-namespace > /dev/null && reattach-to-user-namespace -l $SHELL || $SHELL"
```




#reference
tmux cheet sheet
https://gist.github.com/Geraint/7724164
1分で実現できるtmuxのTips x3 (ついでにinstall to Mac,CentOS,Debian/Ubuntu)
http://qiita.com/nntsugu@github/items/107cbd6cd6db2aa46172
Sharing remote terminal session between two users with Tmux
http://readystate4.com/2011/01/02/sharing-remote-terminal-session-between-two-users-with-tmux/