1

I am editting typescript now, and I'd like to be able to have it so, whenever :w is invoked, the shell runs tsc compiling my new save.

This beats having to !tsc every time.

Does something like this exist? A build or toolchain command is also an acceptable answer, as long as it can be done in one short command.

5

Does something like this exist?

In Vim this is called an autocommand. The relevant help topic can be found by typing :h autocommand (sic!)

An example code to put into vimrc:

augroup typescript_save | au!
    autocmd BufWritePost *.ts !tsc <afile>:p:S
augroup end
  • :S does not escape characters which are special on Vim's command-line such as % or #. You probably want shellescape() and pass it a second non-zero argument: exe '!tsc '..shellescape(expand('<afile>:p'), 1). Or better yet, use system() instead of :!: call system('tsc '..expand('<afile>:p:S')). In that case, :S is reliable. – user938271 Dec 12 '19 at 11:38
  • @user938271 In fact, the whole problem was due to execute which does superfluous cmdline-special substitution. But execute is totally unnecessary here. – Matt Dec 12 '19 at 12:22
  • 1
    It was not :exe which performed these substitutions, but :!. But indeed, the new command is reliable because Vim doesn't process special characters after expanding <afile>. In the original command, expand() could cause an issue because it expanded <afile> before :! is processed, and the latter expands special characters such as %, #, !, <cword>, ... – user938271 Dec 12 '19 at 12:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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