问题描述
我在编写a.less文件,我想在执行:w保存命令时自动调用lessc命令编译该文件变成a.css文件。lessc命令的格式是 lessc a.less a.css。请问该如何设置?
不知道为什么autocmd BufWritePost 不起作用,那么只有用autocmd BufWriteCmd了。
function! CompileLess() exec 'w' exec '!lessc % > %:t:r.css'endfunctionif executable('lessc') autocmd BufWriteCmd *.less call CompileLess()endif
问题解答
回答1:大概代码如下,cmd那里自己完善下,使用system可以取出编译结果,如果错误就输出,这样好定位错误
function! CompileLess() let cmd = ’lessc + ’ + expand(’<afile>:p:h’) let output = system(cmd) if v:shell_errorechohl WarningMsg | echo output endifendfunctionif executable(’lessc’) autocmd BufWritePost *.less call CompileLess()endif回答2:
vim不知道,你可以用gulp之类的工具来监听文件更新
回答3:你这解题思路够奇葩。