14

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.

Zanna
  • 70,465

3 Answers3

19

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.

Zanna
  • 70,465
Thor
  • 3,578
  • Thanks, although this didn't work in this case, I ended up having to kill it. This is the solution I was looking for – hamhut1066 Jun 25 '14 at 09:24
  • 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 Jun 24 '15 at 23:43
  • 1
    @PeterV.Mørch: If this is a regular problem, you should consider running your programs within tmux or GNU screen. – Thor Jun 25 '15 at 07:27
  • @Thor Thanks, I know about tmux and I "consider" it, but on average tend to forget / not bother :-) – Peter V. Mørch Jun 25 '15 at 07:43
  • @PeterV.Mørch: Me too, that's how I found reptyr :-) – Thor Jun 25 '15 at 08:47
9

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".
Jos
  • 29,224
  • 1
    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 Jun 24 '14 at 13:08
  • This would work... but I still can't kill or reattach to my vim session which is still running Just -9'd it... – hamhut1066 Jun 24 '14 at 13:10
1

You'll want to use something like the 'screen' or 'tmux' utility to keep your terminal sessions going in the background.

lbaile200
  • 586
  • 5
  • 12
  • 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 Jun 24 '14 at 13:08
  • It would probably be a good idea, then, to add that information to your original question. – lbaile200 Jun 24 '14 at 13:11
  • I thought it clear enough, considering the instructions I give to emulate it. I have edited for clarity. thanks : – hamhut1066 Jun 24 '14 at 13:14