Fuzzy-switching Tmux Sessions with Ease
source link: https://jdhao.github.io/2021/11/20/tmux_fuzzy_session_switch/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Fuzzy-switching Tmux Sessions with Ease
During my work, I use multiple Tmux sessions to manage different projects. When I create a new session, I usually give it unique name so that I know that the project is about via the session name.
switching between sessions – the basic way
Tmux has a builtin subcommand called choose-session
. Initially, I add a key
binding for this command in my .tmux.conf
:
bind-key S choose-session
It works by showing a list of sessions, and you can switch between these
sessions by using Ctrl-N
and Ctrl-P
.
One disadvantage with this method is that we can only switch between different session one at a time. When I have multiple sessions, switching between them is a pain.
fuzzy-switching sessions with fzf
A more advanced way is to use fzf to filter sessions by their name and then switch to the selected session.
First, we create a shell script ~/.tmux/tmux-switch-session.sh
1:
#!/bin/bash
tmuxsessions=$(tmux list-sessions -F "#{session_name}")
tmux_switch_to_session() {
session="$1"
if [[ $tmuxsessions = *"$session"* ]]; then
tmux switch-client -t "$session"
fi
}
choice=$(sort -rfu <<< "$tmuxsessions" \
| fzf-tmux \
| tr -d '\n')
tmux_switch_to_session "$choice"
We need to give this script execution rights (solution found here):
chmod +x ~/.tmux/tmux-switch-session.sh
Otherwise, when trying to run this script using key bindings, we get the following error:
/home/jdhao/.tmux/tmux_switch_to_session returned 126
Then add the following config to tmux conf:
bind-key S run-shell -b "~/.tmux/tmux-switch-session.sh"
Note that -b
option for run-shell
subcommand is important, without which,
tmux will stuck and freeze forever, see also this issue.
switching tmux sessions with menu
This post shows how to switch sessions using a menu, which is also cool.
First, we create a script ~/.tmux/session-menu
(do not forget to give it
execution rights):
#!/usr/bin/env bash
# See https://qmacro.org/autodidactics/2021/08/12/session-switching-with-the-tmux-menu/
tmux list-sessions -F '#S' \
| awk 'BEGIN {ORS=" "} {print $1, NR, "\"switch-client -t", $1 "\""}' \
| xargs tmux display-menu -T "Switch-session"
Then we create a key binding for it in tmux conf:
bind-key ` run-shell -b "~/.tmux/session-menu"
It will show a small menu for you to switch between different sessions. Pressing a number will switch to that session.
References
Recommend
-
66
简介 Fuzzy finder 是一款使用 GO 语言编写的交互式的 Unix 命令行工具。可以用来查找任何 列表 内容,文件、Git 分支、进程等。所有的命令行工具可以生成列...
-
59
-
49
The tmux utility, a terminal multiplexer, lets you treat your terminal as a multi-paned window into your system. You can arrange the configuration, run different processes in each, and generally make better use of your s...
-
93
I use rsync a lot . I use it to copy files to USB drives, to VMs or Docker containers, to share files between computers at home, to backup stuff to remote machines or simply copy to the loca...
-
21
zfm - Zsh Fuzzy Marks zfm is a minimal command line bookmark manager for zsh built on top of fzf. It lets you bookmark files and directories in your system and rapidly...
-
24
Set up Fuzzy Completion for Vim-lsp2020-11-16Nvim240 words 2 mins read 156 times readIn my
-
8
How to fuzzy match in Redshift
-
6
American Fuzzy Lop plus plus (afl++)
-
16
-
7
Session switching with the tmux menu Aug 12, 2021 Here’s a way to get a simple session switcher in tmux using a popup menu. I was looking at Waylon Walker’s
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK