Bash Autocomplete for tmux
I’ve recently become a big fan of tmux and have put far too much time into customizing my config file. When working in the terminal it helps me compartmentalize work and lets me come back a project right where I left off with all of my sessions and windows named to keep things organized. These features are useful when working locally but are a godsend when SSH’d into a remote machine because tmux can keep sessions alive over multiple SSH logins, allowing me to detach from a session and attach to the same one days later.
The one big pain point so far has been tab-completion. Finding and reattaching to a session often looks like this:
$ tmux list-sessions
session-1: 3 windows (created Fri Feb 16 16:37:48 2018) [80x23]
session-2: 4 windows (created Thu Feb 1 15:42:16 2018) [80x23]
session-3: 4 windows (created Tue Feb 13 16:09:11 2018) [80x64]
$ tmux attach-session -t session-1
Bash will tab-complete tmux
but not the tmux command attach-session
or the specific session name. Having to type out all of this out can get annoying and with
completion we could skip tmux list-sessions
entirely because hitting
Tab should list all of the running sessions for us.
The tmux README recommends using
the wonderful tmux-bash-completion script for completions. It can be sourced from .bashrc
but this approach is way
too easy. We need to keep our home directory clean from non-standard files.
The bash-completion package ships with
Ubuntu and provides a ton of completions but unfortunately does not support tmux out of the box (if it did we would’t
be addressing this in the first place). One solution is just to save the recommended script
to the bash-completion’s completionsdir
(which is /usr/share/bash-completion/completions/
on my system). This works fine if you have root privileges but is not portable across systems
where you only have basic user privileges, which is the case for me on my university’s
Linux servers. After running sudo
once on them (and quickly hitting
CTRL+
C once I realized what I had done), I learned that the admins really do not appreciate
students trying to execute commands as root.
A simple work-around is saving the tmux-bash-completion script as ~/.bash_completion
which the bash-completion script will parse automatically. Other custom tab-completion scripts
can live here as well and it allows you to easily manage the script alongside additional home
directory dotfiles with GNU Stow. Perfect!
Download with:
$ curl https://raw.githubusercontent.com/imomaliev/tmux-bash-completion/master/completions/tmux > ~/.bash_completion