How to open Nautilus directory in current Terminal? - Ask Ubuntu


i don't have dozens of terminals opened. there way add open in current terminal instead of open in terminal when using nautilus?

dirty or not, solution below worked in 30 minutes tested frequently. solution works long can right-click file (any) inside directory:

1.

enter image description here

2.

enter image description here

3.

enter image description here

nautilus script

#!/usr/bin/env python3 import subprocess import os import time  def replace(path):     c in [("%23", "#"), ("%5d", "]"), ("%5e", "^"),               ("file://", ""), ("%20", " ")]:         path = path.replace(c[0], c[1])     return path  def get(command):     try:         return subprocess.check_output(command).decode("utf-8").strip()     except subprocess.calledprocesserror:         pass  t = get(["pgrep", "gnome-terminal"]) if t:     w = [l.split()[0] l in get(["wmctrl", "-lp"]).splitlines() if t in l][0]     # current path     current = replace(os.getenv("nautilus_script_current_uri"))     dr = os.path.realpath(current)     # raise found terminal window     subprocess.call(["wmctrl", "-ia", w])     time.sleep(0.3)     # copy cd command clipboard     c1 = "printf 'cd ' | xclip -in -selection c"     c2 = 'echo "'+"'"+dr+"'"+'"  | xclip -in -selection c'     # paste & run     c in [c1, c2]:         subprocess.call(["/bin/bash", "-c", c])         subprocess.call(["xdotool", "key", "control_l+shift+v"])         time.sleep(0.05) 

how use

  1. the script needs wmctrl, xdotool , xclip:

    sudo apt-get install wmctrl xdotool xclip 
  2. copy script empty file, save open_in_terminal (no extension) in ~/.local/share/nautilus/scripts. create directory if needed. make script executable

that's it. log out , in , you'll have script available in image (2).

explanation

  • right- clicking file, can path using nautilus' "nautilus_script_current_uri".
  • in script, can load path clipboard (using xclip)
  • subsequently, script raises (first found) gnome-terminal window , pastes path, preceded cd command. since used echo load whole line clipboard, return included.

notes

  1. it should clear there should nothing running in terminal, , works best if there 1 terminal window open. in case there multiple, script picks oldest gnome-terminal window.
  2. the script needs tested thoroughly in practice. while ran it, timing no issue single time, not when desktop had "travel" across 4 or 5 viewports terminal window. if errors occur however, few lines more make wait in smart(er) way terminal window raised. let's see happens. doesn't seem necessary.
  3. since script uses realpath, linked directories work correctly.

more information on nautilus scripts here.


alternatively, pick own terminal window if have multiple

if want able choose in which terminal window open current (nautilus) directory, use script below.

how works in practice

  1. right- click (any) file inside directory (in case desktop) below:

    enter image description here

  2. click on (or raise otherwise) terminal window you'd use, , cd directory:

    enter image description here

the script

#!/usr/bin/env python3 import subprocess import os import time  def replace(path):     c in [("%23", "#"), ("%5d", "]"), ("%5e", "^"),               ("file://", ""), ("%20", " ")]:         path = path.replace(c[0], c[1])     return path  def get(command):     try:         return subprocess.check_output(command).decode("utf-8").strip()     except subprocess.calledprocesserror:         pass  # check if gnome-terminal runs pid = get(["pgrep", "gnome-terminal"])  if pid:     t = 0     while t < 30:         # see if frontmost window terminam window         active = get(["xdotool", "getactivewindow"])         if pid in get(["xprop", "-id", active]):                    # current path             current = replace(os.getenv("nautilus_script_current_uri"))             dr = os.path.realpath(current)             # copy cd command clipboard             c1 = "printf 'cd ' | xclip -in -selection c"             c2 = 'echo "'+"'"+dr+"'"+'"  | xclip -in -selection c'             # paste & run             c in [c1, c2]:                 subprocess.call(["/bin/bash", "-c", c])                 subprocess.call(["xdotool", "key", "control_l+shift+v"])                 time.sleep(0.05)             break         else:             t += 1             time.sleep(0.5) 

setup

is first script.

explanation

the script has 1 difference first one: instead of automatically raising first found terminal window, waits first terminal window have focus. then cd's directory inside window.


Comments

Popular posts from this blog

download - Firefox cannot save files (most of the time), how to solve? - Super User

windows - "-2146893807 NTE_NOT_FOUND" when repair certificate store - Super User

sql server - "Configuration file does not exist", Event ID 274 - Super User