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
  • 2,302
  • "gedit ubuntu.css" worked for me. If you want to edit file then use "sudo gedit ubuntu.css" – SHAHS Nov 23 '19 at 14:01
  • 3
    @SHAHS I thought we should move away from using sudo gedit and instead should move to use gedit admin:. Hence, I would like to better understand how to use the gedit admin: syntax. Are you able to help? – Sun Bear Nov 23 '19 at 14:07
  • If you do use sudo then use sudo -H gedit ubuntu.css. The -H option solves the problem of messed up your personal gedit config files caused by using plain sudo. – user68186 Nov 23 '19 at 14:26
  • @user68186 I noticed that the fonts sizes in gedit buttons and title bar appears different ( and not so proportionate) when using sudo -H gedit as compared to gedit admin://. – Sun Bear Nov 23 '19 at 15:07
  • I don't know why the fonts size in the buttons and title bar would be different. Unless you have some customization of gedit config that is ignored when you use -H option. – user68186 Nov 23 '19 at 15:13
  • @user68186 Thanks. I am using Ubuntu 18.04 with GNOME User Theme Extension activated. I did not customise gedit preferences specifically. In any case, I am looking towards better understanding how to use gedit admin:// w/o typing the full path when I am already in the cwd with ubuntu.css. Can you point the way to answer my question or that can help better understand the admin command? I can't find references that explain admin. I have noticed that it uses pkexec to open the file. This is unlike sudo. – Sun Bear Nov 23 '19 at 15:27
  • I don't know enough about pkexec and the admin:// prefix to answer your question. I tried a few variation of what you are trying to do and they didn't work for me either. So, I can't answer your question. – user68186 Nov 23 '19 at 15:32
  • 2
    I'm not on gnome, so I can't test it, but I would expect gedit admin://$(pwd)/ubuntu.css to work. If it works you could easily create an alias or wrapper script to make it even easier to use. – Lienhart Woitok Nov 23 '19 at 17:18
  • @LienhartWoitok Thanks. It works! Can you write it up as an answer and provide a link/reference to explain how to use the admin :// cmd (if you can)? I will accept it thereafter. Thanks again. – Sun Bear Nov 23 '19 at 17:35
  • 1
    Actually @vanadium improved on my initial idea and also provided a script. Apart from a minor formatting edit I have nothing of significance to add to his answer. Feel free to accept his answer :-) – Lienhart Woitok Nov 23 '19 at 19:10

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
  • 88,010
  • 1
    Bummer.... ;( Btw, can you provide a reference/hyperlink to elaborate on the admin:// command in you answer? I can't find it. – Sun Bear Nov 23 '19 at 15:33
  • @SunBear, didn't have more success than you did ;) – vanadium Nov 23 '19 at 15:42
  • Thanks. Let me wait a few days. If no better answer, I will accept yours. ;) Really hoping there could be a shorter way of doing what I asked. – Sun Bear Nov 23 '19 at 17:21
  • I also hope a better one pops up, but I doubt it ;) – vanadium Nov 23 '19 at 17:41
  • I came up with a one-line script that could it have your way: "sedit ubuntu.css". Just put such script in your path and you are all set. Remains a workaround, though. You cannot specify relative admin:// URI's as I said from the start. – vanadium Nov 23 '19 at 18:09
  • $(pwd) appears to be the relative path symbol. If so, I think it is apt not to say "it is not possible to pass a relative path with this admin:// URI". Rather, your answer should say it is possible. – Sun Bear Nov 25 '19 at 07:24
  • That is not correct. pwd prints the absolute path of the current directory. – vanadium Nov 25 '19 at 09:10
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

  • None of the sudo commands are valid to launch graphical applications as root. May not work in Wayland, moreover. Nowadays there is a move to not run any graphical programs as root. Opening with the admin:// URI ensures that the application itself does not have to run with root privileges. I think the script I presented is then the simplest approach to work according to current standards in this respect. On the other hand, installing nautilus-admin may be a valid suggestion depending on how that one works. – vanadium Nov 23 '19 at 18:18
  • @vanadium I'm on 16.04 and admin// is not supported there. In 2021 when 16.04 dies I'll probably convert to that method but in the mean time I like answers that work for all LTS versions. Many answers in AU sudo -H is recommended. – WinEunuuchs2Unix Nov 23 '19 at 18:23
  • "answers that work for all LTS versions" is very relative because it still depends on the user's configuration. "sudo -H" will not work on Wayland, for one thing. Moreover, this question relates to using admin:// and shortcutting the URL when you are in the directory where the file resides. Finally, it is not only about something working, but also about "best practices", and thus I would feel it is better not continue to steer to the old approaches that "work" but not necessarily are best practice. – vanadium Nov 24 '19 at 10:08