How can I copy the path to the currently opened file in gedit to the clipboard?

10,934

Solution 1

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

Solution 2

After some documentation search I was able find solution so I am gonna answer my question here.

Open Gedit and go to "Tools" >> "Manage External Tools" and create new tool.

Add following command just like below:

echo -n $GEDIT_CURRENT_DOCUMENT_URI | xclip -sel clip;exit;

Close it and then restart Gedit(just for confirmation).

Open any document and then go to Tools >> External Tools and then click on the command label just created. The path will be in your clipboard.

Share:
10,934

Related videos on Youtube

Vicky Dev
Author by

Vicky Dev

B.E. COMPUTER ENGG. WEB DEVELOPER (PHP/Magento/Wordpress/jQuery/Core Javascript/PrototypeJs/)

Updated on September 18, 2022

Comments

  • Vicky Dev
    Vicky Dev over 1 year

    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.

    • meuh
      meuh over 8 years
      use xclip -selection CLIPBOARD to set the clipboard that is pasted by ctrl-v.
    • Jacob Vlijm
      Jacob Vlijm over 8 years
      Posted my answer. Please let me know if you manage.
    • Jacob Vlijm
      Jacob Vlijm over 8 years
      Hi Vivek, completely rewrote the answer and combined the two scripts.
    • A.B.
      A.B. over 8 years
      Have you tried this? github.com/gfxmonk/gedit-plugins
  • Vicky Dev
    Vicky Dev over 8 years
    Thank you for your effort and suggestion Jacob, I will try in my gedit(in office system) and let you know :)
  • Jacob Vlijm
    Jacob Vlijm over 8 years
    @VIVEKSHAH could you open a document in gedit (saved on disc) and open a terminal. Then run the command: sleep 5 && python3 /path/to/get_path.py, immediately switching to the gedit window (so thet the gedit window is in front once the script runs)? Then after the five seconds have passed, press Ctrl+V somewhere (not in terminal :) ). Possibly there is an error in the chosen shortcut key. The script (both of them) run without a single error here.
  • Vicky Dev
    Vicky Dev over 8 years
    Yes about shortcut key I think I may have created a conflicting shortcut key. And I will run your python file in terminal, but can you tell me how can I make this run as Gedit menu item like Edit > "Copy document path" or in "external tool" menu ?
  • Jacob Vlijm
    Jacob Vlijm over 8 years
    Breaking into the menu structure of gedit is a completely different type of sport, which I cannot play :). You can however make it available as a shorcut in the gedit launcher. Would that be an option? btw did you get it to work?
  • Vicky Dev
    Vicky Dev over 8 years
    That would also do, I can create another shortcut to only copy document path, how can I pass this command in the Gedit shortcut, using same path "python3...." ?
  • Jacob Vlijm
    Jacob Vlijm over 8 years
    @VIVEKSHAH I can add it to the answer if you like, but did the test with the terminal work?
  • Vicky Dev
    Vicky Dev over 8 years
  • Jacob Vlijm
    Jacob Vlijm over 8 years
    @VIVEKSHAH just curious, but did you try?
  • Vicky Dev
    Vicky Dev over 8 years
    Yes I created shortcut with the code and the path you posted here and it works like a charm. Thanks very much, I will upvote your answer.
  • Jacob Vlijm
    Jacob Vlijm over 8 years
    @VIVEKSHAH perfect! very nice it works well.
  • Vicky Dev
    Vicky Dev over 8 years
    Just one more thing if you don't mind, I have files in the LAN connected pcs too, so if I open the files for other system(LAN connected) the actual path is like "run/gvfs/...." but when I open in Gedit and run "copy document path" shortcut, I get path like "foldername on 192.168.0.1.." so how can I get actual path for those files in clipboard ?
  • Jacob Vlijm
    Jacob Vlijm over 8 years
    @VIVEKSHAH Can be done, also change ~ into an absolute path, but then you have to give me some "aliasses". Could you give me how it shows now and how it should be? I can create it so you can mange it yourself then.
  • Vicky Dev
    Vicky Dev over 8 years
    Right now I don't remember the exact path, but I will surely give you tomorrow.
  • Vicky Dev
    Vicky Dev over 8 years
    The exact path is like this: /run/user/1000/gvfs/smb-share:server=192.168.0.160,share=fol‌​dername/subfolder/si‌​teroot/. Also the curent document path copy doesn't work everytime, even if I keep my terminal and gedit open with document in focus. Can you let me know how to debug this code ?
  • Jacob Vlijm
    Jacob Vlijm over 8 years
    @VIVEKSHAH I used it for a while without an error, it must be something with specific window (names). Is it always with the same window, or at random? If it happens, could you post the window name in a comment? The path to replace is not compltely clear to me; what should /run/user/1000/gvfs/smb-share:server=192.168.0.160,share=fol‌​dername/subfolder/si‌​‌​teroot/ be replaced by?
  • Vicky Dev
    Vicky Dev over 8 years
    I think when I have already anystring copied in clipboard then it doesn't copy the document path. Also about the path, the original path is (I have smb sharing scheme) smb://192.168.0.160/foldername/subfolder/siteroot it should be converted to the "run/gvfs..." path I posted above. Can it be done in python ?
  • Vicky Dev
    Vicky Dev over 8 years
    Do let me know about changes to your python code needed for this, but as far as bash script is concerned, I was able to solve the issue, I will post my answer shortly, thanks for the name of tool "xclip" & "xdotool" they really helped me ;)
  • Jacob Vlijm
    Jacob Vlijm over 8 years
    +1, Very neat! Do you still need my update?
  • Vicky Dev
    Vicky Dev over 8 years
    Yes I would also like to learn python I am also learning it at my home, so a little bit of insight in complex coding would be good, solely if you don't mind.