Reputation: 2854
Let's say I have multiple tmux sessions and windows/panes. After awhile, I may have started multiple vim
in various windows/panes. ps
would show the tty
s of all the vim processes. How would I go about finding the tmux window/pane for a given tty?
Upvotes: 9
Views: 4664
Reputation: 2539
find /tmp/tmux-$UID -type s -print0 |
xargs -0i tmux -S '{}' list-panes -a -F '#{session_name} #{window_index} #{pane_tty} #{window_name}' 2>/dev/null |
grep -w "$(ps -p "$1" -o tty= || echo pts/NO_SUCH_TTY)"
I wish list-panes
took a filter, then it would be even easier.
I call this script p2mux
.
My answer is a bit complicated (find | xargs tmux
) because I nest tmux sessions, with default
being the top-level and all nested sessions being attached each in a window of the same name in the default
session. This way I can very quickly find all my workspaces. I also have a script I call ntmux
that I give a directory to and it will create a new session named after the basename of that directory, and a new window in the default
session named the same, and then it will run tmux -S ... attach-session
in that window -- this makes it trivial to launch new nested sessions.
Upvotes: 0
Reputation: 111
You to directly jump to some known tty, you can use a combination of tmux list-panes
with -F format
and tmux switch-client
. In the format you can use #{pane_tty}
abd #{pane_id}
to shape the output, and then just grep (for example pts/2).
tmux switch-client -t $(tmux list-panes -aF "#{pane_tty}:#{pane_id}" | grep pts/2 | grep -oE "[^:]*$")
If you're like me and love easy fuzzy selections, pipe a fzf:
tmux switch-client -t $(tmux list-panes -aF "#{pane_tty}:#{pane_id}" | sort | fzf | grep -oE "[^:]*$")
It can be even more user friendly when you use the pane_title. As you specifically asked for vim, here is how you can dynamically set the title of a pane to the file you are editing in vim (put it into your vimrc).
autocmd BufReadPost,FileReadPost,BufNewFile,BufEnter,FocusGained * call system("tmux select-pane -T 'vim | " . expand("%:t") . "'")
and then trigger a fuzzy search for it
tmux switch-client -t $(tmux list-panes -aF "#{pane_tty}: #{pane_title} :#{pane_id}" | grep vim | sort | fzf | grep -oE "[^:]*$")
This will give you much more freedom about what list you have to process than choose-tree as you have the option to filter and (fuzzy)search in the list.
Upvotes: 11
Reputation: 11484
While you could try and do something with tmux list-panes
using #{pane_pid}
and grepping over the results, it is likely that your problem could be easier solved with
tmux choose-tree
It gives a list of the sessions/windows/panes that tmux is running, what is running in those panes, and a snapshot of the pane itself when highlighted.
Upvotes: 8