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:
Only copy the path to the file's directory, running the script with the option
or
Copy the path including the file name, running the script with the option
The script
How to use
Install both
xdotool
andxclip
:Copy the script into an empty file, save it as
get_path.py
Test run the script:
gedit
fileopen a terminal window, run the command:
immediately switch to the
gedit
window, to make the last part of tyhe command run with thegedit
window in front.If all works fine, you can make the options available in two ways:
Make both options available in the
gedit
launcher:Copy the content below into an empty file, save it as
gedit.desktop
in~/.local/share/applications
In both lines:
and
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 ofxdotool
, 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:
the first option will copy to the clipboard the path to the file:
while the second option includes the file itself:
As you can see, spaces are taken care of :).