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

copy and replace with a slight modification a line several times

Expand Messages
  • A Loumiotis
    Hi, Starting with a file with two lines: var1_$x; var2 $x-1 var3x; var4x==1 I would like to end up with the following seven line file: var1_1; var2 1-1 var1_2;
    Message 1 of 7 , Dec 11, 2012
    • 0 Attachment
      Hi,
      Starting with a file with two lines:

      var1_$x; var2>$x-1
      var3x; var4x==1

      I would like to end up with the following seven line file:

      var1_1; var2>1-1
      var1_2; var2>2-1
      var1_3; var2>3-1
      var3_a; var4_a==1
      var3_b; var4_b==1
      var3_c; var4_c==1
      var3_d; var4_d==1

      How could I automate this procedure?

      I was thinking of something of the following terms but I'm not sure how to represent the "i" inside a for loop and whether the setline function is called correctly (I'm new with VIM scripting):

      let line=getline(",")
      for i in range(1,3)
         let repl'i'=substitute(line,'\$x','i',"g")
      endfor
      call setline(.,line,repl1,repl2,repl3)??? 

      Thanks,
      Antonis


      --
      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
    • John Beckett
      ... Here is a start which converts the first line of input to the first three lines of output: function! Convert(linenr) let line = getline(a:linenr) let
      Message 2 of 7 , Dec 11, 2012
      • 0 Attachment
        A Loumiotis wrote:
        > Starting with a file with two lines:
        >
        > var1_$x; var2>$x-1
        > var3x; var4x==1
        >
        > I would like to end up with the following seven line file:
        >
        > var1_1; var2>1-1
        > var1_2; var2>2-1
        > var1_3; var2>3-1
        > var3_a; var4_a==1
        > var3_b; var4_b==1
        > var3_c; var4_c==1
        > var3_d; var4_d==1
        >
        > How could I automate this procedure?

        Here is a start which converts the first line of input to the
        first three lines of output:

        function! Convert(linenr)
        let line = getline(a:linenr)
        let result = []
        for i in range(1, 3)
        call add(result, substitute(line, '\$x', i, 'g'))
        endfor
        call setline(a:linenr, result[0])
        execute a:linenr . 'put =result[1:]'
        endfunction

        With the cursor on the first line of input, following converts
        the first line:
        :call Convert('.')

        Or, if the first line to be converted is line 12, this works:
        :call Convert(12)

        I suspect a complex :s/// with replacement \=... could do the
        job, but the script is more maintainable and useful for related
        work.

        For the second change required, see:
        :help function-list
        :help nr2char()

        John

        --
        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
        ... First, see John s reply. But I had a big reply written, and thought I sent it; I have no idea what happened to it. Here s the highlights: I d probably try
        Message 3 of 7 , Dec 11, 2012
        • 0 Attachment
          On Tuesday, December 11, 2012 7:33:48 AM UTC-6, A Loumiotis wrote:
          > Hi,
          > Starting with a file with two lines:
          >
          > var1_$x; var2>$x-1
          > var3x; var4x==1
          >
          > I would like to end up with the following seven line file:
          >
          >
          > var1_1; var2>1-1
          > var1_2; var2>2-1
          > var1_3; var2>3-1
          > var3_a; var4_a==1
          > var3_b; var4_b==1
          > var3_c; var4_c==1
          >
          > var3_d; var4_d==1
          >
          > How could I automate this procedure?
          >
          > I was thinking of something of the following terms but I'm not sure how to represent the "i" inside a for loop and whether the setline function is called correctly (I'm new with VIM scripting):
          >
          >
          > let line=getline(",")
          > for i in range(1,3)
          >    let repl'i'=substitute(line,'\$x','i',"g")
          > endfor
          > call setline(.,line,repl1,repl2,repl3)??? 
          >
          > Thanks,
          > Antonis

          First, see John's reply.

          But I had a big reply written, and thought I sent it; I have no idea what happened to it.

          Here's the highlights:

          I'd probably try using a recorded macro. For the first set of lines, it would rely on CTRL-A in normal mode. For the second set of lines, nr2char() and char2nr() functions in an expression register in insert mode.

          :help CTRL-A
          :help complex-repeat

          Macros might get a little too complex, and are best for one-shot tasks that you won't need to do very often. So I would also look at :help curly-brace-names to fix the initial idea you had for your function. You will also need to get your setline command right, it should be:

          call setline(".",[line,repl1,repl2,repl3])

          --
          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
        • John Beckett
          ... There is a problem with that because four lines will be inserted into the buffer, and those lines will OVERWRITE existing lines. That s why my script used
          Message 4 of 7 , Dec 11, 2012
          • 0 Attachment
            Ben Fritz wrote:
            > ...
            > call setline(".",[line,repl1,repl2,repl3])

            There is a problem with that because four lines will be inserted
            into the buffer, and those lines will OVERWRITE existing lines.
            That's why my script used 'setline()' to replace the first line,
            and 'put' to insert the extra lines.

            John

            --
            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
            ... Oops, I guess I wanted append(). I thought I used setline() in TOhtml like this, but I only do it this way when the new buffer is empty. For adding text
            Message 5 of 7 , Dec 11, 2012
            • 0 Attachment
              On Tuesday, December 11, 2012 6:55:30 PM UTC-6, JohnBeckett wrote:
              > Ben Fritz wrote:
              >
              > > ...
              >
              > > call setline(".",[line,repl1,repl2,repl3])
              >
              >
              >
              > There is a problem with that because four lines will be inserted
              >
              > into the buffer, and those lines will OVERWRITE existing lines.
              >
              > That's why my script used 'setline()' to replace the first line,
              >
              > and 'put' to insert the extra lines.
              >

              Oops, I guess I wanted append(). I thought I used setline() in TOhtml like this, but I only do it this way when the new buffer is empty. For adding text TOhtml uses append().

              --
              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
            • A Loumiotis
              ... Many thanks John and Ben for your help. I m not sure though how to use nr2char() for the second change. However, I slightly modified John s convert()
              Message 6 of 7 , Dec 14, 2012
              • 0 Attachment
                On Wed, Dec 12, 2012 at 3:27 AM, Ben Fritz <fritzophrenic@...> wrote:
                On Tuesday, December 11, 2012 6:55:30 PM UTC-6, JohnBeckett wrote:
                > Ben Fritz wrote:
                >
                > > ...
                >
                > > call setline(".",[line,repl1,repl2,repl3])
                >
                >
                >
                > There is a problem with that because four lines will be inserted
                >
                > into the buffer, and those lines will OVERWRITE existing lines.
                >
                > That's why my script used 'setline()' to replace the first line,
                >
                > and 'put' to insert the extra lines.
                >

                Oops, I guess I wanted append(). I thought I used setline() in TOhtml like this, but I only do it this way when the new buffer is empty. For adding text TOhtml uses append().

                --
                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

                Many thanks John and Ben for your help.  I'm not sure though how to use nr2char() for the second change.  However, I slightly modified John's convert() function and got the required result:

                function! Conva_d(linenr)
                  let line = getline(a:linenr)
                  let result = []
                  for i in ["a","b","c","d"]
                    call add(result, substitute(line, 'x\(\w\)\@!','_' . i, 'g'))
                  endfor
                  call setline(a:linenr, result[0])
                  execute a:linenr . 'put =result[1:]'
                endfunction

                --
                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
              • John Beckett
                ... From something Ben mentioned, the line: execute a:linenr . put =result[1:] is more cleanly written as: call append(a:linenr, result[1:]) John -- You
                Message 7 of 7 , Dec 14, 2012
                • 0 Attachment
                  A Loumiotis wrote:
                  > function! Conva_d(linenr)
                  > let line = getline(a:linenr)
                  > let result = []
                  > for i in ["a","b","c","d"]
                  > call add(result, substitute(line, 'x\(\w\)\@!','_' . i, 'g'))
                  > endfor
                  > call setline(a:linenr, result[0])
                  > execute a:linenr . 'put =result[1:]'
                  > endfunction

                  From something Ben mentioned, the line:
                  execute a:linenr . 'put =result[1:]'

                  is more cleanly written as:
                  call append(a:linenr, result[1:])

                  John

                  --
                  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.