2

I often find myself in this situation:

\foo{\bar abc \baz{def} more stuff}
          ↑

and I want to change anything from the cursor position to the correct enclosing brace. In simple cases, ct} works fine, but not here, as it would yield

\foo{\bar } more stuff}
          ↑

In other simple cases, when I am happy to change the whole content of the braces, I can use text objects, i.e. ci}, to change everything correctly:

\foo{}
     ↑

But how do I get to

\foo{\bar }
          ↑

ideally somehow using text objects (i.e. i}), so that it works well in general?

3

The ninja-feet.vim plugin by Tom McDonald adds this functionality to Vim.

With it installed, add [ or ] between the operator and text object to specify which end you wish edit: press c]i} to perform the edit you describe.

  • Unfortunate that it’s a plugin, but otherwise, perfect! – Joachim Breitner Jan 3 '18 at 11:05
4

when I do c2t} it seems to work, but this is assuming you know that it's the second bracket.

  • I was wondering why someone wouldn't just use the repeat command with . so that would be ct}. – Ibn Rushd Jan 3 '18 at 10:41
  • 1
    Note that you can do c2t} (change to the destination of 2t}) or 2ct} (change twice to the destination of t}) which will change how the . command will repeat your command. In general it is better to use c2t} as Ibn Rushd said I think it is more consistent. – statox Jan 3 '18 at 10:42
  • Damn my comment was 31 seconds too late :) – statox Jan 3 '18 at 10:42
2

If you don't mind having the retype the closing } would:

c]}

do what you want? Although I think I prefer v]} so I can see what it's going to change first ...

  • Nice! This seems like the best solution for the specific example. N.B. ]} is an exclusive motion (see :help exclusive), so you won't have to re-type the closing } if you use c]}, only if you use the visual mode alternative you suggest. – Rich Jan 5 '18 at 9:52
  • Is it? Oh .. it looks as though it's highlighting the } when I use v, but not deleting it with the c ... TIL etc. Thanks :) – Will Crawford Jan 5 '18 at 14:24
  • Yes! At least when it’s braces. I will reach to the ninja feet plugin since there are text objects like t (for HTML tags) where ] does not seem to work (or does it?). – Joachim Breitner Jan 5 '18 at 18:05
0

Use the change command, using search (/) semantics: "Change until the next occurrence of right brace":

c/}
  • 1
    That does not work, as the next right brace is not the next enclosing right brace. Also, for a single character, ct} is faster than c/}. – Joachim Breitner Jan 4 '18 at 0:15

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.