[TxMt] Re: How can I add a sequencial number to this Find/Replace?

René Schwaiger sanssecours at f-m.fm
Fri Feb 20 14:26:58 UTC 2015


Hi jgaltUSA,

> On 20 Feb 2015, at 9:50 , jgaltUSA <jgaltUSA at gmail.com> wrote:
> 
> I am a newbie to textmate and I am trying to perform a find and replace. I want to replace the matched text with a sequential number. 
> 
> Here is my current replacement string: $1##$2 
> 
> How can I replace the ## with a sequential number? 

I do not think that what you want is possible using only regular expressions and “Find & Replace”. It is possible and not to complex using a programming language. The following code does what you want:

    #!/usr/bin/env python

    from re import compile
    from sys import stdin, stdout

    regex = compile(
        """(<PortalOBj portalFlags="16" numbOfRows="1" initialRow=")\d+(">)""")
    index = 16

    def create_subtitute_function():
        def substitute(match):
            global index
            replacement = "{}{}{}".format(match.group(1), index, match.group(2))
            index += 1
            return replacement

        return substitute

    text = regex.sub(create_subtitute_function(), stdin.read())
    stdout.write(text)

I attached a bundle which contains a command that uses the above code. To install the bundle just double click it. You can apply the substitution on some text by pressing the key combination “^⌥⌘R”. Since the above is my code I would be careful when you use the command though :).

> For example, I want the first sequential number to be 16. 
> 
> Here is a screen capture:  
> 
> Thanks!

Kind regards,
  René

P.S.: You can create a screenshot of a single window by using the key combination “⇧⌘4”. After that just hit “Space” and a click on the window you would like to make a screenshot of. This way you do not need to crop the picture.

-------------- next part --------------
A non-text attachment was scrubbed...
Name: Subsitute.tmbundle.zip
Type: application/zip
Size: 2690 bytes
Desc: not available
URL: <http://lists.macromates.com/textmate/attachments/20150220/f175ff88/attachment.zip>


More information about the textmate mailing list