7

I'm having the same problem as asked in this question: emacs is dropping into shell-script mode, but for the wrong shell variant.

Using a shebang as suggested in that question's answer is semantically incorrect: most of my files are meant to be sourced, rather than run. The same applies for adding file extensions.

Normally, I use the mode file-local variable:

# -*- mode: shell-script; -*-

but this always drops me into shell-script[bash] mode. Is there any way to force tcsh, csh, bash, or sh modes with a file-local variable?

Minor sub-question: is there a specific name for modes that distinguish between specific shell flavors? Not knowing the right verbiage makes this tough to Google.

4
  • 1
    If shell-script mode is the same as sh-mode, then, I think, the variable you are looking for is sh-shell-file.
    – wvxvw
    Commented Jul 12, 2018 at 5:38
  • @wvxvw Do you know what the variable value would be? Your comment also helped me dig up more info; I added it to the question.
    – drmuelr
    Commented Jul 12, 2018 at 15:35
  • I'd need to try it, but from looking at the code, it seems like the kind of shell is decided by file's extension, so, maybe use the name of the file + .tcsh?
    – wvxvw
    Commented Jul 12, 2018 at 21:49
  • Please move your Update to an answer and accept it. Otherwise this question will be bumped to the homepage again and again. Thanks in advance.
    – Tobias
    Commented Jul 13, 2018 at 7:25

2 Answers 2

6

C-hf sh-mode says:

This mode adapts to the variations between shells (see sh-set-shell) by means of an inheritance based feature lookup (see sh-feature). This mechanism applies to all variables (including skeletons) that pertain to shell-specific features. Shell script files can use the sh-shell local variable to indicate the shell variant to be used for the file.

This means that you can use file-local variable settings. E.g. you can put the following on the first line of the file:

# -*- mode: sh; sh-shell: tcsh; -*-

or the following at the end of your file:

# Local Variables:
# mode: sh
# sh-shell: tcsh
# End:

If Emacs already knew that it should be using sh-mode for your file then you can omit the mode spec.

1
  • This is exactly what I needed! Thanks for unearthing this question!
    – drmuelr
    Commented Aug 13, 2019 at 18:13
0

This stack overflow answer to a similar question offers the following workaround:

If you can't have a shebang line, but want the correct shell type to be selected, the only way to do this is with an eval in the mode line or local variables section. Adding this will generate a confirmation prompt every time the file is loaded into Emacs, so this is not generally recommended, but may be acceptable in some cases. The mode line would be

-*- mode: sh; eval: (sh-set-shell "tcsh") -*-

However, there is an option in the subsequent prompt to permanently mark the value as safe. Thus, this is at least a workaround to the original problem, though it doesn't achieve the desired effect using variables, and it's a little on the kludge side.

The original answer is ~4 years old, so there may be a way to do this by now.

Credit to wvxvw for giving me the search seed to find this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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