I open a terminal then I open Vim. Then I work on my source code, but unfortunately I click on the close button of the terminal title bar and my work is lost. Is it possible to configure Vim such that it will tell me when I am trying to close the terminal without saving my work?

gnome-terminal will warn if there is any program running in the terminal windows including, of course, vim.
–
barrapontoFeb 23 '12 at 20:50

2

Which "Termimal"? There's a program called that on many different OSes, and they have different ways of being configured to cope with this problem.
–
Warren YoungFeb 23 '12 at 20:53

@Warren Young: It is a xfce4-terminal. According to the answer of Chris Down below, I would like to change the way my terminal handles WM_DELETE_WINDOW, but I do not know how to do it yet.
–
BenjaminFeb 23 '12 at 20:56

5 Answers
5

There is no way for vim to know in advance that it's grandparent process (the terminal) is killed so it too, is normally killed as a result of killing the parent processes.

However, on most default setups vim will create a .swp file named after the current file being edited; for instance MyPrecious.java.swp. This file should contain a snapshot of the file MyPrecious.java just before the vim process was killed. Unless you have set up vim to specifically put these .swp files in some other directory, they should reside in the same directory as the edited file and you can easily restore your editing work.

Note that some of these files starts with a dot . so they are invisible unless you use ls -a to list the files in a directory.

Looking at the problem from a different angle, screen or tmux will allow you to keep your shell going independently of your terminal. When you close the terminal you can open a new one and reconnect to the original shell. Vim will still be running.

vim has no control over how your terminal's GUI handles WM_DELETE_WINDOW. Instead, look for a setting in your terminal that controls how the terminal acts when asked to close when it has a shell with children.

I'm not familiar with Vim, but maybe it can be configured to save all work before exiting when a SIGHUP signal is received. If not, maybe you can set a shell trap on SIGHUP, perhaps in combination with running Vim with nohup, to execute a special command that somehow causes Vim to exit more gracefully when the terminal is closed.

As others have stated, vim cannot react to such an event. But you might consider one of these work-arounds:

Using gvim instead of vim, which can be invoked from the command line, too. An alias might help adjusting to the switch (assuming bash-compatible shell): alias vim=gvim. Stating gvim as your preferred editor might help, too: export EDITOR=gvim. This works only on a local machine, though.

Using a terminal muxer (such as tmux or screen) was already mentioned.

Configuring your terminal (whichever that is) to confirm that you want to close it. This will catch you by surprise the first few times, which should force you to think before closing your terminal in the long term.