The quickest way to delete a function argument in Vim
If I have the following code:
foo->call(bar, baz->widget);
With cursor on "b" in "baz" or on "w" in "widget", how would I quickly delete the argument at the cursor开发者_运维知识库?
foo->call(bar);
It can be any argument (first, last, or in the middle).
With my omap-param plugin, just type di,
or da,
.
It won't be bothered by other brackets. For instance, in f(a+g(42, "string")), foobar)
, from "a" to "g", da,
will just leave foobar
in f call.
For the first argument, this key-combination goes to opening parenthesis, and deletes everything up to the next comma, including the space after it:
T(df,x
For a middle argument, this key-combination goes to the previous comma, and deletes it and everything until the next comma:
F,dt,
For the last argument, this key-combination goes to the previous comma, and deletes it and everything until the closing parenthesis:
F,dt)
-- or --
You can write your own macro to do this sort of thing. A similar example is here.
targets.vim is a further plugin that provides aa
and ia
(which fixes also missing default behaviour of builtin text-objects, compare discussion on TextObjectify)
vim-argumentative is another plugin option that provides a,
and i,
text objects and more.
?,enter to move to the previous ','
d/,enter to delete to the next ','
no plugins required
you may need to prepend the search terms with a number in the case of template params:
void foo( sometype a, some_template_type< sometype, sometype > b, sometype c )
or you could press '.' however many comma seperations you want rid of.
It's difficult to make something that works consistently for the first or last cases.
Something like this will work reasonably well:
/(\)|,)
dT,
This will find the next ,
or )
character, then delete back to the character after the previous ,
. It won't work for the first parameter and won't work as intended if your parameter includes a )
or ,
, for example if there is a function call in there.
You could put this into a macro:
qd
/(\)|,)
dT,
q
Then to reuse, go to the parameter you would like to delete and run the macro:
@d
精彩评论