Vim: How to delete to closing square bracket in nested block

Viewed 100

I have a long nested list in Python:

[{'name': 'John', 'args': [[1], [165], [22]]},
{'name': 'Tom', 'args': [[2], [180], [28]]},
...
{'name': 'James', 'args': [[143], [174], [45]]}, # Delete from here on
...
{'name': 'Ron', 'args': [[298], [199], [38]]}]

and want delete from somewhere in the middle until the closing bracket. If I had a similar nested block with round brackets or braces, I could use d]) or d]}. That works as expected, but d]] not. Unfortunately f, t are also no good options, because of the nested structure.

2 Answers

In this particular case, you could do:

d/}]

(and hit enter) to delete until the search pattern: }]

In general, using search as a motion is my goto when I can't quickly see a built in motion/text object situation. You can even replicate the f/t distinction by using the {pattern}/{offset} functionality. E.g.

/{pattern}/s-

will move the cursor to just before the start of {pattern}.

:help search-offset

Related