David Rio

My terminal flow: tmux sessions

Tmux plays a crucial role in my terminal flow. It helps me managing my terminals. Here I'd like to discuss how I use the tool and in particular, how I handle multiple sessions.

You probably work on different projects that require different contexts. By context I mean: hosts, filesystem location, processes running. You may be working on a project that requires running a couple of processes in a few remote machine and you have to see their standard output and standard error while navigating the system to perform different tasks. In parallel, you may also be working on a local project that requires your terminal showing a code base, and then a few other processes: perhaps an http server, kafka server, etc... Then you may have your SQL playground where you refine your SQL queries by running them against remote database engines. You get the point.

The first suggestion I have is to group your panes and windows in sessions. In the previous example we had three projects: remote machines, local dev and SQL playground. We could name your sessions based on that: remote, local-dev and SQL playground.

A list of tmux sessions and windows.

Within sessions, I'd suggest not having too many windows. I typically have two only. I may have one window for my editor neovim and another window that I call cli where I may have tests output, logs and processes that are part of the project I am working on.

Maybe I end up with three or four projects, sometimes many more depending on how many things I am juggling at the same time. At this point I don't want to have to recreate all the tmux structure if, let's say, I have to restart my machine (by the way here is the current uptime in my laptop, not bad):

➜ uptime
 6:04  up 25 days, 12:56, 2 users, load averages: 1.53 3.88 6.17

I use the tmux resurrect plugin to persist the state of my sessions so I can restore them easily when I restart my machine.

Jumping between sessions in tmux with fzf.

Now, the key point I wanted to make in this post: do not jump between projects by navigating the list of windows. Use sessions. Feed the list of tmux sessions to a fuzzy finder (I use fzf) to quickly jump between sessions. Here is what I use in my tmux.conf:

bind C-j display-popup -E "\
  tmux list-sessions -F '#{?session_attached,,#{session_name}}' |\
  sed '/^$/d' |\
  fzf --height 70% --reverse --preview 'tmux capture-pane -pt {}'  |\
  xargs tmux switch-client -t"