user2918461
user2918461

Reputation:

Vim wizardry to do this

Want a simple way to create this file:

def foo1():

def foo2():

def foo...():

def fooN():

without typing each function signature out. Possible? In excel I would make 'def foo' a column, make an integer column, then a '():' column, copy paste into a .py file, but I want an equally simple solution in vim or sublime! Anyone know how to do this sorcery?

Upvotes: 5

Views: 174

Answers (2)

Change the value of the variable N to your needs:

:let N=5
:execute "normal! ".N."idef foo0():\r\r\<Esc>" | g/\d\+/ :.,$s//\=submatch(0)+1/

N denotes the number of foo functions starting from 1 to N

Upvotes: 3

dionyziz
dionyziz

Reputation: 2462

For this trick, we'll use vim. Start by typing the first definition at the top of the code. In insert mode type:

def foo1():

Then exit insert mode using <esc>.

We'll now create a vim macro to replicate this as many times as you like. Go to the beginning of the file with gg.

Now, start recording a macro using qq. This will store your macro in the "q" register. First make a copy of the function definition using yyp. If you want, you can create a blank line above using O<esc>j. Then increment the function number of your copy using Ctrl + a. Finish by going to the beginning of the line using 0 and stop recording the macro using q.

Now simply replay the macro as many times as you like. For example, type 100@q to play it 100 times. Voilà!

Upvotes: 6

Related Questions