0

I am using vim 9.0 (2022 Jun 20, compiled Nov 30 2022 23:37:37).

I am running KDE and under Konsole I have very many windows with terminals /dev/ptsnn open with shell bash in each.

Many Vim instances are running.

There are many files I have worked on in the last days. Some of them are yet open in a Vim, some of them are already closed. I do not remember exactly.

Very often there is the need to deremine for a special file if it is yet open, already closed and especially in which Vim instance, in which shell, in which tty it is still open or it was open (the last case is perhaps not possible).

Using find I can look for existing .swp.

In the .swp the PID of the Vim process is contained. With the PID one can find in which tty the Vim is running.

How can I read the contents of .swp in a read-only mode.

Using less it is not possible. Is another utility available?

Vim with the recover modes I would like to avoid. I do not want to change anything, even not the three timestamps-times.

Does someone know a solution?

1

3 Answers 3

0

One of the primary requirements—to get the PID of a vim instance if that yet has a file open—can be done with swapinfo().

1
  • 5
    If you need to add something, you can edit your own answer. For later readers, it's more helpful to have one answer containing all the information than three that build upon one another.
    – Friedrich
    Nov 21, 2023 at 11:01
0

The solution I know is:

ps -Flwwc -p $(pgrep -d " " vim) | grep <<part_of_the_filename_i_am_looking_for>>

But I don't like this way.

0

I do not yet have a solution to read a swapfile in readonly mode, but I can offer now a full-featured solution based on the code given in my first answer.

NOTES to the code that follows.

The code is written as a bash script one-liner as I use it (before I begin a working session, I write by a script the collection into the bash history list to have it handy).

For usage:

Type into the first assignment a regex to specify the file for which the swapfile is to be searched in the whole system.

“Whole system” means “all processes currently running.”

When no file is found, it means that this file is not open in any Vim instance.

In the first part of the output all instances of Vim that have the specified file open are listed.

The matching string is colored.

In the tty-column one can see in which terminal this Vim instance is running, _e.g., "pts/12".

In the second part of the output the dir-listings are printed of all directories that contain a swapfile of the given file (if that file is open in an instance of Vim).

The basenames of the file, and maybe of its backups, and of the swapfile are colored.

The filename extenstion of the swapfile, e.g., ".sw..." are highlighted but not colored.

{ WEx_grep_its_PCRE_regex_to_specify_file="we_regex_of_filename"; ps -ww -o f -o stat -o euid -o euser -o ruid -o ruser -o pid -o ppid  -o sess  -o cls -o pri -o wchan=WIDE-WCHAN-COLUMN -o lstart -o time -o tname -o args --sort=start_time -p $(pgrep -d " " vim) | grep --color=always -P $WEx_grep_its_PCRE_regex_to_specify_file"; echo ""; (  shopt -s lastpipe; set +m; ps -ww -o args -p $(pgrep -d " " vim) | grep -P $WEx_grep_its_PCRE_regex_to_specify_file" | xargs -IX /bin/bash -c 'WEx="$0"; echo ""; echo "WEx: ~$WEx"; [[ "$WEx" =~ /media/.*$ ]]; WE_retcod=$?; echo "WE_retcod: ~$WE_retcod~"; WEy="${BASH_REMATCH[0]}"; echo "WEy: ~$WEy~"; WEzdir="$(dirname "$WEy")"; echo "WEzdir: ~$WEzdir~"; WEzbas="$(basename "$WEy")"; echo "WEzbas: ~$WEzbas~";  ls -la --time-style=long-iso -t "$WEzdir" | grep --color=always -P "^|$WEzbas"' X; ); } | less -R +/{ WEx_grep_its_PCRE_regex_to_specify_file="we_code_to";     ps -ww -o f -o stat -o euid -o euser -o ruid -o ruser -o pid -o ppid  -o sess  -o cls -o pri -o wchan=WIDE-WCHAN-COLUMN -o lstart -o time -o tname -o args --sort=start_time -p $(pgrep -d " " vim) | grep --color=always -P "$WEx_grep_its_PCRE_regex_to_specify_file"; echo ""; (  shopt -s lastpipe; set +m; ps -ww -o args -p $(pgrep -d " " vim) | grep -P "$WEx_grep_its_PCRE_regex_to_specify_file" | xargs -IX /bin/bash -c 'WEx="$0"; echo ""; echo "WEx: ~$WEx"; [[ "$WEx" =~ /media/.*$ ]]; WE_retcod=$?; echo "WE_retcod: ~$WE_retcod~"; WEy="${BASH_REMATCH[0]}"; echo "WEy: ~$WEy~"; WEzdir="$(dirname "$WEy")"; echo "WEzdir: ~$WEzdir~"; WEzbas="$(basename "$WEy")"; echo "WEzbas: ~$WEzbas~";  ls -la --time-style=long-iso -t "$WEzdir" | grep --color=always -P "^|$WEzbas"' X; ); } | less -R +/CTRL-K"\.sw";

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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