9

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 ttys of all the vim processes. How would I go about finding the tmux window/pane for a given tty?

jduan
  • 2,784
  • 2
  • 15
  • 9
  • I came across a similar question and thought this guide was helpful https://stackoverflow.com/questions/29439835/find-tmux-session-that-a-pid-belongs-to – Manny Adumbire Apr 19 '19 at 20:35

3 Answers3

9

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.

Andrej Zieger
  • 91
  • 1
  • 2
8

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.

jeremysprofile
  • 10,028
  • 4
  • 33
  • 53
  • Thanks for your response! Those two commands are super useful (thanks for sharing them) although they don't quite solve the problem I described. – jduan Feb 06 '19 at 06:42
0
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.

user2259432
  • 2,239
  • 1
  • 19
  • 15