Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I pass the current line to a Vimscript function?

Tags:

vim

I am trying to create a Vim mapping that will operate on the current line, taking a string like this:

[boiled cabbage, mad donkey, elephant, very dark fudge]

And quoting all the list elements to end up with this:

["boiled cabbage", "mad donkey", "elephant", "very dark fudge"]

I tried with vim regexes, but figured it would be easier to write a function that takes the current line as an argument and returns the transformed line. I have no problem performing the transformation in vimscript. But how can I pass the current line to the function, and how do I replace the line with the transformed line?

like image 553
Sean Mackesey Avatar asked Jul 16 '12 04:07

Sean Mackesey


1 Answers

To get current line you can use

let line=getline('.')

(note: you can also do getline(10, 20) to get a list of 11 lines).

To set current line you can use

call setline('.', line)

. You can also replace a number of lines starting with current if you pass a list to this function.

like image 173
ZyX Avatar answered Oct 21 '22 00:10

ZyX