Ubuntu – How to copy the path to the currently opened file in gedit to the clipboard

bashclipboardcommand linegeditscripts

I am trying to write a custom command in Gedit which copies the currently open and active document's path(both upto parent dir & upto file) to clipboard, as I couldn't find any gedit-plugins or tools that can do this.

I have no clue yet as to where to start from, nor have any good references, but I know I have to do scripting in bash script.

I searched for external command to copy any string to clipboard from terminal(as it also runs bash script) but the answers suggest use of "xclip" tool, which I have tried and am disappointed as any string when copied with xclip can only be pasted with "xclip -o" command. I need the copied string to be paste-able with Ctrl-V so I can open the path in file manager(nautilus).

Any help/suggestion is appreciated.

Best Answer

Script to copy the path of a file, opened in gedit

With the gedit window in front, the small script below derives the path from the (gedit) window's name, and copies it to the clipboard.

The script has two options:

  1. Only copy the path to the file's directory, running the script with the option

    -path
    

    or

  2. Copy the path including the file name, running the script with the option

    -file
    

The script

#!/usr/bin/env python3
import subprocess
import sys

name = subprocess.check_output(["xdotool", "getactivewindow", "getwindowname"]).decode("utf-8").strip()
if all(["(" in name, ")" in name]):
    path = name[name.find("(")+1:name.find(")")]
    if sys.argv[1] == "-file":
        fname = name[:name.find("(")]
    elif sys.argv[1] == "-path":
        fname = ""
    command = "echo "+'"'+path+"/"+fname+'"'+" | xclip -selection clipboard"
    subprocess.Popen(["/bin/bash", "-c", command])

How to use

  1. Install both xdotool and xclip:

    sudo apt-get install xdotool xclip
    
  2. Copy the script into an empty file, save it as get_path.py

  3. Test run the script:

    • open an existing gedit file
    • open a terminal window, run the command:

      sleep 5 && python3 /path/to/get_path.py -file
      

      immediately switch to the gedit window, to make the last part of tyhe command run with the gedit window in front.

    • Press Ctrl+V somewhere to paste the just copied path.
  4. If all works fine, you can make the options available in two ways:

    1. Create two shortcut keys for both options: choose: System Settings > "Keyboard" > "Shortcuts" > "Custom Shortcuts". Click the "+" and add both commands to two different shortcuts.
    2. Make both options available in the gedit launcher:

      enter image description here

      Copy the content below into an empty file, save it as gedit.desktop in ~/.local/share/applications

      [Desktop Entry]
      Name=gedit
      GenericName=Text Editor
      Comment=Edit text files
      Exec=gedit %U
      Terminal=false
      Type=Application
      StartupNotify=true
      MimeType=text/plain;
      Icon=accessories-text-editor
      Categories=GNOME;GTK;Utility;TextEditor;
      X-GNOME-DocPath=gedit/gedit.xml
      X-GNOME-FullName=Text Editor
      X-GNOME-Bugzilla-Bugzilla=GNOME
      X-GNOME-Bugzilla-Product=gedit
      X-GNOME-Bugzilla-Component=general
      X-GNOME-Bugzilla-Version=3.10.4
      X-GNOME-Bugzilla-ExtraInfoScript=/usr/share/gedit/gedit-bugreport
      Actions=Window;Document;divider1;Copy current file's directory;Copy path+file name;
      
      Keywords=Text;Editor;Plaintext;Write;
      X-Ubuntu-Gettext-Domain=gedit
      
      [Desktop Action Window]
      Name=Open a New Window
      Exec=gedit --new-window
      OnlyShowIn=Unity;
      
      [Desktop Action Document]
      Name=Open a New Document
      Exec=gedit --new-document
      OnlyShowIn=Unity;
      
      [Desktop Action Copy current file's directory]
      Name=Copy current directory
      Exec=python3 /path/to/get_path.py -path
      OnlyShowIn=Unity;
      
      [Desktop Action divider1]
      Name=.....................................
      OnlyShowIn=Unity;
      
      [Desktop Action Copy path+file name]
      Name=Copy current directory, include file name
      Exec=python3 /path/to/get_path.py -file
      OnlyShowIn=Unity;
      

    In both lines:

    Exec=python3 /path/to/get_path.py -path
    

    and

    Exec=python3 /path/to/get_path.py -file
    

    replace /path/to/get_path.py by the real path to the script.

    Log out and back in to make Unity "switch" to the new, local .desktop file.

Explanation

In the gedit window name, the path is displayed between ( and ). The script simply sees the frontmost window with the help of xdotool, then reads the path between those two characters.

Notes

Since the path is read in a textual way, the script will fail if the file's name includes other () characters.

Examples

With the following window in front:

enter image description here

the first option will copy to the clipboard the path to the file:

~/Bureaublad

while the second option includes the file itself:

~/Bureaublad/some test file.txt

As you can see, spaces are taken care of :).

Related Question