Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 231
Jul3k Avatar asked Oct 26 '25 05:10

Jul3k


2 Answers

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.

like image 62
Ingo Karkat Avatar answered Oct 28 '25 19:10

Ingo Karkat


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
like image 41
msvalkon Avatar answered Oct 28 '25 17:10

msvalkon