3

Is it possible to provide a default if $* is empty in a :make?

I'd like to specify :makeprg so that:

  1. :make runs swift build
  2. :make foo bar runs swift foo bar.
1

This isn't really a vim answer, but consider having an actual makefile (or a shell script, but makefiles are beautiful for this kind of thing—see the language agnostic makefile and is it worth using make).

Then, you don't need weird workarounds for vim's $* (I had to look it up to be sure of what you were talking about).

For example (pay attention to the tabs, which are showing as spaces, unfortunately):

all: build_output

build_output:
    swift build

foo_output:
    swift foo

bar_output:
    swift bar

Then you can simply make, make foo_output, or make bar_output, where *_output is a file or directory tree generated by swift *.

Let make do its job. Don't beat it to death.

The equivalent shell script would have to be something like

#! /usr/bin/env bash

set -euo pipefail

usage() {
  cat <<DOG
usage: $0 [args...]
DOG
}

die() {
  local ex="${1:-1}"
  exit "$ex"
}

usage_and_die() { usage && die; }

build() {
  swift build
}

foo() {
  swift foo
}

bar() {
  swift bar
}

main() {
  if (($# < 1)) ; then
    build
  else
    for param;
      case "$param" in
        -h|--help) usage_and_die ;;
        *) "$param" ;;
      esac
    done
  fi
}

main "$@"

And the complexity of this beast will only grow. Just use make.

Vim?

I suppose you could do something like

command -nargs=* Make if len(<q-args>) > 0 | make <q-args> | else | make build | endif
setlocal makeprg=swift

But then you have to use this new command, and maintain it, and all that garbage. Plus, if you have a workflow, why not encode that in your makefile, and let others benefit from seeing it?

|improve this answer|||||
  • I don't want a Makefile because Swift projects (with the Swift Package Manager) already have standards for building: swift build, swift test, etc. I want something that works for all projects. My default for SwiftPM projects is to run build, but I should be able to run the tests too. – mdiep Nov 24 '19 at 21:37
  • 1
    You must not have read the articles I linked. The point of the makefile is to standardize those commands for interacting with that project. It’s agnostic. But again, see the part at the end re: vim @mdiep – D. Ben Knoble Nov 24 '19 at 22:03

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.