我正在编写一个函数来缩进Vim中字符串的所有行。我正在尝试通过使用空格substitute
替换所有行首来实现这一点n
:
function! Indent(str, n)
return substitute(a:str, '\v^', repeat(' ', a:n), 'g')
endfunction
尽管我使用了g
标志,但这仅缩进第一行。我也尝试过使用\v\_^
,同样的结果。
Indent("To be or not to be\nThat is the question", 2)
# => " To be or not to be\n That is the question" (DESIRED OUTPUT)
# => " To be or not to be\nThat is the question" (ACTUAL OUTPUT)
如何修改我的正则表达式以获得所需的输出?
您可以使用split进行此操作,并相当容易地加入。
function! Indent(str, n)
let l:sep = repeat(' ', a:n)
return l:sep . join(split(a:str, "\n"), "\n".l:sep)
endfunction
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句