In the previous article we wrote about few improvements that you can implement for your Command Line Interface (CLI). But “Perfection knows no limits” and in this article we would like to dig deeper and describe few additional solutions that will make your CLI more powerful in your hands.
Many developers/system administrators sooner or later face with the situation when one terminal window or even few terminal tabs is not the most convenient solution. You want to have everything close at hand: to see all terminal windows at the same time. Sure, you can tile all terminal windows or improvise in some other way but for making the things easier let’s give a shot for a terminal multiplexer.
Terminal multiplexer – it’s a utility that allows keeping few opened interactive shells at the same time and easily switch between them. One of the most popular multiplexers is called Tmux – a utility that gives an ability to switch easily between several programs in one terminal, detach them by keeping running in the background and reattach them to a different terminal etc. Also it’s compatible with most of Linux/BSD systems, and you can use it with your favourite Linux distributive or even with MacOS.
There are no complexities with Tmux installation. Usually you can setup it via package manager, installed in your system. We will describe the installation process on OSX. Unfortunately, there’s still no full-featured package manager out of the box in MacOS. Thanks to the community, we have a great 3rd party package manager for Mac called Homebrew. You can read more about it on the official website http://brew.sh.
First of all, let’s install Homebrew. For this open your CLI and execute the following command there:
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
That’s all, now you can install different packages using this package manager. Then install Tmux via Homebrew: execute the following simple command in the console:
brew install tmux
and wait until Tmux is installed.
Do not rush to start Tmux right now. By default, it might look not very user friendly, that is why we need to configure it. The main Tmux configuration is a text file located in ~/.tmux_conf, open this file using your favourite text editor. We need to spend some time playing with this file.
If you’ve used applications like Vim before, you should know about command-line mode that allows to execute different commands for the application. Tmux has command-line mode as well, it can be accessed via keys combination Ctrl+b. In the command-line mode you can execute Tmux commands such as creating a new tab/window, search for a window, rename window, swap/kill windows etc. However, Ctrl+b is not the most handy combination (only in case if you are good piano player). We recommend to change it to Ctrl+a which is much more handy. To achieve this we need to add the following line to the configuration file:
unbind C-b set -g prefix C-a
Then, create few hotkeys for splitting the windows. For example, we want to create a new terminal window in the current tab splitting the tabs vertically or horizontally. Add the following lines to your config:
bind V split-window -h bind H split-window
Do not worry if you find the process confusing, we will have a chance to test everything later.
Now, let’s improve the switching between windows by making it more handy. For this, add the following lines:
bind -n C-Space select-pane -t :.+ bind Escape copy-mode
Tmux also has a menu bar at the bottom (by default). It’s really flexible and you can place there any information you want: weather, US dollar rate or even the next item in your playlist. However, let’s focus on more or less useful information. Add the following menu bar configuration to the file:
# status bar colors set -g status-bg black set -g status-fg white # alignment settings set-option -g status-justify centre # status left options set-option -g status-left '#[fg=green][#[bg=black,fg=cyan]#S#[fg=green]]' set-option -g status-left-length 20 # window list options setw -g automatic-rename on setw -g window-status-format '#[fg=cyan,dim]#I#[fg=blue]:#[default]#W#[fg=grey,dim]#F' setw -g window-status-current-format '#[bg=colour7,fg=black,bold]#I#[bg=colour7,fg=black]:#[fg=black]#W#[fg=dim]#F' # status right options set -g status-right '#[fg=green][#[fg=blue]%Y-%m-%d #[fg=white]%H:%M#[default]#[fg=green]]'
After this, you will get a session name at the left side, windows list in the center and current date and time at the right side of the menu bar. That’s all with the configuration file, you can save it now and see what we’ve received.
In your terminal window type this command:
tmux
and press the Enter key. You should see the similar terminal window you had before but with new menu bar at the bottom we have configured a minute ago. Now you can experiment with the new terminal multiplexer. Press Ctrl+a to enter the command-line mode, then press Shift+H. If you configured everything correctly, you should have a new terminal window split horizontally. If you want a new window split vertically, you should do the same but instead of Shift+H use Shift+V (as you remember, we have configured this behaviour before). To close a new window use the keys combination Ctrl+d. To switch between split windows use Ctrl+Space.
Now try to create a new tab, for this enter the command-line mode (Ctrl+a) and press c. You will receive a new tab (you should see all tabs in the bottom menu). Also, you have the ability to switch between tabs. As you can see, each tab has its own number. Enter the command line mode and just type the tab number you want to go to – you will be switched to that tab. Also, to keep everything in order, you can rename your tab: enter the command line mode and press , – then, type the new tab name and press Enter.
From this moment you can use this powerful multiplexer for your needs. You can make your own “magic” by manipulating lots of terminal windows at the same time with no mouse or additional tools. Here is a short list of general keyboard commands (pipe ‘|’ here divides the command into two parts in case if the first part is followed by the second part):
ctrl-a | c — Create a new window ctrl-a | , — Rename a window ctrl-d - Close a window ctrl-a | 1-9 — Go to tab 1-9 ctrl-a | ctrl-a - Switch between recently used windows ctrl-Space - Switch between windows sequentially ctrl-a | Shift-H - Create a new window and split the current window horizontally ctrl-a | Shift-V - Create a new window and split the current window vertically ctrl-a | [ - Open the visual mode (like in Vim). In the visual mode you can scroll the window's content, select and copy text, etc v or Space — (In the visual mode) Text selection in the visual mode q — (In the visual mode) Exit from the visual mode return — (In the visual mode) Exit from the visual mode and copy the selected text to the buffer
If you need more commands, you can find a good cheatsheet here https://gist.github.com/MohamedAlaa/2961058 . Feel free to ask questions and leave proposals in the comments below. Good luck with your CLI boosting.