The cleanest solution would be of course to edit the code of gedit
. Since that seems out of reach, the solution below is a workaround.
If the path information of recently used gedit files is important to you, the soluution can be used as a replacement of gedit's own "recently used" overview. It gives you the information, exactly as it appears on your gedit
window's title bar, of the last ten used files .
It exist of:
- a (very light) background script, keeping track of possibly opened
gedit
files
- a script to call a list of the most recently used files.
Simply run script [1]
in the background, put script [2]
under a shortcut key or add it as a shortcut to the gedit
launcher's quicklist.
The result:
How to setup
The setup needs wmctrl
to be installed:
sudo apt-get wmctrl
Then:
Copy the script below ([1]
) into an empty file, save it as get_latestgedit.py
#!/usr/bin/env python3
import subprocess
import os
import time
import socket
f = os.environ["HOME"]+"/.latest_gedit.txt"
n = 10
def get():
try:
pid = subprocess.check_output(["pidof", "gedit"]).decode("utf-8").strip()
gedit_w =subprocess.check_output(["wmctrl", "-lp"]).decode("utf-8")
matches = [l.split(socket.gethostname())[1] for l in gedit_w.splitlines() if all(
[pid in l, "~" in l or "/" in l])]
return matches
except:
return []
gedit_windows1 = get()
try:
latest = open(f).read().splitlines()
except FileNotFoundError:
latest = []
while True:
time.sleep(5)
gedit_windows2 = get()
new = [w for w in gedit_windows2 if not w in gedit_windows1]
if len(new) != 0:
for w in [w for w in gedit_windows2 if not w in gedit_windows1]:
try:
latest.remove(w)
except ValueError:
pass
latest.append(w)
if len(latest) > n:
latest = latest[(len(latest) - n):]
with open(f, "wt") as out:
for item in latest:
out.write(item+"\n")
gedit_windows1 = gedit_windows2
Copy the script below into an empty file, save it as get_geditlist.py
:
#!/usr/bin/env python3
import subprocess
import os
import sys
f = os.environ["HOME"]+"/.latest_gedit.txt"
try:
ws = open(f).read().splitlines()[::-1]
except FileNotFoundError:
open(f, "wt").write("")
try:
cmd = 'zenity --list --title="Choose"'+ \
' --text ""'+ \
' --column="Latest opened" '+\
(" ").join(['"'+s.strip()+'"' for s in ws])
to_open = subprocess.check_output(["/bin/bash", "-c", cmd]).decode("utf-8").split("|")[0]
path = to_open[to_open.find("(")+1: to_open.find(")")].strip()+"/"+to_open[:to_open.find("(")].strip()
sections = [s for s in (path.split("/"))]
for i, it in enumerate(sections):
if it.count(" ") != 0:
sections[i] = '"'+it+'"'
path = ("/").join(sections)
command = "gedit "+path
subprocess.Popen(["/bin/bash", "-c", command])
except subprocess.CalledProcessError:
pass
Open a terminal window, test-drive script [1]
with the command:
python3 /path/to/get_latestgedit.py
While the script runs, open a few existing gedit
files, leave them open for at least 5-10 seconds (the loop break time). Now test-run script [2]
with the command (from another terminal) with the command:
python3 /path/to/get_geditlist.py
The most recent gedit
files should show up, as shown in the image.
If all works fine, add script [1]
to your startup applications: Dash > Startup Applications > Add. Add the command:
python3 /path/to/get_latestgedit.py
and add script [2]
to a shortcut key: Choose: System Settings > "Keyboard" > "Shortcuts" > "Custom Shortcuts". Click the "+" and add the command:
/bin/bash -c "sleep 10&&python3 /path/to/get_geditlist.py"
Or,
add it as a quicklist item to your gedit
launcher, as shown in the image:
Copy the gedit
launcher from /usr/share/applications to
~/.local/share/applications`:
cp /usr/share/applications/gedit.desktop ~/.local/share/applications
Open the local copy in gedit
:
gedit ~/.local/share/applications/gedit.desktop
Look for the line:
Actions=Window;Document;
change it to:
Actions=Window;Document;Recently used;
To the very end, add a section to the file:
[Desktop Action Recently used]
Name=Recently used
Exec=python3 /path/to/get_geditlist.py
OnlyShowIn=Unity;
Of course, replace python3 /path/to/get_geditlist.py
by the real path to the script [2]
Explanation
Script [1]
checks (once per five seconds):
- if
gedit
windows are opened, by checking if the pidof gedit
has a valid output. Then, if so:
- it checks if
gedit
's pid occurs in one or more lines in the output of wmctrl -lp
. If so, it filters out the lines, not containing a valid path between "(" and ")", since those windows are not from saved files.
- The remaining windows are added to a list of recently used windows. If the window (the file) already occurs in the list, its chronological position is updated. Furthermore, the script deletes all (oldest) windows when the list exceeds a length of ten (windows).
If the list of recently used windows changes, the list is written to a hidden file in your home
directory.
Script [2]
reads the hidden file, created and updated by scrip1 [1]
. When called:
- the script reads the lines in the hidden file, containing the window's titles.
- from the titles, the path is parsed out, and the window titles appear in a
Zenity
list.
- If the user choosen a window from the list, the path to the file is "fixed" for possibly occurring spaces in either the path or the filename, subsequently the file is opened with
gedit
.