如果文件包含以下文字
,我想阻止Vim保存文件:style=>
这可能位于文件的多个位置。
作为奖励,如果它可以提出一个错误消息,如“停止内联样式!”那也很棒;)
谢谢!
PS:我希望这可以防止在尝试写入文件时触发操作:w
答案 0 :(得分:7)
执行此操作是将save(:w
)命令“绑定”到检查模式的函数:
autocmd BufWriteCmd * call CheckWrite()
您的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
请注意,在这种情况下,您必须自己提供“保存文件”机制(可能不太好,但效果很好)。
将是set readonly
:
autocmd InsertLeave * call CheckRO()
并在您尝试保存时发出警告:
autocmd BufWritePre * call Warnme()
其中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
使用hi
+ syntax match
命令突出显示您的模式也是一个好主意:
syntax match STOPPER /:style=>/
hi STOPPER ctermbg=red
最后,看看this script。
答案 1 :(得分:0)
通过VCS的提交钩子强制实施这样的限制可能更为典型。例如,请参阅http://git-scm.com/docs/githooks。
这样可以保留编辑器的功能,同时禁止提交违规代码。