1

In my file, I have many instances of grid[X][Y] where X and Y are variables. I would like to swap all instances to grid[Y][X] (preferably one by one so I can check them).

Examples (especially note the second example):

grid[row][col]
grid[row + 1][col]

Swap to:

grid[col][row]
grid[col][row + 1]

Note that X/Y are guarenteed to contain only A-Z, a-z, 1-9, + - (if that helps)

1

This should do what you want

:%s/\[\(.\+\)\]\[\(.\+\)\]/[\2][\1]

And yes, I guess all these toothpicks are really needed.

  • 1
    \v would eliminate some of the escapes – D. Ben Knoble Nov 17 '18 at 2:50
1

This does what you want

:%s/\v(\[.+\])(\[.+\])/\2\1/gc
    \v                         Very magic mode
      (\[.+\])                 First group of [...]
              (\[.+\])         Second group of [...]
                            g  Global flag - substitute every instance on each line
                             c Ask flag - ask before each occurence

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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