summaryrefslogtreecommitdiff
path: root/bookmarks/tmux part 1.txt
blob: 56f7908de9918f06064b5faf88d4a8f91e0e5341 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
---
title: TMUX – The Terminal Multiplexer (Part 1)
date: 2012-06-22T13:47:51Z
source: http://blog.hawkhost.com/2010/06/28/tmux-the-terminal-multiplexer/
tags: writing, tools

---

_tmux_ is similar to _screen_ as it lets you run numerous TTY's in the same terminal window. It supports some very cool and intuitive features natively as well as a much more readable configuration syntax (ever looked at a .screenrc file?).

### Why TMUX over Screen?

Well according to the FAQ for _tmux_ it has the following advantages over screen:

* A clearly defined cilent/server model (windows are their own clients which allows flexibility on how you handle windows. You can attach and detach different windows in different sessions without any issues)
* Consistent, well-documented command interface. (You can use the same commands interactively as in the .tmux.conf file, more on that later)
* Easily scriptable
* Multiple paste buffers
* Vi & Emacs keybindings
* A more usable status line syntax (which also allows you to embed the output of a shell command, handy indeed.

### Default keybindings & Functionality

The default keybindings for tmux are actually pretty intuitive, though if you're used to screen you'll likely get a little peeved with the default action binding of _C-b_, though this is easily changed to mimic screens behavior:

***NOTE* **If you're like me the _Ctrl-b_ binding isn't horribly intuitive especially if you're used to _screen_. You can rebind this by putting the following in **_~/.tmux.conf_:**

> **set -g prefix Ctrl-a**

* **_Ctrl-b c_** Create new window
* **_Ctrl-b d_** Detach current client
* _**Ctrl-b l**_ Move to previously selected window
* _**Ctrl-b n**_ Move to the next window
* _**Ctrl-b p**_ Move to the previous window
* _**Ctrl-b &**_ Kill the current window
* _**Ctrl-b ,**_ Rename the current window
* _**Ctrl-b %**_ Split the current window into two panes
* _**Ctrl-b q**_ Show pane numbers (used to switch between panes)
* _**Ctrl-b o**_ Switch to the next pane
* _**Ctrl-b ?**_ List all keybindings

Now these are pretty self explanatory – the real magic (for me) of _tmux_ is the ease of modifying the default behavior to do what _you_ want, but first things first: let's explore the default behavior of _tmux._

### Basic Window Handling

Start up tmux with the tmux command and you should be greeted with a simplistic terminal window that resembles screen – the only difference is it has a default status bar which is nice (easily added to screen as well).

![Terminal Multiplexer][1]

Now let's create a couple of windows and go through them (we'll be using the default bindings). Hit _**Ctrl-b c**_ a few times to create a few windows, you should notice that there are more tabs in the status bar.

Now if you're like me you like to have descriptive names of which each window is for, so let's rename them by hitting _**Ctrl-b ,**_. It should prompt you to rename the current window – type anything you want and hit enter. Now the current window is renamed to what you specified. Now going forward I'm going to have two windows open respectively named "window1″ and "window2″.

Once you rename your windows lets switch back and forth. We have several different ways of switching windows, so I'll go over the ones I personally use:

* _**Ctrl-b n**_ (Move to the next window)
* _**Ctrl-b p**_ (Move to the previous window)
* _**Ctrl-b l**_ (Move to the previously selected window)
* _**Ctrl-b w**_ (List all windows / window numbers)
* _**Ctrl-b <window number>**_ (Move to the specified window number, the default bindings are from 0 – 9)

