2

If one highlights a tab-indented line in makefile-mode and runs a comment function (eg. comment-dwim, comment-line, comment-region, etc.), the comment symbol (#) is inserted after the initial tab, like so:

target:
    # echo This "commented" line will be passed to the shell

As explained in this illuminating answer, Make does not recognise this line as a comment, and the line will be passed to the Shell to interpret on its own terms. makefile-mode helpfully highlights the leading tab to alert us.

What would be the simplest way to alter the behaviour of Emacs for Makefiles, so that for whole lines within the region the inbuilt comment functions cause the comment symbol to be added at the very start of the line, creating true Makefile comments, rather than shell comments?

1
  • Insert a new line starting with # above the region you need to comment, then select the region together with that new line.
    – shynur
    Commented Dec 27, 2023 at 13:06

1 Answer 1

4

The 'cause' of this behavior is mentioned in the docstring of e.g. comment-region.

You can simply adjust the behavior by configuring the comment-style, e.g. using:

(add-hook 'makefile-gmake-mode-hook
          (lambda ()
            (setq-local comment-style 'plain)))

Read the docstring of comment-styles for more info.

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.