Editing system files in Linux (as root) with GUI and CLI text editorsOptimize text search in files with...

Is there a way to make member function NOT callable from constructor?

Why is the design of haulage companies so “special”?

How did the USSR manage to innovate in an environment characterized by government censorship and high bureaucracy?

aging parents with no investments

Are objects structures and/or vice versa?

Why doesn't a const reference extend the life of a temporary object passed via a function?

A poker game description that does not feel gimmicky

How can I fix this gap between bookcases I made?

Why do we use polarized capacitors?

How to answer pointed "are you quitting" questioning when I don't want them to suspect

I see my dog run

Is this food a bread or a loaf?

Can I find out the caloric content of bread by dehydrating it?

What is GPS' 19 year rollover and does it present a cybersecurity issue?

Can I legally use front facing blue light in the UK?

Email Account under attack (really) - anything I can do?

Why was the "bread communication" in the arena of Catching Fire left out in the movie?

COUNT(*) or MAX(id) - which is faster?

What is the meaning of "of trouble" in the following sentence?

Is there a familial term for apples and pears?

Could a US political party gain complete control over the government by removing checks & balances?

Could Giant Ground Sloths have been a good pack animal for the ancient Mayans?

Was there ever an axiom rendered a theorem?

What does it exactly mean if a random variable follows a distribution



Editing system files in Linux (as root) with GUI and CLI text editors


Optimize text search in files with BashCommand-Line tool to find large files on a Linux systemNASM tutorial (Linux syscalls, 64 bit) code with CI-build and testBackup document root and MySQL in LinuxJava Swing GUI editor with tools and scriptsmalloc() and free() for Linux with system callsReporting new USB storage devices on Linux and macOS with Python






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}







2












$begingroup$


My intention is to POSIX-ly write one generalized function for running various text editors I use for different purposes through sudoedit, i.e. editing files as root safely. Safely = for instance, if a power loss occurs during the file edit; another example could be lost SSH connection, etc.





Originally, I had these Bash functions defined for this purpose in my .bash_aliases file:



function sucode
{
export SUDO_EDITOR='/usr/bin/code --wait'
sudoedit "$@"
}
function susubl
{
export SUDO_EDITOR='/opt/sublime_text/sublime_text --wait'
sudoedit "$@"
}
function suxed
{
export SUDO_EDITOR='/usr/bin/xed --wait'
sudoedit "$@"
}




Since yesterday, I'm trying to generalize that solution for other Linux users to be able to take advantage of it. Momentary peek:



# Text editing as root; The proper way through `sudoedit`.
sudoedit_internal()
{
[ "${#}" -lt 3 ] && { printf '%sn' 'sudoedit_internal(): Invalid number of arguments.' 1>&2; return; }
editor_path=$( command -v "${1}" )
[ -x "${editor_path}" ] || { printf '%sn' "sudoedit_internal(): The editor path ${editor_path} does not exist on this system." 1>&2; return; }
editor_wait_option=${2}
shift 2
env SUDO_EDITOR="${editor_path} ${editor_wait_option}" sudoedit "${@}"
}
# CLI
suvi() { sudoedit_internal vi '' "${@}"; }
sunano() { sudoedit_internal nano '' "${@}"; }
# GUI
sucode() { sudoedit_internal code -w "${@}"; }
susubl() { sudoedit_internal subl -w "${@}"; }
suxed() { sudoedit_internal xed -w "${@}"; }


These 5 editors I use. Please take that as an example only.



As I should not update this question any further, you can find the up-to-date version of this script snippet in my Unix & Linux Q&A.










share|improve this question











