4 of 5
Clarified question intent in title
  • 173
  • 5

File-local variable to force specific shell for shell-script major mode

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.


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

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. So this is a solution, just not a particularly good one.

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

  • 173
  • 5