5

Gedit shows the recently opened files.

How do I make gedit show not only the file-names but also the file path for recently opened files?

I work on a website which has an index.tcl / index.adb in many locations. So I have for example 3 time index.tcl. But I'd like to know which one.

0

3 Answers 3

1

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 .

enter image description here

It exist of:

  1. a (very light) background script, keeping track of possibly opened gedit files
  2. 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:

  1. 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
    
  2. 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
    
  3. 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.

  4. 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:

    enter image description here

    • 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.
4
  • @Jacom Vlijm thx for the repy. The test does not succeed - since I'm not used to python I'll have to debug it in the next days (not much time to do debugging / lerning python Jul 3, 2015 at 9:33
  • @ThorstenNiehues Hi Thorsten, there should be nothing to debug, but if you post back what is the terminal output, we'll have it fixed no doubt. That is if you like the solution. Does script [1] run, and does it create a file ~/.latest_gedit.txt if you open gedit files for at least a few seconds?? Jul 3, 2015 at 11:48
  • @Jacom Vlijm the problem was that 'wmctrl' was not installed. Not the script works - thanks. Jul 14, 2015 at 7:24
  • Hi @ThorstenNiehues Thanks for the edit, but I'd prefer not importing additional stuff if it does not add functionality :) Jul 14, 2015 at 7:36
1

A quick hack, just open all files you think you want, then at the top right menu you can find the path of each file, so you can keep what you want and close others.

enter image description here

2
  • OK that's what I am doing now - but it's annoying Jul 3, 2015 at 6:34
  • @ThorstenNiehues I don't think there is other way in native gedit
    – Maythux
    Jul 3, 2015 at 6:35
0

If by any chance, someone's still wondering, gedit has natively implemented showing path of a file in multiple ways. It must've been available for a long time now but v3.36.2 has it for sure.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .