9

Scenario:

  1. I am on my Desktop.
  2. I perform a right mouse click and select "Open in terminal".
  3. I envision this to open the terminal in ~/Desktop and not my home folder.

System:

  • Ubuntu 16.10
  • Unity
  • Nautilus
1
  • Isn't it really much faster to Ctrl-T into a new terminal and then $cd Desktop? You can probably even tab-complete after writing Des
    – tomph
    Jan 9 '17 at 14:20
8

Script-less solution

  1. Open Nautiuls in your home folder.
  2. In your home folder, right click on Desktop, click Make link. You will be presented with Link to Desktop file.
  3. Place link on your Desktop. Now whenever you right click on it, and select Open in Terminal it will open terminal with current working directory set as desktop.
2
  • This is a clever workaround!
    – orschiro
    Jan 9 '17 at 12:03
  • @orschiro Thanks, and it's fast ,too. Takes less than a minute to set up . Jan 9 '17 at 12:08
7

A quick & easy one coincidentally, since I only needed to change a script I already had a bit :).

The script assumes you have at least one item on your desktop though.

How to set up

  1. Create, if it doesn't exist yet, the directory ~/.local/share/nautilus/scripts
  2. Copy the script below into an empty file, save it as terminal_here2 (no extension), and make it executable
  3. Log out and back in.

Now right-click on any file on your desktop, choose Scripts --> open_terminal2:

enter image description here

and a terminal window will open in the desktop's directory ("Bureaublad" in my case)

The script:

#!/usr/bin/env python3
import subprocess
import os

def replace(path):
    for c in [("%23", "#"), ("%5D", "]"), ("%5E", "^"),
              ("file://", ""), ("%20", " ")]:
        path = path.replace(c[0], c[1])
    return path

# get the current path
current = replace(os.getenv("NAUTILUS_SCRIPT_CURRENT_URI")) 
# raise the found terminal window
os.chdir(os.path.realpath(current))
subprocess.Popen("gnome-terminal")
9
  • 1
    You should probably use urllib.parse.unquote instead of manually replacing %-encoded characters
    – muru
    Jan 9 '17 at 8:27
  • Thank you! There is no way to use the already existing "Open in terminal" context menu entry?
    – orschiro
    Jan 9 '17 at 8:57
  • @orschiro I am afraid that is hard-coded in nautilus. Obviously the desktop is not classified as a "normal" window in nautilus, although NAUTILUS_SCRIPT_CURRENT_URI shows the correct directory. Jan 9 '17 at 8:59
  • Do you think it is worth filing a feature request for it? It would be consistent that this function would work as expected.
    – orschiro
    Jan 9 '17 at 9:02
  • 2
    There's no need to keep the Python process running just to wait for gnome-terminal to terminate and then terminate itself. Instead you can call os.execlp("gnome-terminal", "gnome-terminal") to replace the Python process with gnome-terminal. Jan 19 '17 at 10:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.