Q:

How to know when you have a file open in vi while using the shell?

When I am editting a file in vi or vim, I frequently will be editting the file, and then I need to open a shell to execute a shell command.  So, I go to the shell by doing :sh.
This brings me to the shell.  Now, when I want to return to the file I'm editting, I will freqquently do control + D to return the file.  This works fine.  I followed the instructions here.
I do a lot of work on remote AWS machines which I am ssh-d into.  And, control+D is one way to exit those machines.  Unfortunately, while ssh-d into those machines, sometimes I do not know if I have a file open in vi/vim and when I do control + D to go back to the file, it exits my ssh session entirely. 
Is there a way to either 

1) know that I have a file currently open in vi or 
2) exit the shell and go back to my file safely without risking accidentally closing my ssh session?

A:

It looks like the problem you're having isn't trying to figure out whether vi is running but rather how to jump between the parent shell and vi using "job control" (grep for job control in the bash  or sh man page).
Use ctrl-z to background vi and use fg to resume. This works in all POSIX-derived shells and it works over ssh.
This is preferred over using :sh and ctrl-d since you avoid the problem you're having, you get the same shell that you were working with when you started vi, and you have one less shell running (this isn't often a performance issue but if you have a lot of shells running it's easier to look at their PIDs when needed).
There may be other reasons for :sh but the only one I know of is getting a shell in a vi session that wasn't started from a shell to begin with.
Having said that, vi -r (with no args) prints a list of swap files that subsumes the set of files open in vi. Commands like vi -r |& grep -B 3 'still running' are useful.

