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 ?


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 ! – Robin Vogel Nov 1 '18 at 7:45

Your Answer

