tmux: reload bash config across all panes and sessions

Yesterday I got myself into tmux and I love it!

tmux is a terminal multiplexer. It lets you switch easily between several programs in one terminal, detach them (they keep running in the background) and reattach them to a different terminal.

I’ve been using Zellij for several months prior to this. Which I must thank for getting me introduced to the world of terminal multiplexers. However I’ve been running into several issues with it, that got me looking for an alternative:

  • Session manager entires keep shifting and popping in and out randomly. This is the most frustrating thing, probably number one cause for me to look for a change.
  • Occasional crashes during session switching.
  • No ability to toggle between last two used sessions.
  • No number hotkey assignment for tabs.
  • Need to match Zellij theme with your terminal theme. They are two separate things.

The switch

A fantastic start into tmux are these two videos from typecraft over at YouTube. The basics are wonderfully explained there as well as some key configuration. This got me going immediately.

Here’s a documented .tmux.conf a arrived at after a day or two:

# Settings

# Change leader key to something more ergonomic
# Note: On MacOS disable the shortcut to change keyboard input language first in Settings.
set -g prefix C-Space
# Allow mouse interactions like focusing windows and panes and resizing
set -g mouse on
# Simplify the status bar look: white text on black background
set -g status-style bg=black,fg=white
# Disable hostname, date and time display in the status bar
set -g status-right ""
set -g status-position top
# Use vim style navigation
set -g mode-keys vi
# Number windows starting from 1, instead of 0
set -g base-index 1
# When window is closed, refresh the numbering
set -g renumber-windows on
# Change the look of session name in the status bar
set -g status-left "#[fg=blue]#S#[default] #[dim]→#[default] "
# Do not truncate session names too early
set -g status-left-length 100

# Keybindings

# Shortcut for live reload of tmux config
unbind r
bind r source-file ~/.tmux.conf

# Last session toggle
bind S switch-client -l

# Maximize pane
bind z resize-pane -Z

# vim like pane navigation
bind h select-pane -L
bind j select-pane -D
bind k select-pane -U
bind l select-pane -R

# Set new windows and panes in the current directory
bind c new-window -c "#{pane_current_path}"
bind '"' split-window -c "#{pane_current_path}"
bind % split-window -h -c "#{pane_current_path}"

Superpowers: issuing commands to all panes in all sessions

I almost forgot what I wanted to write about initially. So here’s a ready made alias I use to reload bash config across all my tmux bash sessions. This is sooo useful when I for example add something like a new alias in my .bashrc. Previously I’d have to manually source the config in each pane where I’d want to use the alias, which is a pain. Now it’s just one command:

alias reload-bash-config-in-bash-panes=$'tmux list-panes -a -F \'#{session_name}:#{window_index}.#{pane_index} #{pane_current_command}\' | awk \'$2 == "bash" {print $1}\' | xargs -I{} tmux send-keys -t {} "source ~/.bashrc" C-m'

This ensures the source command is only executed in panes that are running “bash”. It won’t affect your neovim instances for example, or other programs where it could cause unintended consequences.

Layouts: GitOps for workspaces

In Zellij, layouts are build in and a wonderful feature. In tmux land, there is tmuxinator–a separate program–that handles layout definition via YAML files and starting sessions based on those layouts. It’s simple and I got going with it immediately.

Here’s a layout I use to run this blog in development environment:

name: klevo
root: ~/projects/klevo

windows:
  - edit:
      layout: main-vertical
      panes:
        - #bash
        - nvim
  - server:
      layout: even-horizontal
      panes:
        - docker compose up
        - ruby -rwebrick -e'WEBrick::HTTPServer.new(:Port => 8083, :DocumentRoot => ("#{Dir.pwd}/_site")).start'

tmux and neovim are the two things I wish I learned much earlier in my career. Mastering these tools provided an immense ergonomic boost to my workflows. It’s a joy to fly through projects and tasks in a fast, consistent, repeatable and simple environment, exactly suited to my needs.

My current tmux configuration running inside Ghostty terminal.

Speeding up office work with Ruby and IRB

I was just paying taxes ?. One of the steps involved was copying and pasting a few long reference numbers from a PDF into my banking app. The thing is, when copied, the reference number would contain extra spaces, after each character, making it invalid where I needed to paste it.

Now there are myriad of different solutions for this I’m sure, but as a programmer, let’s take a look at how to speedily create a makeshift tool in Ruby to do this.

irb
space_eater = ->(text) { text.gsub(' ', '') }

We launched into the interactive shell, and defined a lambda we can call on strings we want to sanitize. Like so:

space_eater.call('2 3 9 2 9 9 A 8 2 8 0 0 0 1')
=> "239299A8280001"

And we’re done. ❤️ Ruby.

Dockerized Percona MySQL Server with automated replication, tools & tests

Docker & Percona Server I developed this container to solve specific needs and alleviate pain points present when dealing with deployment and administration of MySQL on servers that I manage.

I decided to look into Docker during a migration from MySQL 5.5 to 5.6 on one of the production servers. The server hosts multiple applications and services and is running in a hot spare configuration (another server mirrors this server, acts as the MySQL slave, etc.). Thus I wanted a migration strategy where I can have the 5.6 installation ready and running on the server, so that I can test it with production data and when ready just turn the switch to replace the old 5.5. Docker turned out to be the cleanest solution.

Since the MySQL server is such a critical part of the infrastructure I decided to develop the container utilizing test driven development. This allowed me to quickly add new features, like performance optimization and replication over a ssh tunnel (to support servers in different public clouds). Having this functionality in a standalone, tested and isolated unit is amazing. Before, all this complexity would be managed by Chef provisioning, which is much harder to test and experiment with on the production server. Having this functionality contained in a Docker container allows me to just use Chef for orchestration and deployment of the containers themselves, witch requires much simpler logic, compared to provisioning a full MySQL server install, configuration, replication and upgrading.

  • Code is available at Github.

This project is released under the MIT license.

DevOps

serversSoon after I started working with Rails, I wanted to start using my own server environment, instead of a managed hosting. I wanted to be able to use any kind of libraries, their versions and any other tools my projects required on my server, set up the way I need it. Thus I got my first VPS.

From one small VPS server, over a period of a few years, I find myself managing 6+ dedicated servers to serve the needs of the applications I’ve developed and maintain.

At the beginning I managed my first server manually: ssh into the machine, install packages, edit the configs… This soon became tedious and I was aware that there are tools designed to do this better. I picked up Chef and never looked back. Nowadays all my servers are managed by cookbooks, deployed by chef-solo in conjunction with my own knife-like toolset based on Mina.

Here’s a short list of some of the more interesting things that I’ve learned and have experience with in conjunction with DevOps:

  • MySQL configuration, backup & replication (I’ve developed my own Docker container for this).
  • Using Postfix as a relay for services like Mandrill for reliable transactional email delivery.
  • Nginx, Unicorn, Upstart for reliably running apps with hot reloads on deploys.
  • Infrastructure monitoring using Zabbix (I’ve got my own container for that too).
  • Recovery from a failed disk in software RAID environment.
  • Setting up rate limiting and basic DDOS attack precautions.
  • Defending against small attacks like spam bots, password guessers, etc.

All of these skills translate and support my software development and application design, as I’m much more aware of the whole lifecycle of an application, and the real world usage scenarios that will be thrown at it.