Jul3k
Jul3k

Reputation: 1096

How to select the current parameter of a function in vim

Using VIM to code in Python I often have to change or delete the parameters of functions. I expected to find a command like ci, to select the text between two commas. Somehow like selecting between matching parentheses or brackets with ci" and ci(.

def function1(a,b,delete_me, c):
    pass

def function2(a,b,delete_me):
    pass

def function3(a,b,delete_me=[1,2,3]):
    pass

How can I achieve the expected result efficient with VIM giving the cursor points to the desired parameter already?

Upvotes: 4

Views: 2207

Answers (3)

Matthew Strawbridge
Matthew Strawbridge

Reputation: 20640

For simple parameters, ciw (change inside word) is equivalent.

In your third example, if your cursor is at the start of the parameter then cf] (change to find ]) will work.

Upvotes: 1

msvalkon
msvalkon

Reputation: 12077

I don't think there's a builtin command for this but here are some examples which might help.

Selecting text:

ve -- end of word boundary
vE -- end of line

Deleting:

de -- delete until end of word
dE -- delete until end of line

Some regex magic could also do the work, if you have to remove a bunch of identical things.
:%s/\,d.*\]//g -- Replace anything with that begins with ,d and ends in ] with ""

def function3(a,b,delete_me=[1,2,3]):
    pass

Turns into

def function3(a,b):
    pass

Upvotes: 1

Ingo Karkat
Ingo Karkat

Reputation: 172688

Parameter matching is difficult (especially with constructs like nested function calls and arrays) as it depends on the underlying syntax. That's why there's no built-in inner / outer parameter text object.

I use the argtextobj.vim plugin, which works fairly well.

Upvotes: 4

Related Questions