1

The following terminal command will open the text file ubuntu.css from any location of a file structure:

$ gedit admin:///usr/share/gnome-shell/theme/ubuntu.css

However, if I am already in directory /usr/share/gnome-shell/theme and I want to open the file ubuntu.css with gedit, how should I submit the above command without having to type the full path again?

Edit: I am adding a link here that I just found that give a short intro to the above command. But it does not deal with the issue that I have identified in my question.

Sun Bear
  • 3,014

2 Answers2

4

Unfortunately, no, it is not possible to pass a relative path with this admin:// URI. This answer may be disappointing, but that is how it currently (Ubuntu 18.04, Ubuntu 19.10) works.

Using a short wrapper script

You can, however, very conveniently work around the issue with a wrapper script. With the script sedit, you edit a file correctly with administrator privileges, just by typing the command and a filename, as in sedit ubuntu.css.

sedit):

```bash
#!/bin/bash
gedit admin://$(readlink -f "$1")
```

If you place that in a folder in your path, the command sedit ubuntu.css will open the file using the admin URI. Also providing the full path, or any valid path, will work.

Other options

Other, more standard options to not to have to type the pathname are:

  • You can use Tab expansion once you typed the three slashes of the URI.

  • You could drag the file from Files (nautilus) into the terminal. Thus, you could type "gedit admin://' in the terminal, find the file in Files, and then drag the file from Files into the terminal. This can make it easier to enter the URI in the terminal.

  • You could avoid typing the path using $(pwd)filename or $(readlink -f filename).

    gedit admin://$(pwd)/ubuntu.css
    

    or

    gedit admin://$(readlink -f ubuntu.css)`
    
  • You can install the nautilus python extension, nautilus-admin. Install it with the command sudo apt install nautilus-admin or using Synaptic Package manager (unfortunatelly, you cannot find it using Software). This small python extension integrates in the right-click menu, and converts the selected file to an 'admin://' URI for editing with root permissions.

vanadium
  • 97,564
0

Use pwd command for current directory

The pwd command returns the current directory so you can embed it in your command line:

gedit admin://$(pwd)/rootfilename

Choose a different method

As this question Which best practice for using gedit as root? points out:

My researchs brought me these proposals, are they equivalent or to be rejected :

  • pkexec env DISPLAY=$DISPLAY XAUTHORITY=$XAUTHORITY gedit

  • gedit admin:///path/to/document

  • sudo -i gedit

  • sudo -H gedit

You are using option 2 but I prefer option 4 sudo -H gedit which allows relative path when editing a root owned file.


Edit directly from Nautilus

If you are using Nautilus consider the extension Edit as Administrator which dramatically improves your workflow:

nautilus admin.gif


Use your user profile for gedit

The problem with gedit as root is you loose your theme colors, font size, tab settings, line wrap settings, and plug-ins. I wrote a script called sgedit (sudo gedit) that solves that problem.

In the animation below the gedit plug-in shows thumbnail overview of document on the right pane. Grab the slider mini-window with a mouse Left Click and drag it down to quickly locate sections in the document.

Other user settings for theme, font size, line wrapping, tab stop settings, convert tabs to spaces, and all your regular user profile plug-ins are available when running as sudo.

sgedit 80 column right slider.gif