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.
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
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
cdactions 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 the
cd 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
sudo apt-get install wmctrl sudo apt-get install xdotool
Copy the script below into an empty file, save it as
Run it by the command:
I suggest you run it from a (minimized)
gnome-terminalwindow while you are working with
gedit+ embedded terminal; to have it running permanently seems a bit overkill for such a specific task.
#!/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].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)+int(coords)/2))) y_set = str(int(int(coords)+int(coords)-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; 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: