1

I have an issue that probably has been asked and answered before but I am unable to find it because I seem to be using the wrong search terms for it.

However, the issue I have is that if I make my own file, which I have my bash-aliases in e.i ~/.bash_aliases I get no syntax highlighting as I am getting in my .bashrc? When I am making a new systemd service I however get syntax/font highlighting?

1 Answer 1

2

The syntax highlighting of a file is determined by what mode or modes are active. The most common way to choose what mode to activate is based on the file name extension. python-mode will be activated for .py files, c-mode for .c files, html-mode for .html files, and so on. sh-mode is the mode that provides syntax highlighting for shell scripts, and it is primarily activated for files with the .sh extension.

However, file name extensions are not universally used. In fact, the notion that all file names should have an extension seems to come primarily from DOS and CP/M machines, where every file had a metadata field that was presented after the file name, separated from it by a period. Macintosh computers had a separate metadata field for the type that wasn't part of the name (and if I recall correctly wasn't even directly editable by the user). Unixes had no type field in the file metadata, so extensions were used in some contexts but not others. File names like .bashrc, .bash_profile and others of that style have no extension; the leading period was just a trick to hide the file from directory listings. As such, Emacs can also choose a mode for the file based on its contents, rather than on its name.

For shell scripts in particular, Emacs looks for the shebang at the top of the file to choose the mode. Your .bash_aliases file is probably sourced by your other scripts, so it doesn't really need a shebang to work. But you could add one so that Emacs recognizes it as a shell script.

You can also put information for Emacs in the first line of the file, and this is most commonly used to tell Emacs what mode to use. Emacs expects this information to have -*- characters surrounding it, so for your shell script you could do this:

# -*- Shell -*-

The # makes sure that this line is treated as a comment by your shell.

All of this (and more) is documented in the Emacs manual in chapter 23.3 Choosing modes.

4
  • The Unix stuff is somewhat revisionist history: e.g. C files always had .c and .h suffixes and make's pattern rules to deal with them date from the earliest days of its existence. But shell scripts were used as commands and did not have .sh suffixes (after all there was only one shell), just like the "real" commands (executables produces by compiling C programs) did not have a suffix like ".exe" (and they still do not in Linux).
    – NickD
    Commented Nov 12, 2020 at 13:28
  • Yes, I should fix that.
    – db48x
    Commented Nov 12, 2020 at 16:00
  • 1
    Is that better? It's a little longer, but much more correct. Hopefully not too long, considering that it's just there to segue into talking about the content of the files.
    – db48x
    Commented Nov 12, 2020 at 16:20
  • Thank you! I think it's better, so I hope others will agree too.
    – NickD
    Commented Nov 12, 2020 at 16:25

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.