[LINUX] (Maybe) You haven't yet unleashed the true power of tmux

table of contents

--What is tmux? --Start and create a new session --Create a window --Going back and forth between windows --Split window to create pane --Go back and forth between panes --Go back and forth between sessions -[Application] Redirect the display contents of tmux -[Application] Display the branch name of git in the information bar -[Application] Display the current operation path -[Application] Call a snippet --Click here for .tmux.conf with the above settings --tmux has very subtle version compatibility ――I published it on YouTube, so I'd be happy if you subscribe to the channel.

What is tmux

tmux is called a terminal multiplexer, and it is easiest when you log in to a work machine with ssh and operate it, or when you want to go home by executing a heavy program but want to continue working without finishing. This is a use case.

Not only this, tmux can decorate the terminal intricately, and there are various convenient uses, so I will introduce the actual use using a gif animation recording.

Also, like vim, there is a concept of mode, there is a reserved key binding called prefix, and you can switch modes by entering this. The default is ctrl + b.

Launch and create a new session

For Debian and Ubuntu, you can install it with sudo apt-get install tmux. For MacOSX it's brew install tmux.

If you just want to start it, it's OK with no arguments.

$ tmux

When naming a session, start it with the following options.

$ tmux new -s {New session name}

新規セッションの作成.gif

Create a new window

You can create a new window by typing ctrl + b + c while you are in a tmux session. The notation replaced by prefix is prefix + c. Perhaps the meaning of this c is create.

新規windowの作成.gif

Go back and forth between windows

You can move freely between the created windows. By default, prefix + p is used to go to the previous window (p is previous), and prefix + n is used to go to the next window (n is next). ..

Since prefix is expensive to press, I have customized the key bindings to fire without pressing prefix, with shift + rightKey to assign to the next window and shift + leftKey to assign to the previous window. ..

別のwindowへ切り替え.gif

Create a pane by splitting the window

You can split the window like splitting vim, and it doesn't close to the world of text editor like vim, so you can use a wide terminal such as opening vim on the one hand, htop on the other hand, and executing a program on the other hand. You can do.

Without key binding settings, it can be split as prefix +" split-window -h ", prefix +" split-window -v ", etc.

This is also difficult to enter, so I key-bind it to prefix +"-", prefix +" | " to make it easier to operate.

windowをpaneに分割.gif

Go back and forth between panes

By default, moving a pane is a complex keyboard with a prefix command in between, but it can also be defined without a prefix.

I have assigned ctrl + shift + upKey, ctrl + shift + downKey, ctrl + shift + rightKey, ctrl + shift + leftKey to move panes without going through prefix It is possible to move between panes. paneの切り替え.gif

Go back and forth between sessions

A key binding that displays a selection list of sessions is assigned to ctrl + t, and when you press this key, you can see the session list as shown in the figure below.

You can select with the up and down keys, and you can move to that key by selecting.

別のセッションへ切り替え.gif

[Application] Redirect the display contents of tmux

tmux is a virtual terminal like / dev / pts / {fooBar}, and this virtual device is created for each pane. In other words, if {fooBar} can be identified, the contents of a certain pane X can be divided into stderr, stdout, etc. and displayed in pane Y and pane Z respectively.

If you want to check the log of the created program separately from stderr and stdout, or you can read and write to the virtual terminal, you can leave a trail such as the operation log.

[Application] Display the git branch name in the tmux bar

This is a hack that makes use of being able to read shell scripts and python scripts from the tmux configuration file .tmux.conf.

Notifications in the status bar can be defined with the following syntax.

set-option -g status-right '#{host} #(tmux run-shell "tmux_hook --default true --git-path #{pane_current_path}")'  

# {host} is the host name as it is, but tmux run-shell runs an arbitrary script and gives its return value to the status bar settings. The file name tmux_hook is a script I created, which returns the time and git branch name, but since it can return any text information, for example, it can be displayed on my twitter timeline depending on how the script is constructed. Is possible.

Screenshot_20200719_194105.png

[Application] Display the current operation path

Which directory you are in and what path you are in is something you want to check when you are tinkering with the terminal. Since I don't want to type the pwd command each time, it is possible to display information at the location of the dividing border of the pane.

setw -g pane-border-format '#{pane_tty} | #{pane_current_path}'

Screenshot_20200719_203450.png (The right side is the path you are currently operating)

[Application] Call a snippet

Even though it is a session manager with a function to divide the terminal called tmux, it can do something like an IDE (great). You can set send-keys in tmux, and you can enter reserved characters in terminal by entering a specific key combination.

For example, if you have the following settings, you can do something like python setup just by pressing prefix + 1, and if you apply this, it will be available for competition pros.

bind -T prefix -n 1 send-keys "import os\nimport sys\n"

スニペットを登録する.gif

Click here for .tmux.conf with the above settings

tmux has very subtle version compatibility

There are quite a few cases where I set it by looking at the literature on the net, but it doesn't work. In that case, my tmux may be too new or too old. This time, I expected tmux 2.8 or higher to write the config, but if it doesn't work, please check your own environment. As a way to debug yourself, if your existing writing changes, you should read man tmux as it is the most solid document so far.

I also posted on YouTube

It's really embarrassing, but I'm doing it to practice conversation and study expression methods. If you would like to see the usability of tmux in video format, please refer to the following URL. We would appreciate it if you could subscribe to our channel.

Recommended Posts

(Maybe) You haven't yet unleashed the true power of tmux
The Power of Pandas: Python
Power the brushless motor of Oriental motor
The power of combinatorial optimization solvers