vim: how can i make a mapping from a autocmd

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











up vote
0
down vote

favorite












in my .vimrc i have an autocmd which cleans up my files, but how can i make a mapping out of the command?



autocmd BufWritePre *.test1 silent! :%s/s+$//e | silent! :%s/(nn)n+/1/ | silent! :%s#($ns*)+%$## | :%s/;$
nnoremap ;; silent! :%s/s+$//e | silent! :%s/(nn)n+/1/ | silent! :%s#($ns*)+%$## | :%s/;$<CR>


The mapping fails saying



Error detected while processing ~/.vimrc:
line 966:
E486: Pattern not found: ;$<CR>









share|improve this question

























    up vote
    0
    down vote

    favorite












    in my .vimrc i have an autocmd which cleans up my files, but how can i make a mapping out of the command?



    autocmd BufWritePre *.test1 silent! :%s/s+$//e | silent! :%s/(nn)n+/1/ | silent! :%s#($ns*)+%$## | :%s/;$
    nnoremap ;; silent! :%s/s+$//e | silent! :%s/(nn)n+/1/ | silent! :%s#($ns*)+%$## | :%s/;$<CR>


    The mapping fails saying



    Error detected while processing ~/.vimrc:
    line 966:
    E486: Pattern not found: ;$<CR>









    share|improve this question























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      in my .vimrc i have an autocmd which cleans up my files, but how can i make a mapping out of the command?



      autocmd BufWritePre *.test1 silent! :%s/s+$//e | silent! :%s/(nn)n+/1/ | silent! :%s#($ns*)+%$## | :%s/;$
      nnoremap ;; silent! :%s/s+$//e | silent! :%s/(nn)n+/1/ | silent! :%s#($ns*)+%$## | :%s/;$<CR>


      The mapping fails saying



      Error detected while processing ~/.vimrc:
      line 966:
      E486: Pattern not found: ;$<CR>









      share|improve this question













      in my .vimrc i have an autocmd which cleans up my files, but how can i make a mapping out of the command?



      autocmd BufWritePre *.test1 silent! :%s/s+$//e | silent! :%s/(nn)n+/1/ | silent! :%s#($ns*)+%$## | :%s/;$
      nnoremap ;; silent! :%s/s+$//e | silent! :%s/(nn)n+/1/ | silent! :%s#($ns*)+%$## | :%s/;$<CR>


      The mapping fails saying



      Error detected while processing ~/.vimrc:
      line 966:
      E486: Pattern not found: ;$<CR>






      vim vimrc






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Aug 15 at 5:03









      divramod

      1225




      1225




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          So the main problem with your command is that by using | you're actually breaking it into several commands, the first of which being nnoremap, but from the second on, a series of silent! commands with substitutions, which are being run at the time your .vimrc is being sourced, rather than becoming part of the command.



          Using either | or <bar> should solve that.



          The second issue is that nnoremap expects a command in normal mode, so it takes your silent! and runs the s (substitute) command, which then replaces the character under the cursor with ilent! ..., which is not what you want... You need an extra : before that first silent! so it knows to go into command mode.



          And third, your last command %s/;$<CR> is not really well formed, you need two additional /s to complete it. I'm assuming your intention is to strip all ;s at the end of lines, so %s/;$// is what I'm assuming you want here. (I also noticed you skipped a silent! there, maybe by omission? I'm adding it here, just in case.)



          Putting it all together:



          nnoremap ;; :silent! :%s/s+$//e | silent! :%s/(nn)n+/1/ | silent! :%s#($ns*)+%$## | silent! :%s/;$//<CR>





          share|improve this answer
















          • 1




            thx a lot. you got everything right. thx for the explanations too!
            – divramod
            Aug 15 at 5:39











          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',
          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%2funix.stackexchange.com%2fquestions%2f462668%2fvim-how-can-i-make-a-mapping-from-a-autocmd%23new-answer', 'question_page');

          );

          Post as a guest






























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          1
          down vote



          accepted










          So the main problem with your command is that by using | you're actually breaking it into several commands, the first of which being nnoremap, but from the second on, a series of silent! commands with substitutions, which are being run at the time your .vimrc is being sourced, rather than becoming part of the command.



          Using either | or <bar> should solve that.



          The second issue is that nnoremap expects a command in normal mode, so it takes your silent! and runs the s (substitute) command, which then replaces the character under the cursor with ilent! ..., which is not what you want... You need an extra : before that first silent! so it knows to go into command mode.



          And third, your last command %s/;$<CR> is not really well formed, you need two additional /s to complete it. I'm assuming your intention is to strip all ;s at the end of lines, so %s/;$// is what I'm assuming you want here. (I also noticed you skipped a silent! there, maybe by omission? I'm adding it here, just in case.)



          Putting it all together:



          nnoremap ;; :silent! :%s/s+$//e | silent! :%s/(nn)n+/1/ | silent! :%s#($ns*)+%$## | silent! :%s/;$//<CR>





          share|improve this answer
















          • 1




            thx a lot. you got everything right. thx for the explanations too!
            – divramod
            Aug 15 at 5:39















          up vote
          1
          down vote



          accepted










          So the main problem with your command is that by using | you're actually breaking it into several commands, the first of which being nnoremap, but from the second on, a series of silent! commands with substitutions, which are being run at the time your .vimrc is being sourced, rather than becoming part of the command.



          Using either | or <bar> should solve that.



          The second issue is that nnoremap expects a command in normal mode, so it takes your silent! and runs the s (substitute) command, which then replaces the character under the cursor with ilent! ..., which is not what you want... You need an extra : before that first silent! so it knows to go into command mode.



          And third, your last command %s/;$<CR> is not really well formed, you need two additional /s to complete it. I'm assuming your intention is to strip all ;s at the end of lines, so %s/;$// is what I'm assuming you want here. (I also noticed you skipped a silent! there, maybe by omission? I'm adding it here, just in case.)



          Putting it all together:



          nnoremap ;; :silent! :%s/s+$//e | silent! :%s/(nn)n+/1/ | silent! :%s#($ns*)+%$## | silent! :%s/;$//<CR>





          share|improve this answer
















          • 1




            thx a lot. you got everything right. thx for the explanations too!
            – divramod
            Aug 15 at 5:39













          up vote
          1
          down vote



          accepted







          up vote
          1
          down vote



          accepted






          So the main problem with your command is that by using | you're actually breaking it into several commands, the first of which being nnoremap, but from the second on, a series of silent! commands with substitutions, which are being run at the time your .vimrc is being sourced, rather than becoming part of the command.



          Using either | or <bar> should solve that.



          The second issue is that nnoremap expects a command in normal mode, so it takes your silent! and runs the s (substitute) command, which then replaces the character under the cursor with ilent! ..., which is not what you want... You need an extra : before that first silent! so it knows to go into command mode.



          And third, your last command %s/;$<CR> is not really well formed, you need two additional /s to complete it. I'm assuming your intention is to strip all ;s at the end of lines, so %s/;$// is what I'm assuming you want here. (I also noticed you skipped a silent! there, maybe by omission? I'm adding it here, just in case.)



          Putting it all together:



          nnoremap ;; :silent! :%s/s+$//e | silent! :%s/(nn)n+/1/ | silent! :%s#($ns*)+%$## | silent! :%s/;$//<CR>





          share|improve this answer












          So the main problem with your command is that by using | you're actually breaking it into several commands, the first of which being nnoremap, but from the second on, a series of silent! commands with substitutions, which are being run at the time your .vimrc is being sourced, rather than becoming part of the command.



          Using either | or <bar> should solve that.



          The second issue is that nnoremap expects a command in normal mode, so it takes your silent! and runs the s (substitute) command, which then replaces the character under the cursor with ilent! ..., which is not what you want... You need an extra : before that first silent! so it knows to go into command mode.



          And third, your last command %s/;$<CR> is not really well formed, you need two additional /s to complete it. I'm assuming your intention is to strip all ;s at the end of lines, so %s/;$// is what I'm assuming you want here. (I also noticed you skipped a silent! there, maybe by omission? I'm adding it here, just in case.)



          Putting it all together:



          nnoremap ;; :silent! :%s/s+$//e | silent! :%s/(nn)n+/1/ | silent! :%s#($ns*)+%$## | silent! :%s/;$//<CR>






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Aug 15 at 5:27









          Filipe Brandenburger

          3,734622




          3,734622







          • 1




            thx a lot. you got everything right. thx for the explanations too!
            – divramod
            Aug 15 at 5:39













          • 1




            thx a lot. you got everything right. thx for the explanations too!
            – divramod
            Aug 15 at 5:39








          1




          1




          thx a lot. you got everything right. thx for the explanations too!
          – divramod
          Aug 15 at 5:39





          thx a lot. you got everything right. thx for the explanations too!
          – divramod
          Aug 15 at 5:39


















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f462668%2fvim-how-can-i-make-a-mapping-from-a-autocmd%23new-answer', 'question_page');

          );

          Post as a guest













































































          Popular posts from this blog

          Peggy Mitchell

          Palaiologos

          The Forum (Inglewood, California)