In my .vimrc, I want to execute a bash file test.sh when I save a specific file, say test.txt.

However, both files are in /long/list/of/folders/which/makes/an/ugly/looking/line/.

Hence I would like to have:

let fold="/long/list/of/folders/which/makes/an/ugly/looking/line/"
autocmd BufWritePost fold."test.tex" execute "!".fold."test.sh"

Or that second line won't work because fold."test.tex" is supposed to be a pattern matcher and not a vim variable.

Is there any way to achieve my desired result ?

1 Answer 1


If I’m interpreting your question correctly, you want to use the value of the variable at the time that you define the autocommand, so you should be able just to move your execute further out:

execute 'autocmd BufWritePost '.fold.'test.tex !'.fold.'test.sh'

If instead you want to use the value at the time the autocommand is run, you’ll need to fire the autocommand for every file but then manually check the buffer name as part of your action and exit immediately if it’s not the matching file.

  • That's exactly it, I just wasn't familiar enough with the execute command to think about that ! Thanks ! Nov 1, 2018 at 7:45

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.