I am writinga.less
File, I want to be in execution:w
Called automatically when a command is savedlessc
Command to compile the file intoa.css
Documents.lessc
The format of the command islessc a.less a.css
.
How should I set it up?
Don’t know why.autocmd BufWritePost
It doesn’t work, then it only worksautocmd BufWriteCmd
Here we go.
function! CompileLess()
exec "w"
exec "! lessc % > %:t:r.css"
endfunction
if executable("lessc")
autocmd BufWriteCmd *.less call CompileLess()
endif
The general code is as follows. cmd has perfected itself. system can be used to retrieve the compilation results. If there is an error, it will be output, so as to locate the error well.
function! CompileLess() let cmd = 'lessc + ' + expand('<afile>:p:h') let output = system(cmd) if v:shell_error echohl WarningMsg | echo output endif endfunction if executable('lessc') autocmd BufWritePost *.less call CompileLess() endif