How to make gedit terminal itself to change directory to the location of the file which is opened currently in gedit? As by default i have to cd till i get to present file location in my gedit-embedded terminal. I know there is no direct way to do so. But if any long way is also possible, I really want the embedded terminal itself to make dynamically change directory as per currently opened file in whatever current tab is opened in gedit.
-
It seems it can be done, but definitely "the long way". See my updated my answer. – Jacob Vlijm Dec 21 '14 at 15:49
-
you are genius for the effort you have carried. I can see that throughout your work.but let me check it....thanks for the effort you took. – Sumit Singh Dec 21 '14 at 15:52
-
fixed an error occurring on multiple windows, it works best on one window, multiple tabs. – Jacob Vlijm Dec 21 '14 at 16:40
1 Answers
To my own surprise, it can be done. In the 45 minutes I tested it, it worked quite well actually, and didn't fail on me once.
Nevertheless, it should be considered an experimental option, since it is done completely "from outside", so to speak, with the help of xprop
, wmctrl
and xdotool
. To use it, you will have to accept a few peculiarities: it works well as long as you have all files tabbed in one window, and your mouse click, to select tabs, should be a real click: if you "hold" the click, the tab is opened in a new window.
You should also be aware of the fact that the different gedit
tabs (files) share one embedded terminal window. That means that the cd
command will be performed on tab switch, no matter what might be running in the terminal.
Alltogether, you will have to see if it works stable enough in your situation and if it behaves well in the way you use it.
How it works
The main trick is based on the fact that gedit by default has the file's directory in its window name. That is used to extract the targeted directory, and set it, if it changes (by clicking another tab of the gedit window).
A script, running in the background, checks:
- if gedit runs (at all)
- if so, it checks if the frontmost window is a gedit window
- if that is the case, it checks if the currently frontmost file's directory is different from the last check, so another tab must have been clicked (since 1 second ago). This also prevents needless
cd
actions if two tabs work in the same directory (that is: if all files are tabbed in one window).
Then, if a cd
action is needed, the script moves the mouse to the middle of the gedit
window (x-wise), 70 px from the bottom of the window (y-wise), which will definitely be on the terminal section of the window. Then it clicks, types thecd
command + Enter. You will hardly notice, since the mouse is moved back to its original position. You will need to give it time (appr. 0.5 second) to perform the cd
before you continue to work, but again, You will probably not notice the delay at all.
How to use
If not yet on your system, install
wmctrl
andxdotool
first:sudo apt-get install wmctrl sudo apt-get install xdotool
Copy the script below into an empty file, save it as
set_directory.py
Run it by the command:
python3 /path/to/set_directory.py
I suggest you run it from a (minimized)
gnome-terminal
window while you are working withgedit
+ embedded terminal; to have it running permanently seems a bit overkill for such a specific task.
The script
#!/usr/bin/env python3
import os
import subprocess
import socket
import time
home = os.environ["HOME"]
last_dr = ""
def get(cmd):
try:
return subprocess.check_output(["/bin/bash", "-c", cmd]).decode("utf-8").strip()
except:
return ""
def execute(cmd):
subprocess.call(["/bin/bash", "-c", cmd])
def get_windowid():
cmd = "xprop -root"
frontmost = [l for l in get(cmd).splitlines() if "ACTIVE_WINDOW(WINDOW)" in l][0].split()[-1]
return frontmost[:2]+"0"+frontmost[2:]
def get_dirfromname(name):
dr = name[name.find("(")+1:name.find(")")].replace(" ", "\\ ")
if "~" in dr:
dr = dr[dr.find("~"):]
if os.path.exists(dr.replace("~", home+"/")):
return dr
else:
return None
def cd(coords, dr):
currmouse = get("xdotool getmouselocation").split()[:2]
currcoords = (" ").join([it.split(":")[-1] for it in currmouse])
x_set = str(int((int(coords[0])+int(coords[2])/2)))
y_set = str(int(int(coords[1])+int(coords[3])-70))
execute("xdotool mousemove "+x_set+" "+y_set+"&&xdotool click 1")
execute("xdotool type 'cd "+dr+"'"+"&&xdotool key KP_Enter")
execute("xdotool mousemove "+currcoords)
def update_directory():
global last_dr
try:
pid = get("pidof gedit")
except subprocess.CalledProcessError:
pass
else:
wid = get_windowid()
wdata = get("wmctrl -lpG")
wdata = [l for l in wdata.splitlines() if pid in l and wid in l] if len(wdata) != 0 else []
if len(wdata) != 0:
wdata = wdata[0]; coords = wdata.split()[3:7]
wname = wdata.split(socket.gethostname()+" ")[-1]
dr = get_dirfromname(wname)
if dr != None and dr != last_dr:
time.sleep(1)
cd(coords, dr)
last_dr = dr
while True:
update_directory()
time.sleep(1)
Automatically set the working directory to the currently front most file:
cd on tab switch
The built-in cd menu of the gedit terminal
An alternative, almost with the same comfort, could be the gedit terminal built in function. It only takes two mouse clicks (one right- one left):
- Right-click with the mouse in the terminal window
Choose "Change Directory" and the terminal window will cd to the currently frontmost file's directory:

- 83,767
-
but is there any way you can automate the embedded terminal to auto change it's path to currently opened directory?? – Sumit Singh Dec 20 '14 at 17:23
-
thanks for your contribution ,but again i need to run the script everytime i switch on my pc. is there any way i can make some changes to files of gedit(assuming ubuntu exposes all its files) so that it is being run automatically with gedit. – Sumit Singh Apr 30 '15 at 13:34
-
I could make an edited version of this one, to make sue it runs when gedit is run: http://askubuntu.com/questions/526484/fix-scaling-of-java-based-applications-for-a-high-dpi-screen/615754#615754 will look into it :) – Jacob Vlijm Apr 30 '15 at 14:00