This topic might interest all Linux (wiki) 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 the power went down, or an ssh
(man page) session was interrupted by a bad connection, etcetera.
What's more, by using this script I enable you to edit files comfortably even from a somewhat limited list of GUI (wiki) editors.
For a quick look, I made a short SVG demo for you, it takes less than two and a half minutes.
It was loading unbearably slow on this Readme, so if you want to see it, click SVG demo and play it separately.
I will not update the demo if there is some little change, however, it's merely to show you an actual work.
This script enables you to edit text files on Linux as root
(wiki) 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 (wiki) shell script (wiki), it should work in any Linux (wiki) distribution; more precisely, your shell (wiki).
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, it can even be invoked as sudo -e
or sudo --edit
.
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.
Once downloaded, place the script somewhere it can stay for good. Be it ~/bin
or somewhere else completely.
You may edit the beginning of this script to customize the editor lists to your preference before actually using this script, by default there are these specified at the beginning of the script, for sure you can safely add any CLI editor, but GUI (wiki) editors are somewhat problematic, which is why I have only been able to add Sublime-Text (home page) and Xed (GitHub page) so far:
conf__cli_editors='vi nano'
conf__gui_editors='subl xed'
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 (wiki) environment).
Integration into your shell's environment
The integration will vary greatly on what shell you use. As there are many shells (wiki) in general use, I only provide guidance for bash
(wiki).
bash
(wiki)
There are multiple ways to source my script to your bash
(wiki), 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 (.
) notation:
. /full/path/to/sudoedit-enhanced
Sourcing using the dot (.
) is a POSIX (wiki) way of doing so. Although, in bash
(wiki), there is also the source
builtin command, so you can use that, 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
On every machine, there will always be different number of alias
es.
As said before, the script checks on existence of each editor. This is true also for alias
generators.
These alias
es begin with su
+ name of the editor.
To get the current list of generated editor alias
es, just run sudoedit_run
without any argument, example:
$ sudoedit_run
Usage example: sueditor /path/to/file1 /path/to/file2
sudoedit-enhanced defined the following editors:
suvi sunano susubl suxed
CLI (wiki) editors - screenshots (click to enlarge)
Nano editor (home page) | Vi editor (home page) |
---|---|
How to get these editors:
-
The Nano editor (home page) can be downloaded from this page, but note you would have to compile the program yourself, which is what I am doing. It may be much more comfortable just to use the packaged version.
-
The Vi editor (home page) can be downloaded from this page, I personally prefer the packaged version myself to compiling it.
GUI (wiki) editors - screenshots (click to enlarge)
Sublime-Text editor (home page) | Xed Mint editor (home page) |
---|---|
How to get these editors:
-
The Sublime-Text editor (home page) can be downloaded from its home page/download;
-
The Xed Mint editor is pre-installed in Linux Mint (home page) and on some other Ubuntu-based distributions (wiki) can be installed via PPA (wiki):
ppa:savoury1/xapps
(Launchpad link).
Please open a new issue ticket (direct link) or you can also mail me at info@vlastimilburian.cz (email link).
Donations are possible via my PayPal account issued on the same email address as mentioned above.