Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Case in point is editing a configuration file in vim and inadvertently leaving it open. Then you go about your business, switch around in different Tmux sessions, eventually edit the same file from another session and vim will tell you a .swp file already exists.

Now, how do you find which Tmux session the other vim holding the file open is in? Findw seems to only search through active session windows.

share|improve this question
up vote 6 down vote accepted

lsof /path/to/.file.swp will show the process ID of the offending vim process. If you want to write a script, use pid=$(lsof -Fp "$swp_file"); pid=${pid#p} to get just the process ID.

Then ps 12345 where 12345 is the process ID will show some information about the process, in particular what tty it's running on (ps -o tty= -p $pid in a script). The tty uniquely identifies a tmux window (assuming the process is running inside tmux), but I don't know how to go from the tty name to the tmux session.

What would give you the tmux session is the value of the TMUX environment variable in the vim process. The session number is the last number, after the last comma.

Most unices have a way to find out the environment of a process, but the way differs between unix variants. On Linux, you can use </proc/$pid/environ grep -z '^TMUX=' to show the value of $TMUX in process $pid, so you can extract the session number as $(</proc/$pid/environ grep -z '^TMUX=' | sed 's/.*,//').

share|improve this answer
yes indeed, this basically works, at least for scenario when original vi process isn't sudoed. /proc/$pid/environ was new to me, good thinking on that. getting the session value as a number is good enough since the session id letters >9 simply increase alphabetically. – lkraav Nov 19 '10 at 22:01
when using sudo to launch the hypothetical vi process to find session by, it looks like it needs to have sudo -E for the TMUX variable to travel into it's environ. – lkraav Nov 19 '10 at 22:09
@lkraav: Best: use sudoedit instead of sudo vim. Also useful to know for other cases: either use sudo -E or put Defaults !env_reset in /etc/sudoers. In a pinch: find out the parent process of vim, which is likely to be a shell running in the same tmux window, and get its TMUX variable. – Gilles Nov 19 '10 at 22:26

This shell snippet works pretty well for me (you'll need the pstree utility as well):

for s in `tmux list-sessions -F '#{session_name}'` ; do
  echo -e "\ntmux session name: $s\n--------------------"
  for p in `tmux list-panes -s -F '#{pane_pid}' -t "$s"` ; do
    pstree -p -a -A $p

For two tmux sessions with two vim's each, I get this output:

tmux session name: 0
  `-vim,3467 file_1
  `-vim,3495 file_2

tmux session name: 1
  `-vim,3547 file_3
  `-vim,3576 file_4
share|improve this answer
great, thanks! also, adding -h in pstree call will highlight the current pane, FWIW; or, passing -P $PID instead should highlight some $PID you might be searching. But you don't really need this, I suppose. – akavel Oct 31 '13 at 9:28

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.