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.