gedit – 显示最近打开的文件的文件路径

Gedit显示最近打开的文件。

如何使gedit不仅显示文件名,还显示最近打开的文件的文件路径?

我在一个在许多地方都有index.tcl / index.adb的网站上工作。 所以我有3个时间index.tcl 。 但我想知道哪一个。

最干净的解决方案当然是编辑gedit的代码。 由于这似乎遥不可及,下面的解决方案是一种解决方法。


如果最近使用的gedit文件的路径信息对您很重要,则可以使用该解决方案替代gedit自己的“最近使用的”概述。 它为您提供最近十个使用过的文件中与gedit窗口标题栏上显示的信息完全相同的信息。

在此处输入图像描述

它的存在:

  1. 一个(非常轻的)后台脚本,跟踪可能打开的gedit文件
  2. 用于调用最近使用的文件列表的脚本。

只需在后台运行脚本[1] ,将脚本[2]放在快捷键下,或将其添加为gedit启动器快速列表的快捷方式。

结果:

如何设置

安装程序需要安装wmctrl

 sudo apt-get wmctrl 

然后:

  1. 将下面的脚本( [1] )复制到一个空文件中,保存为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. 将下面的脚本复制到一个空文件中,保存为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. 使用以下命令打开终端窗口,测试驱动脚本[1]

     python3 /path/to/get_latestgedit.py 

    当脚本运行时,打开一些现有的gedit文件,让它们打开至少5-10秒(循环中断时间)。 现在使用命令(来自另一个终端)使用以下命令测试运行脚本[2]

     python3 /path/to/get_geditlist.py 

    应该显示最新的gedit文件,如图所示。

  4. 如果一切正常,请将脚本[1]添加到启动应用程序:Dash> Startup Applications> Add。 添加命令:

     python3 /path/to/get_latestgedit.py 

    并将脚本[2]添加到快捷键:选择:系统设置>“键盘”>“快捷方式”>“自定义快捷方式”。 单击“+”并添加命令:

     /bin/bash -c "sleep 10&&python3 /path/to/get_geditlist.py" 

    或者

    将其添加为gedit启动器的快速列表项,如图所示:

    在此处输入图像描述

    • gedit启动器从/usr/share/applications to复制/usr/share/applications to 〜/ .local / share / applications`:

       cp /usr/share/applications/gedit.desktop ~/.local/share/applications 
    • gedit打开本地副本:

       gedit ~/.local/share/applications/gedit.desktop 
    • 寻找线:

       Actions=Window;Document; 

    将其更改为:

      Actions=Window;Document;Recently used; 
    • 最后,在文件中添加一个部分:

       [Desktop Action Recently used] Name=Recently used Exec=python3 /path/to/get_geditlist.py OnlyShowIn=Unity; 

    当然,用脚本的真实路径替换python3 /path/to/get_geditlist.py [2]

说明

脚本[1]检查(每五秒一次):

  • 如果打开gedit窗口,通过检查pidof gedit是否具有有效输出。 然后,如果是这样:
  • 它检查gedit的pid是否出现在wmctrl -lp输出中的wmctrl -lp或多行中。 如果是这样,它会过滤掉行, 包含“(”和“)”之间的有效路径,因为这些窗口不是来自保存的文件。
  • 其余窗口将添加到最近使用的窗口列表中。 如果窗口(文件)已经出现在列表中,则会更新其时间顺序位置。 此外,当列表超过十(窗口)时,脚本将删除所有(最旧的)窗口。

如果最近使用的窗口列表发生更改,则列表将写入home目录中的隐藏文件。

脚本[2]读取由scrip1 [1]创建和更新的隐藏文件。 被叫时:

  • 该脚本读取隐藏文件中包含窗口标题的行。
  • 从标题中解析出路径 ,窗口标题显示在Zenity列表中。
  • 如果用户从列表中选择一个窗口,则路径或文件名中可能出现的空格将“固定”该文件的路径,随后使用gedit打开该文件。

快速破解,只需打开您认为您想要的所有文件,然后在右上角的菜单中找到每个文件的路径,这样您就可以保留所需的内容并关闭其他文件。

在此处输入图像描述