Hi, Allan-
Following up from IRC...
My text editor of choice on Windows was TextPad, and it supported a handy way to insert sequential numbers for "replace all" operations. This was useful, for example, when inserting line numbers, creating unique ids, etc.
I don't know if the syntax was unique to TextPad, or if it is part of some standard regex syntax, but it was pretty simple and effective:
\i(<start_index>,<increment>)
\i Replace with numbers starting from 1, incrementing by 1. \i(10) Replace with numbers starting from 10, incrementing by 1. \i(0,10) Replace with numbers starting from 0, incrementing by 10. \i(100,-10) Replace with numbers starting from 100, decrementing by -10.
Any chance this (or something similar) could be added to TextMate?
Thanks- -Doug
On Mar 1, 2010, at 9:32 AM, Doug Schepers wrote:
My text editor of choice on Windows was TextPad, and it supported a handy way to insert sequential numbers for "replace all" operations. This was useful, for example, when inserting line numbers, creating unique ids, etc.
I don't know if the syntax was unique to TextPad, or if it is part of some standard regex syntax, but it was pretty simple and effective:
\i(<start_index>,<increment>)
\i Replace with numbers starting from 1, incrementing by 1. \i(10) Replace with numbers starting from 10, incrementing by 1. \i(0,10) Replace with numbers starting from 0, incrementing by 10. \i(100,-10) Replace with numbers starting from 100, decrementing by -10.
Any chance this (or something similar) could be added to TextMate?
Hi Doug,
for such cases I use this tmcommand: Input: selection or document Ouptut: new doc Command: #!/usr/bin/perl -w #!/usr/bin/perl -w
$cnt=1; $inc=1;
while(<>) { if(m/^\i((\d+)\s*,\s*(\d+))$/) { $cnt=$1; $inc=$2; next; } while(s/\i/$cnt/) {$cnt+=$inc;} print; } Example text: Hello, this is a test. \i(2,5) Here my \i. item followed by item \i. Furthermore here is my \i. item.
Then you get: Hello, this is a test. Here my 2. item followed by item 7. Furthermore here is my 12. item.
If you do not set up \i(a,b) it simply starts at 1 incremented by 1.
Hopefully it helps.
--Hans
Hi, Hans-
Thanks for the reply.
Hans-Jörg Bibiko wrote (on 3/1/10 4:10 AM):
for such cases I use this tmcommand: Input: selection or document Ouptut: new doc Command: #!/usr/bin/perl -w #!/usr/bin/perl -w
$cnt=1; $inc=1;
while(<>) { if(m/^\i((\d+)\s*,\s*(\d+))$/) { $cnt=$1; $inc=$2; next; } while(s/\i/$cnt/) {$cnt+=$inc;} print; } Example text: Hello, this is a test. \i(2,5) Here my \i. item followed by item \i. Furthermore here is my \i. item.
Then you get: Hello, this is a test. Here my 2. item followed by item 7. Furthermore here is my 12. item.
If you do not set up \i(a,b) it simply starts at 1 incremented by 1.
I've been using TextMate for a while, but I haven't had the time to dig into its more powerful features. I was able to create the command, but I don't quite understand the usage.
Here's an example use case: I have a document, and I want to write out a regex that matches on a certain pattern (Find: (<a) (class="([^"]*)") ) and add an id to each matching instance (Replace: $1 id="_\i(2,5)" $2 ), with changes applied inline into the current document. With a command, how to I supply the pattern to match? Is this possible?
Regards- -Doug
Hi,
a quick solution could be:
Create a tmcommand "increment \i":
Input: selection or document Ouptut: replace selected text Command: #!/usr/bin/perl -w
$cnt=1; $inc=1;
while(<>) { if(m/^\i((\d+)\s*,\s*(\d+))$/) { $cnt=$1; $inc=$2; next; } while(s/\i/$cnt/) {$cnt+=$inc;} print; }
then record a macro: 1. command: Find as regexp: (<a) (class="([^"]*)") Replace all with: $1 id="_\i" $2 2. command: Call "increment \i"
and save that macro.
Then you can specify at the begin of your doc a line for setting up the \i \i(2,5)
and run the new macro.
--Hans
On 1 Mar 2010, at 09:32, Doug Schepers wrote:
[…] it supported a handy way to insert sequential numbers for "replace all" operations […]
Any chance this (or something similar) could be added to TextMate?
It is on my own wishlist, but I don’t feel I have found a satisfying solution yet (for the syntax).