创建一个Vim函数来为所有行添加前缀

cha*_*lax 3 vim

我想创建一个Vim函数,为所有选定的行添加一些文本(比使用C-V I等更快).

我没有脚本编写的经验,并且找到了这个很棒的文档:http: //www.ibm.com/developerworks/linux/library/l-vim-script-1/index.html这个问题:我怎样才能在文本中添加文本在Vim的多行中间线?

我想我会input用来获取文本前缀,然后:'<,'>s/^/prefix_text/用来做实际的前缀,但我不知道如何prefix_text在替换表达式中提供变量.

我尝试了这个非常天真的解决方案(显然,它不起作用,因为它input("Enter prefix text: ")仅附加到当前行):

" Prefix lines
command PrefixLines call <SID>PrefixLines()

function! <SID>PrefixLines()
    '<,'>substitute/^/input("Enter prefix text: ")/
endfunction
Run Code Online (Sandbox Code Playgroud)

谢谢你的帮助!

ib.*_*ib. 5

下面的Vim脚本实现了问题中描述之后的命令.

command! -range -bar Prepend <line1>,<line2>call PrefixLines()
function! PrefixLines() range
    call inputsave()
    let t = input('Prefix: ')
    call inputrestore()
    exe a:firstline.','.a:lastline 's/^/\=t'
endfunction
Run Code Online (Sandbox Code Playgroud)