Search code examples
vim

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


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?


Solution

  • 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.