Let's say I have a line like this:
a = x(y(z));
And I want to change it to:
a = y(z);
What's the most efficient way to do this?
Vi and Vim Stack Exchange is a question and answer site for people using the vi and Vim families of text editors. It only takes a minute to sign up.Sign up to join this community
If you start with your cursor on the
%move to the corresponding parenthesis to the one closest to you, i.e.
``jump back to your previous jump, the starting
2xdelete the next two characters,
If you want this to be more robust you can do a find next and delete instead of the
This works when your function name is variable length.
What I use a mapping that leverages Tim Pope's surround.vim:
nmap <silent> dsf ds)db
I use the mapping
dsf to read as delete surrounding function. Execute this mapping while inside the function call or one of the parens. The mapping is supposed to be the opposite of surround.vim's surround with function mappings, e.g.
Using exchange.vim, when one of the defined regions to exchange is fully inside the other, the smaller one replaces the larger one.
So, with your cursor on the beginning of the function name, you can use
cxoperator is used in pairs: the first time defines the first region to be exchanged, and the second time defines the second region and performs the exchange. It takes any motion or text object, given after the operator.
cx%defines the first region to exchange as the text from the current cursor position (beginning of function name) to where the
%motion goes. When not on a bracket,
%scans forward in the current line until it finds one, then jumps to the matching bracket. After this command, the entire function call will be highlighted.
%moves the cursor to the closing parenthesis of the function call.
cxi(defines the second region to exchange as the inner parenthesized term (so, the function arguments). At this point, the exchange is performed.
If there is any
However, the first region would be defined as
foo[x] and the second one would fail (no matching parenthesized term).