通过vimscript替换正则表达式

bd1hkmkf  于 5个月前  发布在  其他
关注(0)|答案(2)|浏览(87)

我想替换以下事件:

@todo call customer
@todo-next do foo
@todo-wait customer will call me back

字符串
@done当我和curser在一起的时候。例如:

@todo call customer


届时,

@done call customer


我用它来替换@todo,但对于另外两个(@todo-next,@todo-wait),我认为我的正则表达式不能正常工作。
这是我得到的,当我运行它时,线路上没有发生任何事情...:

function! Tdone(  )
    let line=getline('.')
    "let newline = substitute(line, "@todo", "@done", "")
    let newline = substitute(line, "@todo.*? ", "@done", "")
    call setline('.', newline)
    :w
endfunction

zbdgwd5y

zbdgwd5y1#

您的regexp语法错误; Vim不理解Perlish .*?;这是Vim中的.\{-}。参见:help perl-patterns。(此外,由于要匹配尾随空格,因此还需要在替换中包含该空格,或者使用\ze限制匹配。)

let newline = substitute(line, "@todo.\{-} ", "@done ", "")

字符串
我可能会对你的模式进行更多的限制,以避免错误的匹配(取决于你的标记的确切语法,所以这只是一个例子):

let newline = substitute(line, "@todo\%(-\w\+\)* ", "@done ", "")


最后,不需要getline()/setline();你也可以用:substitute来做。如果你保留这个函数,这不会破坏最后一个搜索模式。要清理历史,:call histdel('search', -1)

wnavrhmk

wnavrhmk2#

一个正则表达式可以做到这一点:

:%s/\(@todo\)\( call.*\|-next.*\|-wait.*\)/@done\2/g

字符串

相关问题