Loading ...
Sorry, an error occurred while loading the content.

Substitution gets broken when mapped

Expand Messages
  • Sylvia Ganush
    Hello. I m lost: one same substitution command works fine if executed from the command line, but gets broken if executed via a mapping. ... And it works
    Message 1 of 5 , May 12, 2013
    • 0 Attachment
      Hello.

      I'm lost: one same substitution command works fine if executed from the
      command line, but gets broken if executed via a mapping.

      To remove trailing spaces I do:

      :%s/\s\+$//

      And it works perfectly. Since it's such a useful command I add this
      mapping to my .vimrc:

      nmap <leader>w :%s/\s\+$// <cr>

      But when I use the mapping, many extra lines get deleted and the text is
      mangled.

      I expect the mapping to yield the same result as the direct command, but
      it doesn't. What gives?

      Sylvia

      --
      --
      You received this message from the "vim_use" maillist.
      Do not top-post! Type your reply below the text you are replying to.
      For more information, visit http://www.vim.org/maillist.php

      ---
      You received this message because you are subscribed to the Google Groups "vim_use" group.
      To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@....
      For more options, visit https://groups.google.com/groups/opt_out.
    • Tim Chase
      ... In theory, this *should* work (though the extra space before the can be optionally removed, as can the two trailing / characters). And, if tried
      Message 2 of 5 , May 12, 2013
      • 0 Attachment
        On 2013-05-12 22:01, Sylvia Ganush wrote:
        > To remove trailing spaces I do:
        >
        > :%s/\s\+$//
        >
        > And it works perfectly. Since it's such a useful command I add this
        > mapping to my .vimrc:
        >
        > nmap <leader>w :%s/\s\+$// <cr>

        In theory, this *should* work (though the extra space before the
        "<cr>" can be optionally removed, as can the two trailing "/"
        characters). And, if tried in a raw session of Vim (using "-u
        custom_vimrc" at startup, where "custom_vimrc" contains just

        set nocp
        nmap <leader>w :%s/\s\+$// <cr>
        " nnoremap <leader>w :%s/\s\+$<cr>

        This suggests that...

        > But when I use the mapping, many extra lines get deleted and the
        > text is mangled.

        you have some other mapping that is interfering, perhaps created by a
        plugin. I'd check the output of ":map" to see if something else has
        been mapped to that sequence.

        I'd also default to using "nnoremap" unless I had reason to use
        "nmap" otherwise. I don't think that has anything to do with this
        case, but by defaulting to nnoremap, I find I'm less likely to
        surprise myself in the few cases where it really would make a
        difference.

        -tim


        --
        --
        You received this message from the "vim_use" maillist.
        Do not top-post! Type your reply below the text you are replying to.
        For more information, visit http://www.vim.org/maillist.php

        ---
        You received this message because you are subscribed to the Google Groups "vim_use" group.
        To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@....
        For more options, visit https://groups.google.com/groups/opt_out.
      • tooth pik
        ... I d start by checking mappings as Tim suggested -- then I d use a function so I can (a) save and restore the current cursor position and (b) save and
        Message 3 of 5 , May 12, 2013
        • 0 Attachment
          On Sun, May 12, 2013 at 10:01:37PM +0200, Sylvia Ganush wrote:
          > Hello.

          > I'm lost: one same substitution command works fine if executed from the
          > command line, but gets broken if executed via a mapping.

          > To remove trailing spaces I do:

          > :%s/\s\+$//

          > And it works perfectly. Since it's such a useful command I add this
          > mapping to my .vimrc:

          > nmap <leader>w :%s/\s\+$// <cr>

          > But when I use the mapping, many extra lines get deleted and the text is
          > mangled.

          > I expect the mapping to yield the same result as the direct command, but
          > it doesn't. What gives?

          I'd start by checking mappings as Tim suggested -- then I'd use a
          function so I can (a) save and restore the current cursor position and
          (b) save and restore the last search register so I'm not blinded by
          highlighted spaces every time I insert one at the end of future lines:

          let mapleader = ','
          nnoremap <Leader>a :call StripTrailingWhitespace()<CR>
          function! StripTrailingWhitespace()
          let _s=@/
          let l = line(".")
          let c = col(".")
          %s/\s\+$//e
          let @/=_s
          call cursor(l, c)
          endfunction

          also I added the 'e' option so as not to be harassed with warnings if
          there are no trailing spaces

          sc

          --
          --
          You received this message from the "vim_use" maillist.
          Do not top-post! Type your reply below the text you are replying to.
          For more information, visit http://www.vim.org/maillist.php

          ---
          You received this message because you are subscribed to the Google Groups "vim_use" group.
          To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@....
          For more options, visit https://groups.google.com/groups/opt_out.
        • Tim Chase
          ... I too thought about the /e flag about 2 seconds after I pushed :-) I m curious if there was a reason you didn t use getpos()/setpos() (which takes
          Message 4 of 5 , May 12, 2013
          • 0 Attachment
            On 2013-05-12 15:36, tooth pik wrote:
            > let mapleader = ','
            > nnoremap <Leader>a :call StripTrailingWhitespace()<CR>
            > function! StripTrailingWhitespace()
            > let _s=@/
            > let l = line(".")
            > let c = col(".")
            > %s/\s\+$//e
            > let @/=_s
            > call cursor(l, c)
            > endfunction

            I too thought about the /e flag about 2 seconds after I pushed
            <send> :-)

            I'm curious if there was a reason you didn't use getpos()/setpos()
            (which takes care of some peculiar edge cases regarding
            'virtualedit') or make the saved-search local?

            let l:saved_search=@/
            let l:oldpos = getpos()
            %s/\s\+$//e
            setpos('.', l:oldpos)
            let @/=l:saved_search

            -tim


            --
            --
            You received this message from the "vim_use" maillist.
            Do not top-post! Type your reply below the text you are replying to.
            For more information, visit http://www.vim.org/maillist.php

            ---
            You received this message because you are subscribed to the Google Groups "vim_use" group.
            To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@....
            For more options, visit https://groups.google.com/groups/opt_out.
          • Sylvia Ganush
            ... But of course: I had w tied also to another mapping. I changed the mapping and all works as expected now. Thank you, Tim and Tooth Pik. Sylvia --
            Message 5 of 5 , May 12, 2013
            • 0 Attachment
              On 12 May 2013, at 22:36, tooth pik <toothpik6@...> wrote:

              > I'd start by checking mappings as Tim suggested...

              But of course: I had <leader>w tied also to another mapping. I changed the mapping
              and all works as expected now.

              Thank you, Tim and Tooth Pik.

              Sylvia

              --
              --
              You received this message from the "vim_use" maillist.
              Do not top-post! Type your reply below the text you are replying to.
              For more information, visit http://www.vim.org/maillist.php

              ---
              You received this message because you are subscribed to the Google Groups "vim_use" group.
              To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@....
              For more options, visit https://groups.google.com/groups/opt_out.
            Your message has been successfully submitted and would be delivered to recipients shortly.