This came to me after I asked the question in another forum AskUbuntu: vim.desktop - changes lost when terminal exits and a lot of experimental
*.desktop files demonstrating that
gnome-terminal would allow
vim to be killed without warning even if it was running in an shell & even if there were other commands before or after it for
gnome-terminal to run.
An "interactive" shell (
bash -i) sounded sure to invoke the GNOME warning upon closing the window, based on the literal warning message:
Close this terminal? There is still a process running in this terminal. Closing the terminal will kill it.
but adding the
-i option to bash, as run from the
*.desktop file, stopped GNOME from opening a window in my testing (with Ubuntu 16.04 -
gnome-terminal 3.18.3). So instead I tried invoking the interactive shell from a wrapper script, and that was a full solution:
bash -ic 'vim "$@"' vim "$@"
... then edit
/usr/share/applications/vim.desktop or copy it to a new
*.desktop file, with these changes:
# It doesn't open window for interactive shell when GNOME arranges the terminal:
# Exec=vim-gnome-wrap %F
# ... yet it does so when gnome-terminal is run explicitly:
Exec=gnome-terminal -e "vim-gnome-wrap %F"
This provides a warning upon closing the window whether the
vim buffer has unsaved changes or not, and works on multiple files. Special thanks to @muru below for suggesting
$@ syntax which preserves arguments exactly (spaces & metacharacters).