GNU Emacs

26.14.3 Fortran Comments

The usual Emacs comment commands assume that a comment can follow a line of code. In Fortran 77, the standard comment syntax requires an entire line to be just a comment. Therefore, Fortran mode replaces the standard Emacs comment commands and defines some new variables.

Fortran mode can also handle the Fortran 90 comment syntax where comments start with ‘ !’ and can follow other text. Because only some Fortran 77 compilers accept this syntax, Fortran mode will not insert such comments unless you have said in advance to do so. To do this, set the variable fortran-comment-line-start to ‘ "!"’. If you use an unusual value, you may need to change fortran-comment-line-start-skip.


Align comment or insert new comment ( comment-dwim).

C-x ;

Applies to nonstandard ‘ !’ comments only ( comment-set-column).

C-c ;

Turn all lines of the region into comments, or (with argument) turn them back into real code ( fortran-comment-region).

M-; in Fortran mode runs the standard comment-dwim. This recognizes any kind of existing comment and aligns its text appropriately; if there is no existing comment, a comment is inserted and aligned. Inserting and aligning comments are not the same in Fortran mode as in other modes.

When a new comment must be inserted, if the current line is blank, a full-line comment is inserted. On a non-blank line, a nonstandard ‘ !’ comment is inserted if you have said you want to use them. Otherwise, a full-line comment is inserted on a new line before the current line.

Nonstandard ‘ !’ comments are aligned like comments in other languages, but full-line comments are different. In a standard full-line comment, the comment delimiter itself must always appear in column zero. What can be aligned is the text within the comment. You can choose from three styles of alignment by setting the variable fortran-comment-indent-style to one of these values:


Align the text at a fixed column, which is the sum of fortran-comment-line-extra-indent and the minimum statement indentation. This is the default.

The minimum indentation is fortran-minimum-statement-indent-tab for tab format continuation line style and fortran-minimum-statement-indent-fixed for fixed form style.


Align the text as if it were a line of code, but with an additional fortran-comment-line-extra-indent columns of indentation.


Don’t move text in full-line comments automatically.

In addition, you can specify the character to be used to indent within full-line comments by setting the variable fortran-comment-indent-char to the single-character string you want to use.

Compiler directive lines, or preprocessor lines, have much the same appearance as comment lines. It is important, though, that such lines never be indented at all, no matter what the value of fortran-comment-indent-style. The variable fortran-directive-re is a regular expression that specifies which lines are directives. Matching lines are never indented, and receive distinctive font-locking.

The normal Emacs comment command C-x ; ( comment-set-column) has not been redefined. If you use ‘ !’ comments, this command can be used with them. Otherwise, it is useless in Fortran mode.

The command C-c ; ( fortran-comment-region) turns all the lines of the region into comments by inserting the string ‘ c$$$’ at the front of each one. With a numeric argument, it turns the region back into live code by deleting ‘ c$$$’ from the front of each line in it. The string used for these comments can be controlled by setting the variable fortran-comment-region. Note that here we have an example of a command and a variable with the same name; these two uses of the name never conflict because in Lisp and in Emacs it is always clear from the context which one is meant.