In its more recent versions, shell-script mode tailors its behavior in response to the sh-shell variable's value, which indicates the shell variant (zsh, bash, etc.) that the script is for.

I've written this simple snippet (for shell-script mode) that uses the value of sh-shell:

# -*- mode: snippet -*-
# name: shebang
# key: shebang
# --
#!/usr/bin/env ${1:`(symbol-name sh-shell)`}
# -*- mode: shell-script ; sh-shell: $1 -*-

After one types shebang<TAB>, and assuming that sh-shell is set to bash, the following text is inserted

#!/usr/bin/env bash
# -*- mode: shell-script ; sh-shell: bash -*-

...with the cursor positioned at the beginning of the first bash.

At this point one has the option to type the name of a different shell, which would replace both occurrences of bash.

Is there a way to modify the snippet so that, once the value of the $1 field is finalized, this value is stored in sh-shell? In this way the snippet would serve, in addition to inserting some text, as a way to re-set sh-shell to a new value.

I know that I can always run M-x normal-mode after updating the $1 field, to have Emacs read the new buffer mode settings from the -*- ... -*- line, but it'd be nice to get the same end-result automatically once the snippet is finalized.

  • 1
    Should be doable with a transform using ` yas-moving-away-p` – npostavs Jan 26 '17 at 16:03

Your Answer

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

Browse other questions tagged or ask your own question.