0

I am building a daemon, and nodemon currently monitors a file called server.js, which is compiled from the file I am working on, server.ts

In order to compile server.ts, after saving with :w, I need to run from the shell, tsc server.ts.

What is the cleanest way for me to do this without having to exit vim, and taking into consideration that this action has to be performed about every 30 seconds while I code?

Thanks.

  • 1
    Use :term to have a shell running right in Vim. – muru Oct 9 at 2:41
  • 1
    :make for further explonation read the :he makeprg also you can just run command :!tsc % – Alex Kroll Oct 9 at 2:47
  • See also questions like "Vim save then make automatically?". – Hotschke Oct 9 at 8:01
2

The usual way to compile from Vim is to use :make. From :h :make:

:mak[e][!] [arguments]  1. All relevant |QuickFixCmdPre| autocommands are
               executed.
            2. If the 'autowrite' option is on, write any changed
               buffers
            3. An errorfile name is made from 'makeef'.  If
               'makeef' doesn't contain "##", and a file with this
               name already exists, it is deleted.
            4. The program given with the 'makeprg' option is
               started (default "make") with the optional
               [arguments] and the output is saved in the
               errorfile (for Unix it is also echoed on the
               screen).
            [...]

So, what I'd suggest is to set makeprg to tsc %:

The ":make" command executes the command given with the 'makeprg' option.
This is done by passing the command to the shell given with the 'shell'
option.  This works almost like typing

    ":!{makeprg} [arguments] {shellpipe} {errorfile}".

{makeprg} is the string given with the 'makeprg' option.  Any command can be
used, not just "make".  Characters '%' and '#' are expanded as usual on a
command-line.  You can use "%<" to insert the current file name without
extension, or "#<" to insert the alternate file name without extension, for
example: >
   :set makeprg=make\ #<.o

And then use :make to save your file and build it, instead of :w. (Or make a convenient mapping.)

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.