I want to run a bash command on writing a .less file, saving it and generating the corresponding .css file with the same name and in the same directory. I do have the bash command which generates the .css file.

I'm not used to vimscript except for working with vim and changing some stuff in my .vimrc, so I'm not sure how to get this to work.

To sum it up:

  • How do I run a bash command on :w?
  • How do I get the current file directory (vimscript or bash)?
  • How do I get the current file name (vimscript or bash)?

1 Answer 1


You can use autocommands to run things automatically when certain events occur. In your case, you could do this:

autocmd BufWritePost *.less !less <afile>

This autocommand runs every time a buffer with a name that ends in .less is written. The command that is being run is a bash command, and <afile> is the name of the file that is being written. Essentially, the ! command allows you to run bash commands in vim.

Relevant help topics:

  • :help :autocmd
  • :help BufWritePost
  • :help :!
  • :help <afile>


Vim doesn't automatically refresh the screen when a bash command is run. See the below answer for information on that: Suppress output from a vim autocomand

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.