How can I run sudo gedit as gksu gedit

5,162

Solution 1

Open .bashrc file
gedit ~/.bashrc

Add following alias
alias sudo='gksu'

Now restart terminal to start using the alias.

Only add alias gedit='gksu gedit' if you want gksu only for gedit.
echo 'alias gedit='gksu gedit' >> ~/.bashrc

Solution 2

You could make a smart function that will transform automatically sudo to gksu when the command following it is in a set of pre-programmed functions like so: in your .bashrc add:

sudo_to_gksu=( "gedit" "nautilus" )
sudo() {
    local sudo=$(which sudo)
    local f
    for f in "${sudo_to_gksu[@]}"; do
        if [[ $1 = $f ]]; then
            sudo=gksu
            break
        fi
    done
    "$sudo" "$@"
}

So when you type sudo followed by gedit or nautilus, it will automatically change the sudo to gksu, and otherwise it will leave the sudo. Add some more programs in the array sudo_to_gksu if you wish. You can also extend this to also have gksudo instead of gksu if you wish.

This might have some side effects... let me know if it's the case!

Hope this helps!

Share:
5,162

Related videos on Youtube

SimplySimon
Author by

SimplySimon

Personal details Ex-Lorry Driver, Singer, Poet, Artist, Photographer, Caravanner, programmer, Walker, Reader, Techie, Website Hobbyist & Dad September 2014 saw my beautiful wife and I, selling our house and taking to the roads in a caravan for 4.5 years to 'do Europe' 47 countries accessible by caravan.

Updated on September 18, 2022

Comments

  • SimplySimon
    SimplySimon over 1 year

    I'm looking into ways of automatically loading Gedit as gksu gedit when I enter sudo gedit by mistake?

    I have found that I have made a number of files unreachable by using gedit and I have only just found out why!

    ACHIEVED SO FAR

    I have written a script which will make an alias so that if I type in sudo <application> it can automatically convert that to gksu <application> but I want to make this alias stick, so that I don't have to run the script every time I boot the computer.

    Is there a config file I can edit or should I run this script as a start up script (which would be inconvenient!)?

  • SimplySimon
    SimplySimon almost 11 years
    Brilliant, I just found the same answer after spending days searching in the wrong place :)
  • totti
    totti almost 11 years
    Add alias gedit='gksu gedit' if u want gksu only for gedit. (Remove the earlier)
  • SimplySimon
    SimplySimon almost 11 years
    it's good for sudo nautilus, sudo dolphin as well so I think I'll leave the original alias :)
  • SimplySimon
    SimplySimon almost 11 years
    I have updated to alias gedit='gksu gedit' as I had problems with sudo apt-get autoremove not completing
  • SimplySimon
    SimplySimon almost 11 years
    I'll try this out, it looks very flexible. Thank you