Tmux

Using Tmux plugins with Tpm

2017-08-07 Linux, Tmux No comments

Recently, I decided to organize my Unix dotfiles in a better way. I had a few custom scripts I used in my Tmux bottom bar. I kept these scripts in .scripts directory and during installation or upgrade of my personal configuration, install.sh script was copying them from .scripts directory to /usr/local/bin/ directory. I wanted to make this configuration more solid and consistent, so I decided to transform these scripts into tmux plugins managed by tpm. I was already using a few plugins like:

In my Tmux bottom bar, I display battery level, uptime, CPU, RAM, IP number and song currently played on Spotify. Previously I just used scripts copied to /usr/local/bin/ and configuration looked like that:

set -g status-right "↑ #(showUptime) ⇅ #(showCpuUsage) ☰ #(showRamUsage) ∴ #(showIp) ↯ #{showBatteryLevel} ⧖ #(date '+%a, %b %d, %H:%M') "

I created the following plugins to replace these scripts:

In order to use Tmux plugins, we need to install Tmux Plugin Manager:

git clone https://github.com/tmux-plugins/tpm ~/.tmux/plugins/tpm

and initialize it at the bottom of our .tmux.conf file:

run '~/.tmux/plugins/tpm/tpm'

After that, it’s good to reload shell (source ~/.zshrc) and Tmux config (tmux source-file ~/.tmux.conf)

Next, we can add our plugins to .tmux.conf file:

set -g @plugin 'tmux-plugins/tmux-sidebar'
set -g @plugin 'tmux-plugins/tmux-copycat'
set -g @plugin 'tmux-plugins/tmux-pain-control'
set -g @plugin 'tmux-plugins/tmux-urlview'
set -g @plugin 'pwittchen/tmux-plugin-battery'
set -g @plugin 'pwittchen/tmux-plugin-uptime'
set -g @plugin 'pwittchen/tmux-plugin-cpu'
set -g @plugin 'pwittchen/tmux-plugin-ram'
set -g @plugin 'pwittchen/tmux-plugin-ip'
set -g @plugin 'pwittchen/tmux-plugin-spotify'

When we are in Tmux, we can install plugins by pressing prefix + I to install plugins. In my case, prefix = Ctrl+b.
After that, we can hit Enter and we’re ready to go!

Now, I could update my .tmux.conf with the variables defined by my plugins:

set -g status-right " 🔉 #{spotify_song} ↑ #{uptime} ⇅ #{cpu} ☰ #{ram} ∴ #{ip} ↯ #{battery_level} ⧖ #(date '+%a, %b %d, %H:%M') "

After this operation, I could remove custom scripts from my dotfiles and desired functionality is delivered via plugins. Moreover, anyone can install these plugins via tpm without messing with custom scripts!

screenshot from tmux after configuration

Right now, my plugins are in kind of messy state and they don’t work perfectly across all operating systems (e.g. there are problems on macOS), but they’re usable under Linux Ubuntu 16.04 LTS and it’s a good beginning for organizing mess created by the custom scripts.

That’s it! I have plans to publish another article describing how to write your custom Tmux plugin, which can be managed via tpm.

Automate tile layouts creation in tmux with tmux-auto-pane

2017-04-08 Bash, DSP2017, Linux, Tmux No comments

I just released tmux-auto-pane. It’s a tiny shell script for creating pre-defined tile layouts in Tmux on Linux with xdotool.
In our workflow, we often have some pre-defined pane configurations in a terminal. The project called tmux-auto-pane helps to automate that process. It can save us some time and make us a bit more productive.

We can call tmux-auto-pane with one of the following parameters:

--help | -h   showing help
--1l1r        one left, one right
--1l2r        one left, two right
--2l1r        two left, one right
--1u1d        one up, one down
--1u2d        on up, two down
--2u1d        two up, one down
--4tiles      4 tiles, 1 in each corner

for example tmux-auto-pane --4tiles

will generate such layout:

 ____ ____
|    |    |
|____|____|
|    |    |
|____|____|

we can also have the following layouts:

    1l1r         1l2r         2l1r         1u1d        1u2d         2u1d
 ____ ____    ____ ____    ____ ____    _________    _________    ____ ____
|    |    |  |    |    |  |    |    |  |         |  |         |  |    |    |
|    |    |  |    |____|  |____|    |  |_________|  |____ ____|  |____|____|
|    |    |  |    |    |  |    |    |  |         |  |    |    |  |         |
|____|____|  |____|____|  |____|____|  |_________|  |____|____|  |_________|

Script can be installed via wget:

sh -c "$(wget https://raw.githubusercontent.com/pwittchen/tmux-auto-pane/master/install.sh -O -)"

or via curl:

sh -c "$(curl -fsSL https://raw.githubusercontent.com/pwittchen/tmux-auto-pane/master/install.sh)"

Due to the fact, that tmux-auto-pane uses xdotool under the hood, unfortunately it works only with Linux right now.
It can be improved in the future to work with macOS as well.

Source of the project can be found at https://github.com/pwittchen/tmux-auto-pane.
This project could be extended to start specific applications in each pane.
Maybe, I’ll improve it in the future, so users could parametrize their custom applications.

I hope Tmux & Linux users will find it useful :).