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

134989RE: copy and replace with a slight modification a line several times

Expand Messages
  • John Beckett
    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
    • Show all 7 messages in this topic