Replace to end of text object

In Vim, I continue to find that I want keystrokes to rewrite the rest of the parameter list. For example, in the following Python function:

def myfun(a, b=12, c=(1,2,3), d=15): pass 

I want to replace c=(1,2,3), d=15 with e=12 . The ci( key ci( allows me to replace everything inside the entire parameter list, but I found that I often want to keep some prefix of the Vim text object. In general, I would suggest that this keystroke that I am looking for would be useful in the context of replacing the final parameters function calls as well as definitions.

The desired answer to this question will apply to quoted strings, [] and other text objects. Please note that I understand everything about text objects, as indicated in " How to choose between brackets (or quotation marks or ...) in Vim? ".

Both @ pb2q and @romainl give good search shortcuts, but they require me to visually find the end of the closing block in order to develop a search that is unambiguous in terms of any other garbage that is in the block (for example, I think the nested calls function). In particular, I often find myself in this when I have a nested bracket inside the set of brackets that I want to manipulate. The answer I really want is similar to ci) or ca) , which is completely conceptually based on the nearest spanning bracketing ) and works elegantly with other nested blocks ) .

+4
source share
5 answers

There are many teams starting with ] that go to the end of some structure and respect nesting. ] ) to go to the end of the block in brackets, ] } to go to the end of the brace block, ] / to go to the end of the C comment (style). Use [ instead ] to go to the beginning, not the end.

So replace your type c=(1,2,3), d=15 c ] ) .

A complete list of these commands is listed in :help various-motions . Unfortunately, for blocks limited by parentheses, there is not a single one, because [ [ and ] ] already had a different meaning in the classic vi, and vim defined ] [ and [ ] to combine well with them.

+9
source

Create a map with this

 mavi)o`a 
  • Mark current position a
  • visually select inside () to select the full character set inside (), which is in
  • go to the other end of the selected text :h v_o for more information
  • go to previously marked position

Now you can perform an operation such as c or d ...

Edit: I still like @ Alan Curry's solution because the key sequence is short. However, this method has an advantage when working with].

I also found a simplification for my solution:

 vi)o`` 

`` moves to the last position. Therefore, you do not need to create a mark.

+4
source

ct: in this case everything will change before, but excluding : The preceding t with a numeric argument will allow you to comfortably change the nested curly braces / parsets / quotes, for example. c2t) .

If the cursor is already in the line you want to change, fc will put you in the character you want to start your change in using the commands above or use a search, for example: / c = for a more general case, i.e. if you have c characters earlier in the line or the cursor is currently in the previous line.

0
source

I hacked a small solution. It may have some cases with edges and some errors may occur, but I don't have more time to test today. I will change my mind when I have time.

 function! UntilTextObject(op) " Save marks let save_a = getpos("'a") let save_b = getpos("'b") " Read in the object type and set '< and '> to the ends of the " range of the text-object and 'a to the current position let object_type = nr2char(getchar()) execute "normal! mavi" . object_type . "\<esc>" " Adjust end position (off by one) let pos = getpos("'>") let pos[2] += 1 call setpos("'b", pos) " Operate on the range `a to `b execute "normal! `a" . a:op . "`b" " Restore marks call setpos("'a", save_a) call setpos("'b", save_b) endfunction onoremap u :call UntilTextObject(v:operator)<cr> 

This allows du) to be deleted-to-end-) -text object. Works with other text objects.

I added a few changes that make this a little more polite without stomping on your stamps. This is still a little bizarre if you give it a text object that selects nothing. I'll change my mind later and see if I can figure it out. This is intentionally commented out for demonstration purposes.

0
source

If you do not mind using the plugin, I believe that vim-ninja-feet does exactly what you are looking for.

  • c]i) will change the rest of the parameter list,
  • d[i" will delete back to the beginning of the quote and
  • z]i] puts you in insert mode at the end of the [] block.
0
source

All Articles