Is there a text-editor trick to write several identical lines of code while incrementing the numbers on each line ?

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Is there a text-editor trick to write several identical lines of code while incrementing the numbers on each line ?

petafunk
This isn't a problem per se, I'm just asking for a tip if it exists. Sorry if I my explanation is not clear.

I'm going through Week 3 of the projects right now, and there's been several times already where you have to write n times the same line, only increasing the number in the bus addresses :

    Bit(in=in[0], load=load, out=out[0]);
    Bit(in=in[1], load=load, out=out[1]);
    Bit(in=in[2], load=load, out=out[2]);
    Bit(in=in[3], load=load, out=out[3]);
    [...]

It's fine to do it by hand, but it also sounds like something veteran coders would know how to automate.

In other languages than HDL I'd be using a "if" or other kind of loop, but they don't apply here. I'm thinking maybe there's something to do in the text editor itself ? With regular expressions maybe ? I've used them on a couple occasions but I don't handle them well enough to know if this is a possibility.

If it matters, i'm working with Sublime Text at the moment. Thanks for your suggestions.
Reply | Threaded
Open this post in threaded view
|

Re: Is there a text-editor trick to write several identical lines of code while incrementing the numbers on each line ?

WBahn
Administrator
It's something that you could throw a script together to do very easily in something like python or perl. There are probably text editors that have sufficient support for macros that might support it.

There have been a few text editors (there was a nice little editor for the Texas Instruments Professional computer called CSE -- it was the Texas Chain Saw Editor) that allowed you to highlight, copy, cut, paste any rectangular region on the screen, which made this king of editing trivially easy. I've only seen one other editor -- can't think of what it was -- the supported that feature, but I know there have been others.
Reply | Threaded
Open this post in threaded view
|

Re: Is there a text-editor trick to write several identical lines of code while incrementing the numbers on each line ?

petafunk
I'm not familiar with macros at all, but at some point I'll need to be I guess.

Sublime Text has a feature which, on selection of a string, can highlight all the instances of that string. As is it's not the best way to deal with my task but with some tinkering it's better than manual number incrementation.
Reply | Threaded
Open this post in threaded view
|

Re: Is there a text-editor trick to write several identical lines of code while incrementing the numbers on each line ?

WBahn
Administrator
Most editors have the ability to search and highlight all instances of a string. I'm not clear on how that helps you avoid manual number incrementation. What is it that you do after they are all highlighted?

Reply | Threaded
Open this post in threaded view
|

Re: Is there a text-editor trick to write several identical lines of code while incrementing the numbers on each line ?

petafunk
Sorry, I was confusing with another feature of ST. I was thinking of "Replace All", which you can restrict to part of the text. So I Replace All the 0s on the first line, then the second line etc. with the incremented number. That's only interesting if there's 3+ numbers to replace per line.
Reply | Threaded
Open this post in threaded view
|

Re: Is there a text-editor trick to write several identical lines of code while incrementing the numbers on each line ?

Gerrit0
The way I'd do this is by just opening up my browser's development console (F12) and using a script. I'm not aware of any way to do this within any text editor, though the ability to have multiple cursors can make it less painful. Chrome has a builtin function available in the devtools called copy. Example:
copy(Array.from({ length: 10 }, (_, idx) => `Bit(in=in[${idx}], load=load, out=out[${idx}]);`).join('\n'))
Output:
Bit(in=in[0], load=load, out=out[0]);
Bit(in=in[1], load=load, out=out[1]);
Bit(in=in[2], load=load, out=out[2]);
Bit(in=in[3], load=load, out=out[3]);
Bit(in=in[4], load=load, out=out[4]);
Bit(in=in[5], load=load, out=out[5]);
Bit(in=in[6], load=load, out=out[6]);
Bit(in=in[7], load=load, out=out[7]);
Bit(in=in[8], load=load, out=out[8]);
Bit(in=in[9], load=load, out=out[9]);