I'm not sure if I should ask this problem on vim or on Linux/Ubuntu forum.

The thing is, I opened multiple terminals on my desktop, and several of them opened various of *.txt files. Not all of them are saved.

Then my desktop partially broke down. I can ssh it from my laptop. Is there any way to know which files are opened in vim? And is there any way I can save the unsaved files?

up vote 3 down vote accepted

If you can modify those files, it's likely that Vim created swap files for them. Swap files have extensions .swp, .swo, ..., etc. You can search for these files using find. Looking in your home directory, for example:

find ~ -type f -name '.*.sw?'

If the Vim programs are still running, you could expedite the search by looking at which files are opened by the Vim processes. This can be done by examining the process' /proc entry and finding where the fds point to (ls -l /proc/<pid>/fd/).

For example:

$ pgrep vim | xargs -i ls /proc/{}/fd -l
total 0
lrwx------ 1 muru muru 64 Sep 25 08:31 0 -> /dev/pts/2
lrwx------ 1 muru muru 64 Sep 25 08:31 1 -> /dev/pts/2
lrwx------ 1 muru muru 64 Sep 25 08:31 2 -> /dev/pts/2
lrwx------ 1 muru muru 64 Sep 25 08:31 3 -> socket:[23224440]
lrwx------ 1 muru muru 64 Sep 25 08:31 5 -> socket:[23220671]
lrwx------ 1 muru muru 64 Sep 25 08:31 7 -> /tmp/.gameoverlayui.log.swp
total 0
lrwx------ 1 muru muru 64 Sep 25 08:31 0 -> /dev/pts/5
lrwx------ 1 muru muru 64 Sep 25 08:31 1 -> /dev/pts/5
lrwx------ 1 muru muru 64 Sep 25 08:31 2 -> /dev/pts/5
lrwx------ 1 muru muru 64 Sep 25 08:31 3 -> socket:[23222572]
lrwx------ 1 muru muru 64 Sep 25 08:31 5 -> socket:[23223590]
lrwx------ 1 muru muru 64 Sep 25 08:31 7 -> /home/muru/.vimhelp.vim.swp

So, I have two Vim windows, each having one file opened.


I don't think you can control those processes, not easily1, anyway. You can kill them, and then open those files again. Vim will detect the swap files and offer to recover:

$ pkill vim
$ vim /tmp/gameoverlayui.log
E325: ATTENTION
Found a swap file by the name ".gameoverlayui.log.swp"
          owned by: muru   dated: Fri Sep 25 08:37:49 2015
         file name: /tmp/gameoverlayui.log
          modified: YES
         user name: muru   host name: muru-laptop
        process ID: 11066
While opening file "gameoverlayui.log"
             dated: Fri Sep 25 08:37:43 2015

(1) Another program may be editing the same file.  If this is the case,
    be careful not to end up with two different instances of the same
    file when making changes.  Quit, or continue with caution.
(2) An edit session for this file crashed.
    If this is the case, use ":recover" or "vim -r gameoverlayui.log"
    to recover the changes (see ":help recovery").
    If you did this already, delete the swap file ".gameoverlayui.log.swp"
    to avoid this message.

Swap file ".gameoverlayui.log.swp" already exists!
[O]pen Read-Only, (E)dit anyway, (R)ecover, (D)elete it, (Q)uit, (A)bort: 

1 You could use reptyr or one of the other solutions described in this Unix & Linux post to reattach the Vim process to your SSH session. I have no idea how that will work.

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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