0

Given the following text:

 0 : Container(
          alignment: Alignment.center,
          padding: const EdgeInsets.all(24.0),
          child: RichText(
              text: TextSpan(
                  style: TextStyle(fontStyle: FontStyle.normal),
                  text:
                      'It’s hard')),
          color: getRandomColor()),
      0 : Container(
          alignment: Alignment.center,
          padding: const EdgeInsets.all(24.0),
          child: RichText(
              text: TextSpan(
                  style: TextStyle(fontStyle: FontStyle.normal),
                  text:
                      'My dreams always start in the middle')),
          color: getRandomColor()),
      0 : Container(
          alignment: Alignment.center,
          padding: const EdgeInsets.all(24.0),
          child: RichText(
              text: TextSpan(
                  style: TextStyle(fontStyle: FontStyle.normal),
                  text:
                      'I will the privilege')),

I am trying the following using a vimscript variable to capture the "0 : Container(" while at the same time incrementing the "0":

:let a = 0 | g/ \zs\d\+\ze,$/ s//\=a/ : Container( | let a += 1

This solution was posted here, but it's for a slightly different pattern.

What am I missing in this syntax that's not letting it match?

2
  • This command g/ \zs\d\+\ze,$/ s//\=a/ : Container( | let a += 1 doesn't make sense. There are trailing characters after s//\=a/ which should be :help :s_flags but aren't. Perhaps a copy-paste error?
    – D. Ben Knoble
    Jun 1, 2023 at 19:45
  • What, exactly, are you trying to do? From the starting point you provided, what do you want the outcome to be? Are you trying to increment those 0s or is it something else?
    – romainl
    Jun 2, 2023 at 7:28

1 Answer 1

2

I see from a comment on the linked post that you are confused about where the pattern goes. Let's expand the commands from the post:

:let a = 0
:global/^"Timestamp": \zs\d\+\ze,$/ substitute//\=a/ | let a += 1

The primary pattern is the stuff between //, that is, ^"Timestamp": \zs\d\+\ze,$. This pattern is reused in :substitute thanks to the way empty patterns are synonyms for the previous pattern, which :global (in addition to other commands) sets.

So to change the pattern, you need to change the ^"Timestamp": \zs\d\+\ze,$ bit to match what you want (for example: \zs0\ze : Container(). Tip: refine by searching with / first, then do your :let a = 0 | g//s//\=a/ | let a += 1 (we can reuse the pattern again with // for :global.)

The full command becomes

:let a = 0
:global/\zs0\ze: Container(/substitute//\=a/ | let a += 1

Abbreviate as desired interactively.

5
  • Thank you for responding to this! What if the pattern I wanted to match was "0 : Container(" and I wanted to increment the 0? Can I swap around the pattern and do: global/ \zs\d\+\ze,$/ substitute//\=a/ : Container( | let a += 1 Jun 1, 2023 at 19:53
  • The command you pasted is the same broken command from your question @AdjunctProfessorFalcon. You need to change the part between the slashes of global if you want to change the matched pattern. (I'm not going to do it for you, because I think it's a good exercise for you at this point.)
    – D. Ben Knoble
    Jun 1, 2023 at 22:13
  • I don't understand your answer because you're just illustrating a solution that's the opposite of what I'm trying to do (which is to understand the correct syntax with the variable usage) if the pattern is inverted. The example you've provided is the same as your other answer to the other post. Jun 1, 2023 at 22:23
  • Obviously this works to match the pattern: g/ \d : Container( but I can't seem to insert the variable stuff in there. I've tried: let a = 10 | g// \d /s//\=a/ : Container( but then I get error like "misplaced =" Jun 1, 2023 at 22:26
  • 1
    It's not my answer. But I've pointed out what you need to change. You don't want to put anything after s//\=a/ except the | let a += 1; anything after the slash but before the bar becomes a syntax error. To change the pattern to ` 0 : Container` but change only the 0, write :let a = 1 | g/ \zs0\ze : Container(/s//\=a/ | let a += 1. This is why I recommend trying with just regular search / first, to make sure the pattern works as you want. Then, simply :let a = 1 | g//s//\=a/ | let a += 1. You keep trying to add pattern-ish things outside of where I showed @AdjunctProfessorFalcon
    – D. Ben Knoble
    Jun 2, 2023 at 13:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.