Resuming a vim session I have accidentally disconnected from

12,940

Solution 1

Consider reptyr:

NAME
       reptyr - Reparent a running program to a new terminal

SYNOPSIS
       reptyr PID

Find vim's process id with pidof vim or ps aux | grep vim.

It is also possible to see the process id if you try to open a file which is still opened by vim. Using the shown pid you should be able to reattach to your previous session.

If you get an error:

Unable to attach to pid 12345: Operation not permitted

Ensure that the ptrace scope is set to 0 (instead of 1):

echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope

After running reptyr, you may have to press Enter or another key to refresh the terminal (console) window.

Solution 2

vim -r {file} is what you want, "r" for "Recovery mode". See man vim:

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".
Share:
12,940

Related videos on Youtube

hamhut1066
Author by

hamhut1066

Updated on September 18, 2022

Comments

  • hamhut1066
    hamhut1066 over 1 year

    I came across an unusual problem today,

    I was using vim, had created a new alias in bash, and wanted to try it out.

    So I ctrl-z, exec bash (I do this because I don't like nesting it with bash).

    And my job has gone. And I now can't resume vim.

    The only option I can think of is to kill vim and start a new session.

    vim
    

    ctrl-z

    exec bash
    fg  
    

    Edit: This is independent of using screen/tmux.

  • hamhut1066
    hamhut1066 almost 10 years
    I already do this, but the point is, I have no way to reattach to it, it was in a screen session, but I restarted my bash shell, and it didn't kill my vim
  • hamhut1066
    hamhut1066 almost 10 years
    yes, this is probably the best option, but I would like to have a way to reattach to the same session! something like emacs --daemon
  • hamhut1066
    hamhut1066 almost 10 years
    This would work... but I still can't kill or reattach to my vim session which is still running Just -9'd it...
  • lbaile200
    lbaile200 almost 10 years
    It would probably be a good idea, then, to add that information to your original question.
  • hamhut1066
    hamhut1066 almost 10 years
    I thought it clear enough, considering the instructions I give to emulate it. I have edited for clarity. thanks :
  • hamhut1066
    hamhut1066 almost 10 years
    Thanks, although this didn't work in this case, I ended up having to kill it. This is the solution I was looking for
  • Peter V. Mørch
    Peter V. Mørch almost 9 years
    This is GREAT. Now if I loose network connectivity, I can resume vim after logging in again without having to resort to killing vim and cleaning up with vim -r. Wish I could give more than one up-vote!
  • Peter V. Mørch
    Peter V. Mørch almost 9 years
    @Thor Thanks, I know about tmux and I "consider" it, but on average tend to forget / not bother :-)
  • Thor
    Thor almost 9 years
    @PeterV.Mørch: Me too, that's how I found reptyr :-)