How to remove ^@ character returned by `system()`?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
2
down vote

favorite












When I call this function inside vim statusline it returns this nvim^@.weird character, What is the meaning of ^@ in returned value and how to get rid of it?



function! GetEditor()
let s:name = system("echo $EDITOR")
return s:name
endfunction

set statusline+=%0* %GetEditor()









share|improve this question

























    up vote
    2
    down vote

    favorite












    When I call this function inside vim statusline it returns this nvim^@.weird character, What is the meaning of ^@ in returned value and how to get rid of it?



    function! GetEditor()
    let s:name = system("echo $EDITOR")
    return s:name
    endfunction

    set statusline+=%0* %GetEditor()









    share|improve this question























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      When I call this function inside vim statusline it returns this nvim^@.weird character, What is the meaning of ^@ in returned value and how to get rid of it?



      function! GetEditor()
      let s:name = system("echo $EDITOR")
      return s:name
      endfunction

      set statusline+=%0* %GetEditor()









      share|improve this question













      When I call this function inside vim statusline it returns this nvim^@.weird character, What is the meaning of ^@ in returned value and how to get rid of it?



      function! GetEditor()
      let s:name = system("echo $EDITOR")
      return s:name
      endfunction

      set statusline+=%0* %GetEditor()






      vimscript statusline






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 4 hours ago









      John Fred Fadrigalan

      326




      326




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          3
          down vote



          accepted










          The ^@ is a NUL character, 0x00, which Vim uses in certain cases to store a newline, 0x0a.



          Unix commands usually terminate their output with a newline, and the system() function captures that newline in the output of your echo command.



          To get rid of that terminating newline, use substitute():



          let s:name = substitute(system("echo $EDITOR"), 'n', '', 'g')


          That substitute() call replaces newlines ('n') in the output of system() with nothing (''), and does so globally ('g'), i.e., to all newlines in the output of system().






          share|improve this answer




















          • Thank you so much! It works now..
            – John Fred Fadrigalan
            2 hours ago

















          up vote
          2
          down vote













          You got an answer already. However, for your specific case, you don't need the system() call, since you want an environment variable to be expanded. In that case you could simply use echo $EDITOR or in case of a function call do:



          function! GetEditor()
          return $EDITOR
          endfunction



          Now, suppose you still need a system() function call, because you need to return the output of a system specific command. If you know, the output will only have a simple line (e.g. only one single linebreak), you could simply return the output with the last byte (the linebreak) removed:



          return system("yes |head -1")[:-2]


          which uses the expr syntax to strip the last byte from the output.




          Alternatively, you could make use of the systemlist() function which automatically converts the output of the system command into a vim list, so you do not have to take care of those trailing linebreaks:



          return systemlist("echo $EDITOR")



          See also the similar answer on superuser






          share|improve this answer




















            Your Answer







            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "599"
            ;
            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',
            convertImagesToLinks: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













             

            draft saved


            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fvi.stackexchange.com%2fquestions%2f17704%2fhow-to-remove-character-returned-by-system%23new-answer', 'question_page');

            );

            Post as a guest






























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            3
            down vote



            accepted










            The ^@ is a NUL character, 0x00, which Vim uses in certain cases to store a newline, 0x0a.



            Unix commands usually terminate their output with a newline, and the system() function captures that newline in the output of your echo command.



            To get rid of that terminating newline, use substitute():



            let s:name = substitute(system("echo $EDITOR"), 'n', '', 'g')


            That substitute() call replaces newlines ('n') in the output of system() with nothing (''), and does so globally ('g'), i.e., to all newlines in the output of system().






            share|improve this answer




















            • Thank you so much! It works now..
              – John Fred Fadrigalan
              2 hours ago














            up vote
            3
            down vote



            accepted










            The ^@ is a NUL character, 0x00, which Vim uses in certain cases to store a newline, 0x0a.



            Unix commands usually terminate their output with a newline, and the system() function captures that newline in the output of your echo command.



            To get rid of that terminating newline, use substitute():



            let s:name = substitute(system("echo $EDITOR"), 'n', '', 'g')


            That substitute() call replaces newlines ('n') in the output of system() with nothing (''), and does so globally ('g'), i.e., to all newlines in the output of system().






            share|improve this answer




















            • Thank you so much! It works now..
              – John Fred Fadrigalan
              2 hours ago












            up vote
            3
            down vote



            accepted







            up vote
            3
            down vote



            accepted






            The ^@ is a NUL character, 0x00, which Vim uses in certain cases to store a newline, 0x0a.



            Unix commands usually terminate their output with a newline, and the system() function captures that newline in the output of your echo command.



            To get rid of that terminating newline, use substitute():



            let s:name = substitute(system("echo $EDITOR"), 'n', '', 'g')


            That substitute() call replaces newlines ('n') in the output of system() with nothing (''), and does so globally ('g'), i.e., to all newlines in the output of system().






            share|improve this answer












            The ^@ is a NUL character, 0x00, which Vim uses in certain cases to store a newline, 0x0a.



            Unix commands usually terminate their output with a newline, and the system() function captures that newline in the output of your echo command.



            To get rid of that terminating newline, use substitute():



            let s:name = substitute(system("echo $EDITOR"), 'n', '', 'g')


            That substitute() call replaces newlines ('n') in the output of system() with nothing (''), and does so globally ('g'), i.e., to all newlines in the output of system().







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered 3 hours ago









            garyjohn

            4,8441015




            4,8441015











            • Thank you so much! It works now..
              – John Fred Fadrigalan
              2 hours ago
















            • Thank you so much! It works now..
              – John Fred Fadrigalan
              2 hours ago















            Thank you so much! It works now..
            – John Fred Fadrigalan
            2 hours ago




            Thank you so much! It works now..
            – John Fred Fadrigalan
            2 hours ago










            up vote
            2
            down vote













            You got an answer already. However, for your specific case, you don't need the system() call, since you want an environment variable to be expanded. In that case you could simply use echo $EDITOR or in case of a function call do:



            function! GetEditor()
            return $EDITOR
            endfunction



            Now, suppose you still need a system() function call, because you need to return the output of a system specific command. If you know, the output will only have a simple line (e.g. only one single linebreak), you could simply return the output with the last byte (the linebreak) removed:



            return system("yes |head -1")[:-2]


            which uses the expr syntax to strip the last byte from the output.




            Alternatively, you could make use of the systemlist() function which automatically converts the output of the system command into a vim list, so you do not have to take care of those trailing linebreaks:



            return systemlist("echo $EDITOR")



            See also the similar answer on superuser






            share|improve this answer
























              up vote
              2
              down vote













              You got an answer already. However, for your specific case, you don't need the system() call, since you want an environment variable to be expanded. In that case you could simply use echo $EDITOR or in case of a function call do:



              function! GetEditor()
              return $EDITOR
              endfunction



              Now, suppose you still need a system() function call, because you need to return the output of a system specific command. If you know, the output will only have a simple line (e.g. only one single linebreak), you could simply return the output with the last byte (the linebreak) removed:



              return system("yes |head -1")[:-2]


              which uses the expr syntax to strip the last byte from the output.




              Alternatively, you could make use of the systemlist() function which automatically converts the output of the system command into a vim list, so you do not have to take care of those trailing linebreaks:



              return systemlist("echo $EDITOR")



              See also the similar answer on superuser






              share|improve this answer






















                up vote
                2
                down vote










                up vote
                2
                down vote









                You got an answer already. However, for your specific case, you don't need the system() call, since you want an environment variable to be expanded. In that case you could simply use echo $EDITOR or in case of a function call do:



                function! GetEditor()
                return $EDITOR
                endfunction



                Now, suppose you still need a system() function call, because you need to return the output of a system specific command. If you know, the output will only have a simple line (e.g. only one single linebreak), you could simply return the output with the last byte (the linebreak) removed:



                return system("yes |head -1")[:-2]


                which uses the expr syntax to strip the last byte from the output.




                Alternatively, you could make use of the systemlist() function which automatically converts the output of the system command into a vim list, so you do not have to take care of those trailing linebreaks:



                return systemlist("echo $EDITOR")



                See also the similar answer on superuser






                share|improve this answer












                You got an answer already. However, for your specific case, you don't need the system() call, since you want an environment variable to be expanded. In that case you could simply use echo $EDITOR or in case of a function call do:



                function! GetEditor()
                return $EDITOR
                endfunction



                Now, suppose you still need a system() function call, because you need to return the output of a system specific command. If you know, the output will only have a simple line (e.g. only one single linebreak), you could simply return the output with the last byte (the linebreak) removed:



                return system("yes |head -1")[:-2]


                which uses the expr syntax to strip the last byte from the output.




                Alternatively, you could make use of the systemlist() function which automatically converts the output of the system command into a vim list, so you do not have to take care of those trailing linebreaks:



                return systemlist("echo $EDITOR")



                See also the similar answer on superuser







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 38 mins ago









                Christian Brabandt

                14.6k2443




                14.6k2443



























                     

                    draft saved


                    draft discarded















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fvi.stackexchange.com%2fquestions%2f17704%2fhow-to-remove-character-returned-by-system%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Comments

                    Popular posts from this blog

                    Long meetings (6-7 hours a day): Being “babysat” by supervisor

                    Is the Concept of Multiple Fantasy Races Scientifically Flawed? [closed]

                    Confectionery