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

increment existing numbers by 1

Expand Messages
  • stosss
    I found this. I think it is what I want but I don t understand what it is doing or how to initiate it.
    Message 1 of 5 , Dec 23, 2012
    • 0 Attachment
      I found this. I think it is what I want but I don't understand what it
      is doing or how to initiate it.

      http://vim.wikia.com/wiki/Substitute_with_incrementing_numbers

      Incrementing selected numbers

      function! Incr()
      let a = line('.') - line("'<")
      let c = virtcol("'<")
      if a > 0
      execute 'normal! '.c.'|'.a."\<C-a>"
      endif
      normal `<
      endfunction
      vnoremap <C-a> :call Incr()<CR>


      I want to increment numbers that exist by 1. I also don't want it to
      change other numbers only numbers in a certain pattern. The pattern
      that I want to manipulate is:

      :'y,'zs@^[ ]*<navPoint id="navPoint-\zs\(\d\d\d\)\ze">$@not sure what
      I would put here@

      I have tried several different attempts with s@pattern@replace@ but
      each one would skip every other pattern.

      So if the number was 242 I want it to change to 243 and then that one
      change to 244 etc. That would happen on only the pattern in the range
      'y,'z or the actual line numbers could be the range. The pattern
      number of each line needs to become the pattern number of the next
      pattern after it. The spacing between the lines with the pattern
      varies from five lines to six lines.

      The number in the pattern is the only number on the line.

      I have several hundred lines scattered over about 3000 lines that need
      to be edited but the only way I know how to do it is manually one line
      at a time. I am obviously trying to avoid that pain.

      Can anyone point me to a solution or tell me if I found the solution
      and how to use it?

      Thanks for any help

      --
      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
    • stosss
      ... I found my answer now I have some more tools in my Vim bag. y, zs@navPoint- zs d d d ze@ =submatch(0) + 1@ It worked great! -- You received this message
      Message 2 of 5 , Dec 23, 2012
      • 0 Attachment
        On Sun, Dec 23, 2012 at 8:33 PM, stosss <stosss@...> wrote:
        > I found this. I think it is what I want but I don't understand what it
        > is doing or how to initiate it.
        >
        > http://vim.wikia.com/wiki/Substitute_with_incrementing_numbers
        >
        > Incrementing selected numbers
        >
        > function! Incr()
        > let a = line('.') - line("'<")
        > let c = virtcol("'<")
        > if a > 0
        > execute 'normal! '.c.'|'.a."\<C-a>"
        > endif
        > normal `<
        > endfunction
        > vnoremap <C-a> :call Incr()<CR>
        >
        >
        > I want to increment numbers that exist by 1. I also don't want it to
        > change other numbers only numbers in a certain pattern. The pattern
        > that I want to manipulate is:
        >
        > :'y,'zs@^[ ]*<navPoint id="navPoint-\zs\(\d\d\d\)\ze">$@not sure what
        > I would put here@
        >
        > I have tried several different attempts with s@pattern@replace@ but
        > each one would skip every other pattern.
        >
        > So if the number was 242 I want it to change to 243 and then that one
        > change to 244 etc. That would happen on only the pattern in the range
        > 'y,'z or the actual line numbers could be the range. The pattern
        > number of each line needs to become the pattern number of the next
        > pattern after it. The spacing between the lines with the pattern
        > varies from five lines to six lines.
        >
        > The number in the pattern is the only number on the line.
        >
        > I have several hundred lines scattered over about 3000 lines that need
        > to be edited but the only way I know how to do it is manually one line
        > at a time. I am obviously trying to avoid that pain.
        >
        > Can anyone point me to a solution or tell me if I found the solution
        > and how to use it?
        >
        > Thanks for any help

        I found my answer now I have some more tools in my Vim bag.

        'y,'zs@navPoint-\zs\d\d\d\ze@\=submatch(0) + 1@

        It worked great!

        --
        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
      • stosss
        ... I figured out how to use this. The next step will be understanding how it is doing it. -- You received this message from the vim_use maillist. Do not
        Message 3 of 5 , Dec 26, 2012
        • 0 Attachment
          On Sun, Dec 23, 2012 at 10:09 PM, stosss <stosss@...> wrote:
          >> function! Incr()
          >> let a = line('.') - line("'<")
          >> let c = virtcol("'<")
          >> if a > 0
          >> execute 'normal! '.c.'|'.a."\<C-a>"
          >> endif
          >> normal `<
          >> endfunction
          >> vnoremap <C-a> :call Incr()<CR>

          I figured out how to use this. The next step will be understanding how
          it is doing it.

          --
          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
        • stosss
          I found this ... Note that you have to press Ctrl-V Ctrl-A to get the ^A character. I have ... to do what I want. But can CTRL-A or ^A be used in a
          Message 4 of 5 , Dec 31, 2012
          • 0 Attachment
            I found this

            You have to use normal to execute normal mode commands in command mode:

            :g/searchString/ normal ^A

            Note that you have to press Ctrl-V Ctrl-A to get the ^A character.

            I have

            :s@\d\+@\=submatch(0) +1@g

            to do what I want.

            But can CTRL-A or ^A be used in a :s@search@replace@g command? I
            haven't found anything that shows that it can be used that way. I saw
            in :help that it can be used in a macro. It seems if it could be used
            in a single line search with global replacement at the command line
            then it could also be used in a map.

            I'm just curious because I found a workable solution when I was trying
            to figure out if CTRL-A could be used.

            Thanks

            --
            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
          • Ben Fritz
            ... I think it should be possible to use in an s command using the expression register and a function wrapping the normal command. But I think the solution you
            Message 5 of 5 , Jan 1, 2013
            • 0 Attachment
              On Monday, December 31, 2012 9:23:43 PM UTC-6, Jack Gates wrote:
              > I found this
              >
              >
              >
              > You have to use normal to execute normal mode commands in command mode:
              >
              >
              >
              > :g/searchString/ normal ^A
              >
              >
              >
              > Note that you have to press Ctrl-V Ctrl-A to get the ^A character.
              >
              >
              >
              > I have
              >
              >
              >
              > :s@\d\+@\=submatch(0) +1@g
              >
              >
              >
              > to do what I want.
              >
              >
              >
              > But can CTRL-A or ^A be used in a :s@search@replace@g command? I
              >
              > haven't found anything that shows that it can be used that way. I saw
              >
              > in :help that it can be used in a macro. It seems if it could be used
              >
              > in a single line search with global replacement at the command line
              >
              > then it could also be used in a map.
              >

              I think it should be possible to use in an s command using the expression register and a function wrapping the normal command. But I think the solution you used with submatch() is much better.

              It can certainly be used in a mapping as well, where <C-A> is a better notation than a literal ^A character, but either will work. Just remember CTRL-A is a *normal mode* command, and therefore cannot be used directly from the command-line, where your :s command is executed.

              --
              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
            Your message has been successfully submitted and would be delivered to recipients shortly.