问题
Say I have the following python array literal:
def f():
arr = [
1,
2,
3
]
I want to delete everything in the brackets so that it becomes this:
def f():
arr = []
How can I do that with minimal commands in vim?
These are some of my attempts:
Using
di]will delete the text, but not the empty newlines, leaving a lot of whitespace I'd have to delete:def f(): arr = [ ]Using
da]will delete the newlines, but also the brackets:def f(): arr =
回答1:
You can simply do:
ca[[]<Esc>
or:
ca][]<Esc>
See :help text-objects.
回答2:
With your cursor on the first opening bracket ([), press V, followed by %. This will select the block which you then can join J, followed by di[.
回答3:
Select the lines in visual mode (v) and use J to remove newlines. Then use di[.
Or if there are many lines, di[ first, after which you move the cursor to top line and then J. This will potentially leave a space between the brackets which has to be removed with x.
回答4:
I find using a code formatter shortcut saves a lot of time
If you install vim prettier, you could do di[ <leader>p
Using the formatter in JS for example means I don't have to remove extra spacing, jump to end of line to insert ;, or fix indenting etc
来源:https://stackoverflow.com/questions/40209074/delete-everything-between-two-brackets-in-vim-including-newlines