I would like to create a Vim function to prefix all selected lines with some text (it's quicker than using Ctrl-VI, etc.).
I have no experience in scripting and found this great piece of documentation and this question:
- Scripting the Vim editor, Part 1: Variables, values, and expressions – IBM Developer,
- How can I insert text in the middle of the line to multiple lines in Vim?.
I guess, I will be using the input
function to get the text to prefix with, and then will use the :'<,'>s/^/‹prefix_text›/
command to do the actual prefixing, but I have no idea about how to provide that ‹prefix_text›
as a variable to be plugged into a substitute expression.
I tried this very naive solution (which, evidently, does not work because it appends input("Enter prefix text: ")
only to the current line):
" Prefix lines
command PrefixLines call <SID>PrefixLines()
function! <SID>PrefixLines()
'<,'>substitute/^/input("Enter prefix text: ")/
endfunction
Thanks for your help!