Validate `~/.vimrc`

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












2















I want to validate that my ~/.vimrc file is correct, basically just by launching vim and immediately running quit. I can almost do this with vim -c quit, except that I have to press the enter key. For example,



> cat ~/.vimrc
echoerr 'test'

> vim -c quit
Error detected while processing /home/josh/.vimrc:
line 1:
test
Press ENTER or type command to continue


Is there a way to do this without pressing enter? I guess I could just run echo | vim -c quit, but I was hoping there was a more elegant solution.










share|improve this question




























    2















    I want to validate that my ~/.vimrc file is correct, basically just by launching vim and immediately running quit. I can almost do this with vim -c quit, except that I have to press the enter key. For example,



    > cat ~/.vimrc
    echoerr 'test'

    > vim -c quit
    Error detected while processing /home/josh/.vimrc:
    line 1:
    test
    Press ENTER or type command to continue


    Is there a way to do this without pressing enter? I guess I could just run echo | vim -c quit, but I was hoping there was a more elegant solution.










    share|improve this question


























      2












      2








      2








      I want to validate that my ~/.vimrc file is correct, basically just by launching vim and immediately running quit. I can almost do this with vim -c quit, except that I have to press the enter key. For example,



      > cat ~/.vimrc
      echoerr 'test'

      > vim -c quit
      Error detected while processing /home/josh/.vimrc:
      line 1:
      test
      Press ENTER or type command to continue


      Is there a way to do this without pressing enter? I guess I could just run echo | vim -c quit, but I was hoping there was a more elegant solution.










      share|improve this question
















      I want to validate that my ~/.vimrc file is correct, basically just by launching vim and immediately running quit. I can almost do this with vim -c quit, except that I have to press the enter key. For example,



      > cat ~/.vimrc
      echoerr 'test'

      > vim -c quit
      Error detected while processing /home/josh/.vimrc:
      line 1:
      test
      Press ENTER or type command to continue


      Is there a way to do this without pressing enter? I guess I could just run echo | vim -c quit, but I was hoping there was a more elegant solution.







      vim vimrc






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 21 at 10:24









      mosvy

      7,3191529




      7,3191529










      asked Jan 21 at 7:07









      Joshua SpenceJoshua Spence

      1434




      1434




















          1 Answer
          1






          active

          oldest

          votes


















          4














          You can use a try-catch block to catch errors (which get converted to exceptions), and fail on any exception:



          $ cat vimrc-fail 
          echoerr 'test'
          $ cat vimrc-pass
          echo 'test'
          $ vim -u NONE -c 'try | source vimrc-pass | catch | cq | endtry | q'; echo $?
          0
          $ vim -u NONE -c 'try | source vimrc-fail | catch | cq | endtry | q'; echo $?
          1


          From the help:



          :try :try :endt :endtry E600 E601 E602
          :endt[ry] Change the error handling for the commands between
          ":try" and ":endtry" including everything being
          executed across ":source" commands, function calls,
          or autocommand invocations.


          And from :echoerr:




          When used inside a try conditional, the message is raised as an error exception instead (see try-echoerr).




          Other notes:




          • -u NONE prevents sourcing the default vimrcs


          • :cq makes Vim return a non-zero exit status


          To get the last error message (now actually an exception), use v:exception:



          $ vim -u NONE -c 'try | source vimrc-fail | catch | silent exec "!echo " shellescape(v:exception) |cq | endtry | q'; echo $?
          Vim(echoerr):test
          1





          share|improve this answer

























          • Thanks! Is there a way to echo the error message as well?

            – Joshua Spence
            Jan 21 at 23:11











          • @JoshuaSpence I think the last exception is stored in v:exception. Maybe something like exec "!echo " shellescape(v:exception) | cq instead of the plain cq would work.

            – Olorin
            Jan 22 at 1:10












          • That almost works... it requires me to press enter though.

            – Joshua Spence
            Jan 22 at 3:56











          • Ok, it works if I run vim with -E and -s

            – Joshua Spence
            Jan 22 at 4:11











          • silent exec should fix the Enter problem.

            – Olorin
            Jan 22 at 4:37










          Your Answer








          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "106"
          ;
          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%2funix.stackexchange.com%2fquestions%2f495721%2fvalidate-vimrc%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









          4














          You can use a try-catch block to catch errors (which get converted to exceptions), and fail on any exception:



          $ cat vimrc-fail 
          echoerr 'test'
          $ cat vimrc-pass
          echo 'test'
          $ vim -u NONE -c 'try | source vimrc-pass | catch | cq | endtry | q'; echo $?
          0
          $ vim -u NONE -c 'try | source vimrc-fail | catch | cq | endtry | q'; echo $?
          1


          From the help:



          :try :try :endt :endtry E600 E601 E602
          :endt[ry] Change the error handling for the commands between
          ":try" and ":endtry" including everything being
          executed across ":source" commands, function calls,
          or autocommand invocations.


          And from :echoerr:




          When used inside a try conditional, the message is raised as an error exception instead (see try-echoerr).




          Other notes:




          • -u NONE prevents sourcing the default vimrcs


          • :cq makes Vim return a non-zero exit status


          To get the last error message (now actually an exception), use v:exception:



          $ vim -u NONE -c 'try | source vimrc-fail | catch | silent exec "!echo " shellescape(v:exception) |cq | endtry | q'; echo $?
          Vim(echoerr):test
          1





          share|improve this answer

























          • Thanks! Is there a way to echo the error message as well?

            – Joshua Spence
            Jan 21 at 23:11











          • @JoshuaSpence I think the last exception is stored in v:exception. Maybe something like exec "!echo " shellescape(v:exception) | cq instead of the plain cq would work.

            – Olorin
            Jan 22 at 1:10












          • That almost works... it requires me to press enter though.

            – Joshua Spence
            Jan 22 at 3:56











          • Ok, it works if I run vim with -E and -s

            – Joshua Spence
            Jan 22 at 4:11











          • silent exec should fix the Enter problem.

            – Olorin
            Jan 22 at 4:37















          4














          You can use a try-catch block to catch errors (which get converted to exceptions), and fail on any exception:



          $ cat vimrc-fail 
          echoerr 'test'
          $ cat vimrc-pass
          echo 'test'
          $ vim -u NONE -c 'try | source vimrc-pass | catch | cq | endtry | q'; echo $?
          0
          $ vim -u NONE -c 'try | source vimrc-fail | catch | cq | endtry | q'; echo $?
          1


          From the help:



          :try :try :endt :endtry E600 E601 E602
          :endt[ry] Change the error handling for the commands between
          ":try" and ":endtry" including everything being
          executed across ":source" commands, function calls,
          or autocommand invocations.


          And from :echoerr:




          When used inside a try conditional, the message is raised as an error exception instead (see try-echoerr).




          Other notes:




          • -u NONE prevents sourcing the default vimrcs


          • :cq makes Vim return a non-zero exit status


          To get the last error message (now actually an exception), use v:exception:



          $ vim -u NONE -c 'try | source vimrc-fail | catch | silent exec "!echo " shellescape(v:exception) |cq | endtry | q'; echo $?
          Vim(echoerr):test
          1





          share|improve this answer

























          • Thanks! Is there a way to echo the error message as well?

            – Joshua Spence
            Jan 21 at 23:11











          • @JoshuaSpence I think the last exception is stored in v:exception. Maybe something like exec "!echo " shellescape(v:exception) | cq instead of the plain cq would work.

            – Olorin
            Jan 22 at 1:10












          • That almost works... it requires me to press enter though.

            – Joshua Spence
            Jan 22 at 3:56











          • Ok, it works if I run vim with -E and -s

            – Joshua Spence
            Jan 22 at 4:11











          • silent exec should fix the Enter problem.

            – Olorin
            Jan 22 at 4:37













          4












          4








          4







          You can use a try-catch block to catch errors (which get converted to exceptions), and fail on any exception:



          $ cat vimrc-fail 
          echoerr 'test'
          $ cat vimrc-pass
          echo 'test'
          $ vim -u NONE -c 'try | source vimrc-pass | catch | cq | endtry | q'; echo $?
          0
          $ vim -u NONE -c 'try | source vimrc-fail | catch | cq | endtry | q'; echo $?
          1


          From the help:



          :try :try :endt :endtry E600 E601 E602
          :endt[ry] Change the error handling for the commands between
          ":try" and ":endtry" including everything being
          executed across ":source" commands, function calls,
          or autocommand invocations.


          And from :echoerr:




          When used inside a try conditional, the message is raised as an error exception instead (see try-echoerr).




          Other notes:




          • -u NONE prevents sourcing the default vimrcs


          • :cq makes Vim return a non-zero exit status


          To get the last error message (now actually an exception), use v:exception:



          $ vim -u NONE -c 'try | source vimrc-fail | catch | silent exec "!echo " shellescape(v:exception) |cq | endtry | q'; echo $?
          Vim(echoerr):test
          1





          share|improve this answer















          You can use a try-catch block to catch errors (which get converted to exceptions), and fail on any exception:



          $ cat vimrc-fail 
          echoerr 'test'
          $ cat vimrc-pass
          echo 'test'
          $ vim -u NONE -c 'try | source vimrc-pass | catch | cq | endtry | q'; echo $?
          0
          $ vim -u NONE -c 'try | source vimrc-fail | catch | cq | endtry | q'; echo $?
          1


          From the help:



          :try :try :endt :endtry E600 E601 E602
          :endt[ry] Change the error handling for the commands between
          ":try" and ":endtry" including everything being
          executed across ":source" commands, function calls,
          or autocommand invocations.


          And from :echoerr:




          When used inside a try conditional, the message is raised as an error exception instead (see try-echoerr).




          Other notes:




          • -u NONE prevents sourcing the default vimrcs


          • :cq makes Vim return a non-zero exit status


          To get the last error message (now actually an exception), use v:exception:



          $ vim -u NONE -c 'try | source vimrc-fail | catch | silent exec "!echo " shellescape(v:exception) |cq | endtry | q'; echo $?
          Vim(echoerr):test
          1






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 22 at 4:38

























          answered Jan 21 at 8:02









          OlorinOlorin

          3,3331418




          3,3331418












          • Thanks! Is there a way to echo the error message as well?

            – Joshua Spence
            Jan 21 at 23:11











          • @JoshuaSpence I think the last exception is stored in v:exception. Maybe something like exec "!echo " shellescape(v:exception) | cq instead of the plain cq would work.

            – Olorin
            Jan 22 at 1:10












          • That almost works... it requires me to press enter though.

            – Joshua Spence
            Jan 22 at 3:56











          • Ok, it works if I run vim with -E and -s

            – Joshua Spence
            Jan 22 at 4:11











          • silent exec should fix the Enter problem.

            – Olorin
            Jan 22 at 4:37

















          • Thanks! Is there a way to echo the error message as well?

            – Joshua Spence
            Jan 21 at 23:11











          • @JoshuaSpence I think the last exception is stored in v:exception. Maybe something like exec "!echo " shellescape(v:exception) | cq instead of the plain cq would work.

            – Olorin
            Jan 22 at 1:10












          • That almost works... it requires me to press enter though.

            – Joshua Spence
            Jan 22 at 3:56











          • Ok, it works if I run vim with -E and -s

            – Joshua Spence
            Jan 22 at 4:11











          • silent exec should fix the Enter problem.

            – Olorin
            Jan 22 at 4:37
















          Thanks! Is there a way to echo the error message as well?

          – Joshua Spence
          Jan 21 at 23:11





          Thanks! Is there a way to echo the error message as well?

          – Joshua Spence
          Jan 21 at 23:11













          @JoshuaSpence I think the last exception is stored in v:exception. Maybe something like exec "!echo " shellescape(v:exception) | cq instead of the plain cq would work.

          – Olorin
          Jan 22 at 1:10






          @JoshuaSpence I think the last exception is stored in v:exception. Maybe something like exec "!echo " shellescape(v:exception) | cq instead of the plain cq would work.

          – Olorin
          Jan 22 at 1:10














          That almost works... it requires me to press enter though.

          – Joshua Spence
          Jan 22 at 3:56





          That almost works... it requires me to press enter though.

          – Joshua Spence
          Jan 22 at 3:56













          Ok, it works if I run vim with -E and -s

          – Joshua Spence
          Jan 22 at 4:11





          Ok, it works if I run vim with -E and -s

          – Joshua Spence
          Jan 22 at 4:11













          silent exec should fix the Enter problem.

          – Olorin
          Jan 22 at 4:37





          silent exec should fix the Enter problem.

          – Olorin
          Jan 22 at 4:37

















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Unix & Linux 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.

          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%2funix.stackexchange.com%2fquestions%2f495721%2fvalidate-vimrc%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

          How to check contact read email or not when send email to Individual?

          Displaying single band from multi-band raster using QGIS

          How many registers does an x86_64 CPU actually have?