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

find and replace with sequential numbers

Expand Messages
  • Chris Lott
    I have a document with the following kind of text: ## 3. foo various text here ## 1. foo more text here What I would like to do is search for all the headers
    Message 1 of 4 , Jul 12 9:21 AM
    • 0 Attachment
      I have a document with the following kind of text:

      ## 3. foo

      various text here

      ## 1. foo

      more text here

      What I would like to do is search for all the headers (## X. etc) and
      replace them with the proper sequential numbering. How do I approach
      this?

      c
      --
      Chris Lott <chris@...>

      --
      --
      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.
    • Paul Isambert
      ... Something like this: function! s:renumber () let [a:count, a:lineno, a:last] = [0, 1, line( $ )] while a:lineno
      Message 2 of 4 , Jul 12 10:05 AM
      • 0 Attachment
        Chris Lott <chris@...> a écrit:
        > I have a document with the following kind of text:
        >
        > ## 3. foo
        >
        > various text here
        >
        > ## 1. foo
        >
        > more text here
        >
        > What I would like to do is search for all the headers (## X. etc) and
        > replace them with the proper sequential numbering. How do I approach
        > this?

        Something like this:

        function! s:renumber ()
        let [a:count, a:lineno, a:last] = [0, 1, line('$')]
        while a:lineno <= a:last
        let a:line = getline(a:lineno)
        if a:line =~ '^##\s\+\d\+\.'
        let a:count += 1
        call setline(a:lineno, substitute(a:line, '^##\s\+\zs\d\+\ze\.', a:count, ''))
        endif
        let a:lineno += 1
        endwhile
        endfunction
        com! Renumber call s:renumber()

        I’m sure some “vizard” can do the same without using a counter like
        a:count (and, of course, with :substitute).

        Best,
        Paul

        --
        --
        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.
      • Adnan Zafar
        ... The relevant helptags are |:g| and |sub-replace-expression|. There s also the Nexus plugin:
        Message 3 of 4 , Jul 12 10:33 AM
        • 0 Attachment
          On Fri, Jul 12, 2013 at 12:21 PM, Chris Lott <chris@...> wrote:
          > I have a document with the following kind of text:
          >
          > ## 3. foo
          >
          > various text here
          >
          > ## 1. foo
          >
          > more text here
          >
          > What I would like to do is search for all the headers (## X. etc) and
          > replace them with the proper sequential numbering. How do I approach
          > this?
          >
          > c
          > --
          > Chris Lott <chris@...>
          >
          > --
          > --
          > 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.


          The following should do it:

          :let i = 1
          :g/## \d\+\./ s/\d\+/\=i/ | let i = i + 1

          The relevant helptags are |:g| and |sub-replace-expression|. There's
          also the Nexus plugin:
          <http://of-vim-and-vigor.blogspot.com/2012/04/do-it-with-vim-nexus.html>

          --Adnan

          --
          --
          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 m pretty sure it requires a counter, but it can be done with a :substitute command and a helper function: function! Add() let b:counter += 1 return
          Message 4 of 4 , Jul 12 11:21 AM
          • 0 Attachment
            On 2013-07-12 19:05, Paul Isambert wrote:
            > Chris Lott <chris@...> a écrit:
            > > I have a document with the following kind of text:
            > >
            > > ## 3. foo
            > >
            > > various text here
            > >
            > > ## 1. foo
            > >
            > > more text here
            > >
            > > What I would like to do is search for all the headers (## X. etc)
            > > and replace them with the proper sequential numbering. How do I
            > > approach this?
            >
            > I’m sure some “vizard” can do the same without using a counter like
            > a:count (and, of course, with :substitute).

            I'm pretty sure it requires a counter, but it can be done with
            a :substitute command and a helper function:

            function! Add()
            let b:counter += 1
            return b:counter
            endfunction

            :let b:counter=0 | %s/## \zs\d\+\ze\./\=Add()

            This also give you pretty easy access to change the starting index,
            and if you wanted to pass an increment-amount to Add(), it could be
            done.

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