$endgroup$



















    2












    $begingroup$


    My intention is to POSIX-ly write one generalized function for running various text editors I use for different purposes through sudoedit, i.e. editing files as root safely. Safely = for instance, if a power loss occurs during the file edit; another example could be lost SSH connection, etc.





    Originally, I had these Bash functions defined for this purpose in my .bash_aliases file:



    function sucode
    {
    export SUDO_EDITOR='/usr/bin/code --wait'
    sudoedit "$@"
    }
    function susubl
    {
    export SUDO_EDITOR='/opt/sublime_text/sublime_text --wait'
    sudoedit "$@"
    }
    function suxed
    {
    export SUDO_EDITOR='/usr/bin/xed --wait'
    sudoedit "$@"
    }




    Since yesterday, I'm trying to generalize that solution for other Linux users to be able to take advantage of it. Momentary peek:



    # Text editing as root; The proper way through `sudoedit`.
    sudoedit_internal()
    {
    [ "${#}" -lt 3 ] && { printf '%sn' 'sudoedit_internal(): Invalid number of arguments.' 1>&2; return; }
    editor_path=$( command -v "${1}" )
    [ -x "${editor_path}" ] || { printf '%sn' "sudoedit_internal(): The editor path ${editor_path} does not exist on this system." 1>&2; return; }
    editor_wait_option=${2}
    shift 2
    env SUDO_EDITOR="${editor_path} ${editor_wait_option}" sudoedit "${@}"
    }
    # CLI
    suvi() { sudoedit_internal vi '' "${@}"; }
    sunano() { sudoedit_internal nano '' "${@}"; }
    # GUI
    sucode() { sudoedit_internal code -w "${@}"; }
    susubl() { sudoedit_internal subl -w "${@}"; }
    suxed() { sudoedit_internal xed -w "${@}"; }


    These 5 editors I use. Please take that as an example only.



    As I should not update this question any further, you can find the up-to-date version of this script snippet in my Unix & Linux Q&A.










    share|improve this question











    $endgroup$















      2












      2








      2





      $begingroup$


      My intention is to POSIX-ly write one generalized function for running various text editors I use for different purposes through sudoedit, i.e. editing files as root safely. Safely = for instance, if a power loss occurs during the file edit; another example could be lost SSH connection, etc.





      Originally, I had these Bash functions defined for this purpose in my .bash_aliases file:



      function sucode
      {
      export SUDO_EDITOR='/usr/bin/code --wait'
      sudoedit "$@"
      }
      function susubl
      {
      export SUDO_EDITOR='/opt/sublime_text/sublime_text --wait'
      sudoedit "$@"
      }
      function suxed
      {
      export SUDO_EDITOR='/usr/bin/xed --wait'
      sudoedit "$@"
      }




      Since yesterday, I'm trying to generalize that solution for other Linux users to be able to take advantage of it. Momentary peek:



      # Text editing as root; The proper way through `sudoedit`.
      sudoedit_internal()
      {
      [ "${#}" -lt 3 ] && { printf '%sn' 'sudoedit_internal(): Invalid number of arguments.' 1>&2; return; }
      editor_path=$( command -v "${1}" )
      [ -x "${editor_path}" ] || { printf '%sn' "sudoedit_internal(): The editor path ${editor_path} does not exist on this system." 1>&2; return; }
      editor_wait_option=${2}
      shift 2
      env SUDO_EDITOR="${editor_path} ${editor_wait_option}" sudoedit "${@}"
      }
      # CLI
      suvi() { sudoedit_internal vi '' "${@}"; }
      sunano() { sudoedit_internal nano '' "${@}"; }
      # GUI
      sucode() { sudoedit_internal code -w "${@}"; }
      susubl() { sudoedit_internal subl -w "${@}"; }
      suxed() { sudoedit_internal xed -w "${@}"; }


      These 5 editors I use. Please take that as an example only.



      As I should not update this question any further, you can find the up-to-date version of this script snippet in my Unix & Linux Q&A.










      share|improve this question











      $endgroup$




      My intention is to POSIX-ly write one generalized function for running various text editors I use for different purposes through sudoedit, i.e. editing files as root safely. Safely = for instance, if a power loss occurs during the file edit; another example could be lost SSH connection, etc.





      Originally, I had these Bash functions defined for this purpose in my .bash_aliases file:



      function sucode
      {
      export SUDO_EDITOR='/usr/bin/code --wait'
      sudoedit "$@"
      }
      function susubl
      {
      export SUDO_EDITOR='/opt/sublime_text/sublime_text --wait'
      sudoedit "$@"
      }
      function suxed
      {
      export SUDO_EDITOR='/usr/bin/xed --wait'
      sudoedit "$@"
      }




      Since yesterday, I'm trying to generalize that solution for other Linux users to be able to take advantage of it. Momentary peek:



      # Text editing as root; The proper way through `sudoedit`.
      sudoedit_internal()
      {
      [ "${#}" -lt 3 ] && { printf '%sn' 'sudoedit_internal(): Invalid number of arguments.' 1>&2; return; }
      editor_path=$( command -v "${1}" )
      [ -x "${editor_path}" ] || { printf '%sn' "sudoedit_internal(): The editor path ${editor_path} does not exist on this system." 1>&2; return; }
      editor_wait_option=${2}
      shift 2
      env SUDO_EDITOR="${editor_path} ${editor_wait_option}" sudoedit "${@}"
      }
      # CLI
      suvi() { sudoedit_internal vi '' "${@}"; }
      sunano() { sudoedit_internal nano '' "${@}"; }
      # GUI
      sucode() { sudoedit_internal code -w "${@}"; }
      susubl() { sudoedit_internal subl -w "${@}"; }
      suxed() { sudoedit_internal xed -w "${@}"; }


      These 5 editors I use. Please take that as an example only.



      As I should not update this question any further, you can find the up-to-date version of this script snippet in my Unix & Linux Q&A.







      linux posix sh text-editor






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 23 hours ago









      Jamal

      30.6k11121227




      30.6k11121227










      asked yesterday









      VlastimilVlastimil

      653318




      653318






















          1 Answer
          1






          active

          oldest

          votes


















          3












          $begingroup$

          It's good form to return non-zero on error. The not-optional option is a little ugly and an environment variable may work better.



          Some extraneous syntax can go:





          • 1 before >&2 is implied


          • {} around unsubstituted dereferences doesn't add anything


          • echo is an alias for printf "%sn"

          • testing for error instead of success allows test && echo && return without braces


          • command -v tests validity for you; no need to test again

          • you've moved the complexity into a function already; reward yourself by using aliases to invoke it


          sudoedit_internal()
          {
          [ $# -lt 2 ] && echo "sudoedit_internal(): Invalid number of arguments." >&2 && return 1
          ! command -v "$1" >/dev/null && echo "sudoedit_internal(): The editor $1 does not exist on this system." >&2 && return 1
          editor="$1"; shift
          SUDO_EDITOR="$editor $opt" sudoedit "$@"
          }

          for ed in vi nano ; do alias su$ed="opt= sudoedit_internal $ed"; done
          for ed in code subl xed ; do alias su$ed="opt=-w sudoedit_internal $ed"; done





          share|improve this answer











          $endgroup$













          • $begingroup$
            If I don't accept in 2 days, please comment as a reminder. I'm busy at the moment. Thank you.
            $endgroup$
            – Vlastimil
            17 hours ago






          • 1




            $begingroup$
            I love the way aliases are generated. Big thanks! The previous comment still applies, I am just peeking.
            $endgroup$
            – Vlastimil
            15 hours ago














          Your Answer





          StackExchange.ifUsing("editor", function () {
          return StackExchange.using("mathjaxEditing", function () {
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          });
          });
          }, "mathjax-editing");

          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "196"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f217005%2fediting-system-files-in-linux-as-root-with-gui-and-cli-text-editors%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          3












          $begingroup$

          It's good form to return non-zero on error. The not-optional option is a little ugly and an environment variable may work better.



          Some extraneous syntax can go:





          • 1 before >&2 is implied


          • {} around unsubstituted dereferences doesn't add anything


          • echo is an alias for printf "%sn"

          • testing for error instead of success allows test && echo && return without braces


          • command -v tests validity for you; no need to test again

          • you've moved the complexity into a function already; reward yourself by using aliases to invoke it


          sudoedit_internal()
          {
          [ $# -lt 2 ] && echo "sudoedit_internal(): Invalid number of arguments." >&2 && return 1
          ! command -v "$1" >/dev/null && echo "sudoedit_internal(): The editor $1 does not exist on this system." >&2 && return 1
          editor="$1"; shift
          SUDO_EDITOR="$editor $opt" sudoedit "$@"
          }

          for ed in vi nano ; do alias su$ed="opt= sudoedit_internal $ed"; done
          for ed in code subl xed ; do alias su$ed="opt=-w sudoedit_internal $ed"; done





          share|improve this answer











          $endgroup$













          • $begingroup$
            If I don't accept in 2 days, please comment as a reminder. I'm busy at the moment. Thank you.
            $endgroup$
            – Vlastimil
            17 hours ago






          • 1




            $begingroup$
            I love the way aliases are generated. Big thanks! The previous comment still applies, I am just peeking.
            $endgroup$
            – Vlastimil
            15 hours ago


















          3












          $begingroup$

          It's good form to return non-zero on error. The not-optional option is a little ugly and an environment variable may work better.



          Some extraneous syntax can go:





          • 1 before >&2 is implied


          • {} around unsubstituted dereferences doesn't add anything


          • echo is an alias for printf "%sn"

          • testing for error instead of success allows test && echo && return without braces


          • command -v tests validity for you; no need to test again

          • you've moved the complexity into a function already; reward yourself by using aliases to invoke it


          sudoedit_internal()
          {
          [ $# -lt 2 ] && echo "sudoedit_internal(): Invalid number of arguments." >&2 && return 1
          ! command -v "$1" >/dev/null && echo "sudoedit_internal(): The editor $1 does not exist on this system." >&2 && return 1
          editor="$1"; shift
          SUDO_EDITOR="$editor $opt" sudoedit "$@"
          }

          for ed in vi nano ; do alias su$ed="opt= sudoedit_internal $ed"; done
          for ed in code subl xed ; do alias su$ed="opt=-w sudoedit_internal $ed"; done





          share|improve this answer











          $endgroup$













          • $begingroup$
            If I don't accept in 2 days, please comment as a reminder. I'm busy at the moment. Thank you.
            $endgroup$
            – Vlastimil
            17 hours ago






          • 1




            $begingroup$
            I love the way aliases are generated. Big thanks! The previous comment still applies, I am just peeking.
            $endgroup$
            – Vlastimil
            15 hours ago
















          3












          3








          3





          $begingroup$

          It's good form to return non-zero on error. The not-optional option is a little ugly and an environment variable may work better.



          Some extraneous syntax can go:





          • 1 before >&2 is implied


          • {} around unsubstituted dereferences doesn't add anything


          • echo is an alias for printf "%sn"

          • testing for error instead of success allows test && echo && return without braces


          • command -v tests validity for you; no need to test again

          • you've moved the complexity into a function already; reward yourself by using aliases to invoke it


          sudoedit_internal()
          {
          [ $# -lt 2 ] && echo "sudoedit_internal(): Invalid number of arguments." >&2 && return 1
          ! command -v "$1" >/dev/null && echo "sudoedit_internal(): The editor $1 does not exist on this system." >&2 && return 1
          editor="$1"; shift
          SUDO_EDITOR="$editor $opt" sudoedit "$@"
          }

          for ed in vi nano ; do alias su$ed="opt= sudoedit_internal $ed"; done
          for ed in code subl xed ; do alias su$ed="opt=-w sudoedit_internal $ed"; done





          share|improve this answer











          $endgroup$



          It's good form to return non-zero on error. The not-optional option is a little ugly and an environment variable may work better.



          Some extraneous syntax can go:





          • 1 before >&2 is implied


          • {} around unsubstituted dereferences doesn't add anything


          • echo is an alias for printf "%sn"

          • testing for error instead of success allows test && echo && return without braces


          • command -v tests validity for you; no need to test again

          • you've moved the complexity into a function already; reward yourself by using aliases to invoke it


          sudoedit_internal()
          {
          [ $# -lt 2 ] && echo "sudoedit_internal(): Invalid number of arguments." >&2 && return 1
          ! command -v "$1" >/dev/null && echo "sudoedit_internal(): The editor $1 does not exist on this system." >&2 && return 1
          editor="$1"; shift
          SUDO_EDITOR="$editor $opt" sudoedit "$@"
          }

          for ed in vi nano ; do alias su$ed="opt= sudoedit_internal $ed"; done
          for ed in code subl xed ; do alias su$ed="opt=-w sudoedit_internal $ed"; done






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 6 hours ago

























          answered 20 hours ago









          Oh My GoodnessOh My Goodness

          2,242315




          2,242315












          • $begingroup$
            If I don't accept in 2 days, please comment as a reminder. I'm busy at the moment. Thank you.
            $endgroup$
            – Vlastimil
            17 hours ago






          • 1




            $begingroup$
            I love the way aliases are generated. Big thanks! The previous comment still applies, I am just peeking.
            $endgroup$
            – Vlastimil
            15 hours ago




















          • $begingroup$
            If I don't accept in 2 days, please comment as a reminder. I'm busy at the moment. Thank you.
            $endgroup$
            – Vlastimil
            17 hours ago






          • 1




            $begingroup$
            I love the way aliases are generated. Big thanks! The previous comment still applies, I am just peeking.
            $endgroup$
            – Vlastimil
            15 hours ago


















          $begingroup$
          If I don't accept in 2 days, please comment as a reminder. I'm busy at the moment. Thank you.
          $endgroup$
          – Vlastimil
          17 hours ago




          $begingroup$
          If I don't accept in 2 days, please comment as a reminder. I'm busy at the moment. Thank you.
          $endgroup$
          – Vlastimil
          17 hours ago




          1




          1




          $begingroup$
          I love the way aliases are generated. Big thanks! The previous comment still applies, I am just peeking.
          $endgroup$
          – Vlastimil
          15 hours ago






          $begingroup$
          I love the way aliases are generated. Big thanks! The previous comment still applies, I am just peeking.
          $endgroup$
          – Vlastimil
          15 hours ago




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Code Review Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          Use MathJax to format equations. MathJax reference.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f217005%2fediting-system-files-in-linux-as-root-with-gui-and-cli-text-editors%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          is 'sed' thread safeWhat should someone know about using Python scripts in the shell?Nexenta bash script uses...

          How do i solve the “ No module named 'mlxtend' ” issue on Jupyter?

          Pilgersdorf Inhaltsverzeichnis Geografie | Geschichte | Bevölkerungsentwicklung | Politik | Kultur...