Skip to content
Safe text files editing as root in Linux (all POSIX shells). Requirement: sudo.
Shell
Branch: master
Clone or download

Latest commit

Fetching latest commit…
Cannot retrieve the latest commit at this time.

Files

Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
LICENSE
README.md
sudoedit-enhanced

README.md

Safe text files editing as root in Linux

This topic should interest all Linux administrators as it is them who do the changes. Be it a home user, or a corporate admin. Without a doubt, no one wants to cripple their system just because power went down, or an ssh session was interrupted by a bad connection, etcetera.

What's more, I enable you by using this script to edit files comfortably even from a GUI editor of your choice.


POSIX shells compatibility

This script enables you to edit text files on Linux as root safely through sudoedit.

Internal workings are that sudoedit copies the file into a temporary file, and overwrites the original file if, and only if, that file has successfuly been changed (saved) and the text editor properly exited.

It is a standard POSIX shell script, it should work in any Linux distribution; more precisely, your shell.


Requirements

The only requirement is to have sudo properly installed and configured. If you have that, then sudoedit is available automatically as it is directly sudo's edit feature.


Usage instructions

Download

Visit the latest release download page. Directly download the file named sudoedit-enhanced in that release. Note, that there is no need to adjust name or permissions, but you are free to do so if you wish.

General

Once downloaded, place the script somewhere it can stay for good.

Before use

Please, customize the editors lists to your preference before actually using this script, by default there are these specified at the beginning of the script:

sudoedit__cli_editor_list='nano vi'
sudoedit__gui_editor_list='gedit emacs xed subl code'

You don't have to explicitly remove those not present in your system, as the script checks on existence of the editors upon every call (more precisely upon every sourcing to your shell's environment).


Integration into your shell's environment

The integration will vary greatly on what shell you use. As there are many shells in general use, I only provide guidance for bash.

bash

There are multiple ways to source my script to your bash, of course.

My personal recommendation is to create (if not yet existing) the ~/.bash_aliases file and source my script from there using the dot (.):

. /full/path/to/sudoedit-enhanced

Sourcing using the dot (.) is a POSIX way of doing so. In bash, however, there is also the builtin command source, you can use it if you want.

Afterward, you need to make sure that these (or similar) lines are present and not commented out in your ~/.bashrc:

if [ -f ~/.bash_aliases ]; then
    . ~/.bash_aliases
fi

Generated aliases

On every machine, there will always be different number of aliases.

As said before, the script checks on existence of each editor. This is true also for alias generators.

These aliases begin with su + name of the editor.


Examples of an actual use

sunano /etc/default/grub

Disclaimer: There is absolutely no affiliation with Nano editor team, but I personaly like this command-line editor.

GRUB config file safe editing with Nano CLI editor


susubl /etc/default/grub

Disclaimer: There is absolutely no affiliation with Sublime-Text editor team, but I personaly like this graphical-interface editor.

GRUB config file safe editing with Sublime-Text GUI editor


Reporting bugs and suggestions

Please open a new issue ticket.

You can’t perform that action at this time.