You are not using tmux to its fullest yet! (Probably)

Hello, Habr! I present to your attention the translation of the article "(ใŸ ใถ ใ‚“๏ผ‰ ๅ› ใฏ ใพ ใ  tmux ใฎ ็œŸ ใฎ ๅŠ› ใ‚’ ๅผ• ใ ๅ‡บ ใ› ใฆ ใ„ ใช ใ„" .



Content



  • What is tmux
  • Starting and starting a new session
  • Opening a new window
  • Transitions between windows
  • Splitting windows and creating panels
  • Moving between panels
  • Moving between sessions
  • [Practical use] Redirecting displayed content in tmux
  • [Practical use] Displaying the name of the Git branch in the info field
  • [Practical use] Display the path of the current operation
  • [Practical use] Calling a snippet
  • Link to settings .tmux.conf
  • Tmux version compatibility


What is tmux



tmux is a terminal multiplexer. When you work on a machine using SSH, running a heavy program, but want to move on to another task, etc. without stopping execution, then tmux can be very useful.



But tmux's capabilities don't end there. There are various handy use cases for tmux, which I want to demonstrate with the examples below.



The basic idea is similar to mods in Vim. With the help of prefix'a (reserved key bindings or key bindings) you can change the mod, the default is ctrl + b.



Starting and starting a new session



If your OS is Ubuntu or Debian, you can install tmux with the command `sudo apt-get install tmux`. On MacOS - brew install tmux.



Command to run:



$ tmux


If you want to start tmux and immediately name the new session, use this command:

$ tmux new -s {   }


image



Opening a new window



Once you've created a session in tmux, you can also create new windows. The standard link is "ctrl + b + c". If you have kept your prefix, you can use the "prefix + c" link. Most likely, the value of โ€œcโ€ is create, hence such a bundle.



image



Transitions between windows



You can also freely switch between the created windows. The standard bindings for this are "prefix + p" (p - previous) and "prefix + n" (n - next).



Using prefix is โ€‹โ€‹not always convenient, so I have customized the key bindings for switching between windows: shift + rightKey for the next window and shift + leftKey for the previous one.



image



Splitting windows and creating panels



Splitting windows in tmux is similar to splitting in vim. You divide the window into several panels, thus you can open vim in one window, htop in another, and execute the program in the third.



Without keybinding, the commands for splitting a window are prefix + "split-window -h", prefix + "split-window -v".



Since I don't really want to prescribe this every time, I made hotkeys for myself in the form of prefix + "-", prefix + "|".



image



Moving between panels



The standard commands for moving between panels are complicated, not very easy to use, so in this case I also assigned my hotkeys: ctrl + shift + upKey, ctrl + shift + downKey, ctrl + shift + rightKey, ctrl + shift + leftKey.



image



Moving between sessions



Using the ctrl + t key combination, you can call up the list of sessions, and use the โ†‘ โ†“ keys to select the required session.



image



[Practical use] Redirecting displayed content in tmux



Basically, tmux is a kind of virtual terminal, contained in the / dev / pts / {fooBar} directory. A directory of the same format is created for each panel.



In other words, if we define {fooBar} for each panel, we can redirect the stdout and stderr streams from panel X separately - one to panel Y, the other to panel Z.



Thus, you can output programs that you write to different stderr and stdout panels, and also use the terminal as a log of operations.



image



[Practical use] Displaying the name of the Git branch in the status bar



This is a life hack that uses the ability to read Shellscript and Python script from .tmux.conf - tmux configuration file.



The status bar can be configured with the following command:



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


# {host} is obviously the hostname, and the tmux run-shell runs a custom script that returns value to the status bar settings.



tmux_hook is a file I created to display the time and current Git branch, but it can essentially return any text information. For example, if configured correctly, you can even display your Twitter timeline in the status bar.



image



[Practical use] Display the path of the current operation



Often, jumping through the terminal, you want to understand which directory you are in, which path it is contained in.



I do not want to write pwd for this once again, so you can display the path of the current operation in the same place where the path to the current panel is displayed.



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


image



[Practical use] Calling a snippet



Even though tmux is a session manager with a terminal sharing feature, it can do some IDE functions (cool isn't it?). You can bind your hotkeys to the special send-keys setting and call them in the terminal.



For example, if you set up send-keys as in my example below, you can simply press prefix + 1 and display the import commands required to work with python.



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


image



Link to settings .tmux.conf



Right here



Tmux version compatibility



Sometimes it happens that some tmux settings do not work - in this case, its version may be either too new or too old.



All of the above and the config to which I left the link are for tmux version 2.8 and higher. If something doesn't work for you, check if this version is right for you.



If you are going to debug something yourself, then first of all you should consult man tmux - the most reliable documentation on tmux at the moment.



The link to the original of this article is here.



We will be very happy if you tell us if you liked this article, was the translation clear, was it useful to you?



All Articles