Resuming a vim session I have accidentally disconnected from

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.


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.


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".