如果文件包含以下文本,我想阻止Vim保存文件
:style=>
Run Code Online (Sandbox Code Playgroud)
这可能在文件中的多个位置.
作为奖励,如果它可以提出一个错误消息,如"停止内联样式!" 那也很棒;)
谢谢!
PS:我希望这可以防止在尝试写入文件时触发操作:w
这样做是为了将save(:w)命令"绑定" 到检查模式的函数:
autocmd BufWriteCmd * call CheckWrite()
Run Code Online (Sandbox Code Playgroud)
你的Check()函数看起来像这样:
function! CheckWrite()
let contents=getline(1,"$")
if match(contents,":style=>") >= 0
echohl WarningMsg | echo "stop putting styles inline!" | echohl None
else
call writefile(contents, bufname("%"))
set nomodified
endif
endfunction
Run Code Online (Sandbox Code Playgroud)
请注意,在这种情况下,您必须自己提供"保存文件"机制(可能不是一个好主意,但效果很好).
将set readonly当你的模式出现:
autocmd InsertLeave * call CheckRO()
Run Code Online (Sandbox Code Playgroud)
并在尝试保存时发出警告:
autocmd BufWritePre * call Warnme()
Run Code Online (Sandbox Code Playgroud)
在哪里CheckRO()和Warnme()将会是这样的:
function! CheckRO()
if match(getline(1,"$"),":style=>") >= 0
set ro
else
set noro
endif
endfunction
function! Warnme()
if match(getline(1,"$"),":style=>") >= 0
echohl WarningMsg | echo "stop putting styles inline!" | echohl None
endif
endfunction
Run Code Online (Sandbox Code Playgroud)
使用hi+ syntax match命令突出显示您的模式也是一个好主意:
syntax match STOPPER /:style=>/
hi STOPPER ctermbg=red
Run Code Online (Sandbox Code Playgroud)
最后,看看这个脚本.