Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to automatically execute a shell command after saving a file in Vim?

Tags:

vim

autocmd

This is because I'd like to automatically run tests after each file save.

I have looked at autocmd and BufWritePost but cannot make it work.

like image 363
Running Turtle Avatar asked Jan 07 '11 16:01

Running Turtle


People also ask

How do you run Unix commands while in a Vim session?

Go to command mode Esc , then run :! unix_command . Anything run from the : prompt starting with a bang ! will be run as a unix shell command. You'll be shown the output and allowed to hit a key to get back to your work in vim.

What is Autocmd in Vim?

Introduction *autocmd-intro* You can specify commands to be executed automatically when reading or writing a file, when entering or leaving a buffer or window, and when exiting Vim. For example, you can create an autocommand to set the 'cindent' option for files matching *.c.


1 Answers

This runs run_tests.sh after any file is saved, with the current filename as the only parameter:

:autocmd BufWritePost * !run_tests.sh <afile> 

View the auto-command with:

:autocmd BufWritePost * 

And remove all auto-commands from the previous with:

:autocmd! BufWritePost * 
like image 57
Alex Howell Avatar answered Sep 27 '22 16:09

Alex Howell