Editing system files in Linux (as root) with GUI and CLI text editors #2 (final)
Self-review



comments



Comments are very important to future readers, they speed up the comprehension of the whole code. I think I messed up at least one comment, (others pending review):


original:

# let's take a closer look at the first argument, the editor

suggested:

# store the first argument, the editor name







combine what can be combined

By combining simple pieces of code, we make it easier to read.


original:

# store an editor alias, if there is any
editor_alias=$( alias "$editor_name" 2&gt; /dev/null )

# remove that alias for now
if [ -n "$editor_alias" ]; then
    unalias "$editor_name"
fi

suggested:

# store an editor alias; and if there is any, remove it for now
if editor_alias=$( alias "$editor_name" 2&gt; /dev/null ); then
    unalias "$editor_name"
fi





implement code workaround into the function

My previous solution does not do any checks for code and also, by doing this we get rid of that alien alias.


original:

# VS Code specific workaround to work under root
alias sucode="sudo mkdir -p /root/.vscode &amp;&amp; sudo code -w --user-data-dir=/root/.vscode"

suggested:

# run the editor with one-time SUDO_EDITOR set-up
if [ "$editor_name" = code ]; then
    # code specific workaround
    sudo mkdir -p /root/.vscode &amp;&amp;
    sudo code -w --user-data-dir=/root/.vscode "$@"
else
    # main command generic
    SUDO_EDITOR="$editor_path $wait_option" sudoedit "$@"
fi





avoid generating editors aliases for which editor is not installed

My previous solution is generating all editor aliases, no matter if such program is installed on the system, this could have been unpleasant to users.


original:

for cli_editor in $sudoedit__cli_editor_list; do
    alias su$cli_editor="sudoedit_enhanced_run $cli_editor ''"
done
for gui_editor in $sudoedit__gui_editor_list; do
    alias su$gui_editor="sudoedit_enhanced_run $gui_editor -w"
done

suggested:

for cli_editor in $sudoedit__cli_editor_list; do
    if command -v "$cli_editor" &gt; /dev/null 2&gt;&amp;1; then
        alias su$cli_editor="sudoedit_enhanced_run $cli_editor ''"
    fi
done
for gui_editor in $sudoedit__gui_editor_list; do
    if command -v "$gui_editor" &gt; /dev/null 2&gt;&amp;1; then
        alias su$gui_editor="sudoedit_enhanced_run $gui_editor -w"
    fi
done