search

Home  >  Q&A  >  body text

vimrc - How to configure vim to automatically execute a certain command line command when executing the :w save command?

I am writing a a.less file. I want to automatically call the lessc command to compile the file into when executing the :w save command. a.css file. The format of the lessc command is lessc a.less a.css.
How to set it up?


I don’t know why autocmd BufWritePost doesn’t work, so I have to use autocmd BufWriteCmd.

function! CompileLess()
    exec "w"
    exec "!lessc % > %:t:r.css"
endfunction

if executable("lessc")
    autocmd BufWriteCmd *.less call CompileLess()
endif
我想大声告诉你我想大声告诉你2791 days ago991

reply all(3)I'll reply

  • 高洛峰

    高洛峰2017-05-16 16:38:31

    The code is roughly as follows. You can improve it yourself in cmd. You can use system to get out the compilation result. If there is an error, it will be output, so that you can easily locate the error

    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

    reply
    0
  • PHP中文网

    PHP中文网2017-05-16 16:38:31

    I don’t know about vim, but you can use tools like gulp to monitor file updates

    reply
    0
  • PHP中文网

    PHP中文网2017-05-16 16:38:31

    Your idea of ​​solving the problem is quite strange.

    reply
    0
  • Cancelreply