Now these ones fairly self explanatory however they don't really cater to a lot of different windows. What if you have 10+ windows open? It becomes quite tedious to find the window you want – but don't fret! Tmux has a _find-window_ option & keybinding. Type _**Ctrl-b f**_ and type in the window name you want (it actually searches for the window so you can type in only part of the name of the window you're looking for).

You can also get a list of the windows in the current session by executing the _list-windows_ command. To execute commands interactively you type _**Ctrl-b :**_ which will bring up a text prompt. From there you can execute any command _tmux_ supports interactively (tab completion is supported).

![Terminal Multiplexer - Interactive Prompt][2]![Terminal Multiplexer - List Windows][3]

### Basic Pane Handling

One of the most powerful features tmux offers is the ability to split up your current window into "panes". Anyone whose familiar with tiling windows managers will feel quite at home. It's a bit difficult to explain this in words so a simple screenshot will suffice:

![Terminal Multiplexer - Split Windows][4]

Now here are some basic key bindings and commands to split the terminal window (vertically and horizontally) and to switch between them

* _**Ctrl-b %**_ (Split the window vertically)
* _**Ctrl-b : "split-window"**_ (Split window horizontally)
* _**Ctrl-b o**_ (Goto next pane)
* _**Ctrl-b q**_ (Show pane numbers, when the numbers show up type the key to goto that pane)
* _**Ctrl-b {**_ (Move the current pane left)
* _**Ctrl-b }**_ (Move the current pane right)

Now some obviously the default bindings don't encompass some of features, such as splitting horizontally. I personally rebind the keys so "|" splits the current window vertically, and "-" splits it horizontally. Not the easiest things to type, though easy to remember.

You can achieve this by putting the following in **_~/.tmux.conf _or by typing it in the interactive prompt (_Ctrl-b :_). Keep in mind if you do the latter it will only be in effect for that session:**

> **unbind %
bind | split-window -h
bind – split-window -v **** **

### Advanced Window Handling

Now that we went over the basics lets dive a little deeper into some "advanced" features of _tmux_. This includes moving windows around, linking windows together, switching windows from different sessions and much more. By default _tmux_ doesn't have key bindings for these features, so we'll be entering them in the interactive dialog (accessed by typing _**Ctrl-b :**_) – keep in mind _tmux_ is very scriptable and you can easily create your own key bindings for all of these.

### Moving Windows

Now if you want to move a window you can use the **_move-window_** command. The command to do this:

> move-window [ −d] [ −s src-window] [ −t dst-window]
>
> swap-window [ -d] [ -s src-window] [ -t dst-window]

Similar to the above command except both windows have to exist – if they both do the window with the ID source and destination windows will be swapped.

### Advanced Pane Handling

When you split up a window into multiple smaller windows they're referred to as panes. Tmux also offers "layouts" for the panes, or the default positioning and behavior when you create a new window. You can switch through the panes by using the key binding _**Ctrl-b <space>**_ which will toggle through the different layouts. Each one has different behaviors such as _**main-vertical**_ which means the current active pane will take up more space in the current window, or **_even-vertical_** which will split the panes equally. Since this is difficult to describe in text I believe a few screen shots are in order:

![][5]![][6]![][7]

Now that you've seen the different layouts let's see what we can do with these panes. As mentioned above in the "Pane Handling" section you can switch through panes by issuing the _**Ctrl-b o**_ key combination (which is using the _**down-pane**_ command) or by typing _**Ctrl-b q**_ which will list the pane ID's and you select the one you want.

### Make your pane into its own window

If you want to take a pane and make it into its own window you do the following:

> **Ctrl-b : "break-pane"**

Simple enough, you should now have the pane in its brand new window. If you don't want it to automatically make the pane you just broke out as the active window issue the "-d" switch which will simply break the pane to a new window but keep you in the current window.

### Resizing Panes

You can also resize panes if you don't like the layout defaults. I personally rarely need to do this, though it's handy to know how. Here is the basic syntax to resize panes:

> **Ctrl-b : resize-pane** (By default it resizes the current pane down)
** Ctrl-b : resize-pane -U** (Resizes the current pane upward)
** Ctrl-b : resize-pane -L** (Resizes the current pane left)
** Ctrl-b : resize-pane -R** (Resizes the current pane right)
** Ctrl-b : resize-pane 20** (Resizes the current pane down by 20 cells)
** Ctrl-b : resize-pane -U 20** (Resizes the current pane upward by 20 cells)
** Ctrl-b : resize-pane -L 20** (Resizes the current pane left by 20 cells)
** Ctrl-b : resize-pane -R 20** (Resizes the current pane right by 20 cells)
** Ctrl-b : resize-pane -t 2 20** (Resizes the pane with the id of 2 down by 20 cells)
** Ctrl-b : resize-pane -t -L 20** (Resizes the pane with the id of 2 left by 20 cells)
… etc

Hopefully you get the jist – don't get confused! Simply load up a tmux session and split the window a couple of times and issue the above commands. It should become fairly evident how it behaves after fiddling with it for a bit.

### Utilizing the client / server model

I've avoided mentioning that a lot of these commands can actually be applied to numerous tmux sessions which allows quite a bit of flexibility – the reason for avoiding it is it's too much information all at once! An example of using this functionality is if you have two sessions open you can "link" or "move" windows across different sessions – unfortunately the actual "how-to" will be in Part 2.

### Conclusion

Tmux may be a bit confusing however it's worth putting in a few minutes to check it out and see what it has to offer – quick easy and intuitive.

### Upcoming in Part 2

I'll be going over several tips and tricks for tmux including:

* Custom hotkeys
* Custom window styles & colors
* Scripting with _tmux_ (bash, sh)
* How to use the server / client model further

[1]: http://blog.hawkhost.com/wp-content/uploads/2010/06/1-300x219.png "TMUX"
[2]: http://blog.hawkhost.com/wp-content/uploads/2010/06/2-300x218.png "TMUX - Interactive Prompt"
[3]: http://blog.hawkhost.com/wp-content/uploads/2010/06/3-300x218.png "TMUX - List Windows"
[4]: http://blog.hawkhost.com/wp-content/uploads/2010/06/4-300x219.png "TMUX - Split Windows"
[5]: http://blog.hawkhost.com/wp-content/uploads/2010/06/main-vertical-300x219.png "main-vertical"
[6]: http://blog.hawkhost.com/wp-content/uploads/2010/06/even-horizontal-300x218.png "even-horizontal"
[7]: http://blog.hawkhost.com/wp-content/uploads/2010/06/even-vertical-300x218.png "even-vertical"