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 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 documen. 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-remote-document.py
- Skim through and review the code (because you shouldn’t blindly run code off the Internet)
- Run it on the troublesome machine
Documents that are opened in tabs instead of individual windows are a bit harder to recover, as gedit and dbus are not 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 really 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 do not see the need.