I use a script called sgedit
which inherits user preferences for font, tabs, and extensions. It uses sudo -H gedit
instead of gksu gedit
for stability in GUI environment. It prompts for a password.
Have sudo
inherit your user account gedit
settings

In this example the user settings for font name, font size, tab stops, convert tabs to spaces, 80 column highlight, and right side thumbnail slider bar have been inherited by sudo
.
With regular sudo -H gedit
you cannot make nor save these configuration settings. With the script below sgedit
the settings are inherited from your user account.
This script also addresses the "gksu
is bad and not installed by default" and "pkexec
is hard to setup" problems.
Background
I've been nagged by the same issue for years. This weekend's project was to write the sgedit
script:
- Call using
sgedit filename1 filename2...
- Gets user's gedit settings for tab stops, fonts, line-wrap, etc.
- Elevates to
sudo -H
to preserve file ownership whilst getting root powers.
- Requests password if last sudo has timed out.
- Gets sudo's gedit settings
- Compares differences between user and sudo gedit settings
- Runs
gsettings set
on the differences only (reduces 174 set
commands to a dozen or less. Next time it's run perhaps only one or two changes but often times none.
- Calls gedit as a background task such that terminal prompt reappears immediately.
Bash script sgedit
#!/bin/bash
NAME: sgedit
PATH: /mnt/e/bin
DESC: Run gedit as sudo using $USER preferences
DATE: June 17, 2018.
Must not prefix with sudo when calling script
if [[ $(id -u) == 0 ]]; then
zenity --error --text "You cannot call this script using sudo. Aborting."
exit 99
fi
Get user preferences before elevating to sudo
gsettings list-recursively | grep -i gedit | grep -v history |
grep -v docinfo |
grep -v virtual-root | grep -v state.window > /tmp/gedit.gsettings
sudoFunc () {
# Must be running as sudo
if [[ $(id -u) != 0 ]]; then
zenity --error --text "Sudo password authentication failed. Aborting."
exit 99
fi
# Get sudo's gedit preferences
gsettings list-recursively | grep -i gedit | grep -v history |
grep -v docinfo |
grep -v virtual-root | grep -v state.window > /tmp/gedit.gsettings.root
diff /tmp/gedit.gsettings.root /tmp/gedit.gsettings | grep '>' > /tmp/gedit.gsettings.diff
sed -i 's/>/gsettings set/g; s/uint32 //g' /tmp/gedit.gsettings.diff
chmod +x /tmp/gedit.gsettings.diff
bash -x /tmp/gedit.gsettings.diff # Display override setting to terminal
nohup gedit $@ &>/dev/null &
}
FUNC=$(declare -f sudoFunc)
sudo -H bash -c "$FUNC; sudoFunc $*;"
Housekeeping
Copy the bash script above to a new file called sgedit
. I recommend placing it in your $HOME/bin
directory, i.e. /home/YOURNAME/bin
. You may have to create the directory first.
Mark the file as executable using:
chmod a+x ~/sgedit
Note ~
is a shortcut for /home/YOURNAME
.
pkexec
work for you instead ofgksu
? – Thomas Ward May 31 '18 at 15:31pkexec
. (you have to hunt for it) – Thomas Ward May 31 '18 at 15:40sudo -H GUI-program
and if Wayland there is this link, that might be useful, ... there are workarounds, if you have a GUI tool, that works well for you and needs elevated permissions. – sudodus May 31 '18 at 15:47As for wayland, not allowing synaptic or gparted guis is ridiculous from the point of view of an ordinary home desktop user. They are invaluable. Systems can be so secure that no one wants to use them - there's a balance, and in this particular case (wayland) the user must be right.
– pastim Jun 01 '18 at 07:38I also found http://www.webupd8.org/2015/03/how-to-run-gedit-and-nautilus-as-root.html which provides polkits for gedit and nautilus as root should I need them.
– pastim Jun 01 '18 at 13:23sudo deja-dup --backup
? That starts the gui. – Organic Marble Jun 01 '18 at 20:09