The title of this question basically says all.

Visit a new buffer, let's say 'foo'. Enter shell script text in the new buffer, let's say "#!/bin/sh".

How can I now set the mode to sh-mode and fontify the buffer accordingly, without the save buffer as foo, reload buffer sequence?


You can call M-x sh-mode directly or, having added that shebang line, you could use M-x normal-mode which will at that point recognise the shell script content and set sh-mode.

Being familiar with M-x normal-mode means you generally don't need to care what the desired mode name is. I use this frequently enough that I have (defalias 'nm 'normal-mode) so that I only need M-x nm.

Obviously if the file is named foo.sh then Emacs will pick sh-mode by default; but if you have global mode auto-insert-mode enabled, then creating a new file foo in any bin/ directory will make Emacs prompt you to treat the file as a shell script as well.

| improve this answer | |
  • ARghh! I could have sworn I tried that. It just works. Thank you! All I now need to do is make a little function that doesn't remove the linum (which I have on globally by default) when I call sh-mode like you suggested. Again -- thanks! – Roadowl Apr 2 at 22:20

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.