Monday, February 1, 2016

Re: How would you repeatedly produce these lines?

On 1 February 2016 at 05:18, <etoipm1i0@gmail.com> wrote:
> I'm coding in Python and need to produce a number of lines like the following
>
>> p3[0] = {'name': , 'symbol': , 'number': }
>> p3[1] = {'name': , 'symbol': , 'number': }
>> ...
>
> and I'm wondering the best way to go about this. My main thought is to just copy-paste and maybe fill the brackets with some kind of place-keeper and then try to figure out a way to sequentially replace them with increasing integers. But somehow it seems like there out to be a more elegant Vim solution.

There are probably lots of different approaches, but I'd probably do
something like this:

Copy the first line, then paste it n (in this case 7) times:

yy7p

Move the cursor over the 0 in the first line, go into visual blockwise
mode and select the whole column:

Ctrl-V 7j

Increment using Dr Chip's visincr plugin
(http://vim.sourceforge.net/scripts/script.php?script_id=670)

:I

Alternatively (if you don't want to use plugins), after copying and
pasting, you could do:

:let a = 0 | g@^p3@s/0/\=a/ | let a += 1

This creates a variable 'a' and initialises it to zero. Then, for
every line starting in p3 (g@^p3@) it substitutes the first 0 with the
current value of a and then increments a.

Al

--
http://www.cgtk.co.uk

--
--
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@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment