How to get vim to list the PIDs of selected files that are presently being edited, avoiding recovery mode, and not list all the other files

665 views Asked by At

The vim manual page contains two similar -r type commands. I'll give more background below, this question is really how to invoke the first type of -r to list the swap files, but avoid the second -r that invokes recovery

   -r          List swap files, with information about using them for  re‐
               covery.

   -r {file}   Recovery  mode.  The swap file is used to recover a crashed
               editing session.  The swap file is a  file  with  the  same
               filename as the text file with ".swp" appended.  See ":help
               recovery".

The -r without filename (the first -r above ) reports on the swap files of other files too, including ones in other directories

Background:

I'm trying to have vim report the swap files of a specific file (mostly to determine if vim still editing the file). If the file is being edited ( in another window, either on linux or cygwin ) I can 'raise' that window up to the top with "\e[2t\e[1t" as I have successfully be able to do thanks to Bring Window to Front

Vim has multiple swap file names, and multiple directories that it could put a file, so I want to ask vim, please tell me the name of the swap files that are currently in use for a given file, and if there is a current vim process on the file. Unfortunately, sometimes vim will open a command file in recovery mode in unexpected ways.

I'm invoking vim like this vim -r -c :q file, well actually, I'm invoking it from script, since I want vim to see something more like a terminal, then I look at the output file, so it's more like script -q -c "vim -r -c :q foo" fooscript, then I look in the fooscript file for messages like /Note: process STILL RUNNING: (\d+)/

It is beginning to look like I need to use vim -r without a file name, and parse the output of the -r report, and that there isn't a way to get the report pre-filtered to a single file in question.

1

There are 1 answers

1
David Dyck On

after switching my focus to just vim -r, and

  • Knowing that vim will try to put the swap file into the same directory as the file it's editing ( thanks to @romainl for the pointer to :help swap-file )

  • observing that vim -r reports on the files in the current directory first,

  • observing that the file name associated with the swap file is reported before the process id of the vim process, and

  • observing that vim appends (STILL RUNNING) if it finds the active process

  • I changed the current directory appropriately and ran this code after plugging in the name of the file-to-search-for

     perl -lne '
       last if /^\s+In directory/;
       undef $f if /^\d+/;
       $f = $1 if /^\s+file name:\s+(.*)\s*$/;
       if ( $f =~ m#/file-to-search-for# && /^\s+ process ID:\s(\d+).*?STILL RUNNING/ ) {
         print $1;
         $pid //= $1;
       }
       END { exit !$pid; } '
    

The pid of the running vim process is printed, and the exit status is zero when the appropiate swap file is found, and non-zero if the file was not being edited