I’ve lost the habit of nervously pressing Control–S every few minutes to save documents changes. However, when disaster strikes or you leave your computer without saving; how do you recover an unsaved document from a running instance of the GNOME Text Editor (gedit) document through the console?
To make a long story short, my GNOME desktop have crashed a few times on me lately. Leaving me with unsaved changes or entire documents in GEdit that I needed to recover. I’ve also had the same problem when leaving GEdit running with an unsaved document on a remote machine. Recreating these documents would take significant time. Instead, I tried finding a way to recover them from the still running instances of GEdit.
If you can get a terminal open or even remotely connect to the system where GEdit is running, you can likely still rescue the document. I concocted the following little program in Python to do the job. The program asks the dbus service for a list of windows belonging to GEdit. It then selects and copies all the text in each window, and prints that to the terminal. From there you can pipe it back to a file or do whatever you need with it.
- Grab recover-gedit-document-remotely.py
- Skim through and review the code (because you shouldn’t blindly run code off the Internet)
- Run it on the troublesome machine
Since you’ll probably be running it in an interactive terminal shell without a display server attached, you’ll need to specify the default display:
- DISPLAY=:0 ./recover-gedit-document-remotely.py
Documents that are opened in tabs instead of individual windows are a bit harder to recover, as GEdit and dbus aren’t the best of friends. You can instruct GEdit to the tab to the left or right of the current. But GEdit will not volunteer any information about a window’s tabs, so it’s hard to know if there are any tabs or not. The program has two lines of code for navigating between tabs. These are commented out and you can bring them back to life by uncommenting them. They will blindly switch one tab to the left or right. You’ll need blind experimentation to get anywhere with this … .
This little program might not be the ideal solution to the problem. However, when you need to recover a unsaved GEdit document, this may be just the thing to save the day. By the by, you will lose anything stored on the clipboard on the affected machine. Sorry about that, but this is meant as an emergency recovery utility.
The program depends on the Python modules dbus, gi.repository, and lxml to communicate with GEdit. Written in Python 3. It could probably be rewritten to run in legacy Python, but I don’t see the need.