In my makefiles, I prefer the following indentation for continuation lines:

FILES:=                \
    file1.cpp          \
    file2.cpp          \

LIBS:=                 \
    libsth1.so         \

still, makefile mode indents it in the following way (when asked to reindent file or region):

FILES:=            \
file1.cpp          \
file2.cpp          \

LIBS:=             \
libsth1.so         \

Is it possible to configure it somehow to use the former variant (= indent continuation lines by 4 spaces or tab) ?

  • What about indent-according-to-mode command? Nov 6, 2014 at 22:24

3 Answers 3


Building on purple_arrows' solution:

(defun my-makefile-indent-line ()
(forward-line 0)
 ;; keep TABs
 ((looking-at "\t")
 ;; indent continuation lines to 4
 ((and (not (bobp))
       (= (char-before (1- (point))) ?\\))
  (indent-to 4))
 ;; delete all other leading whitespace
 ((looking-at "\\s-+")
  (replace-match "")))))

(add-hook 'makefile-mode-hook
      (lambda ()
    (setq-local indent-line-function 'my-makefile-indent-line)))
  • The only problem is that this will not work if your file list is indented by TABs, because my code leaves those untouched. Nov 17, 2014 at 11:06
  • fix the indentation to 4 is not a good solution, what if the FILES and LIBS are long, and there is one element after :=, and you better align the following elements with the first one after := ?
    – CodyChan
    Apr 29, 2015 at 2:22
  • Four spaces is what the question asked. May 15, 2015 at 19:46

Yes. It is possible to configure it, somehow.

(With apologies for the snark.)

Write a function that indents a line the way you want, then set that function as the value of the variable indent-line-function for makefile-mode. Something like:

(defun my-makefile-indent-line ()

(add-hook 'makefile-mode-hook (lambda () (setq-local indent-line-function 'my-makefile-indent-line)))
  • Well, those 3 dots are sth I am not sure how to fill... But thanks for partial pointer.
    – Mekk
    Nov 7, 2014 at 9:23

In case you're using aggressive-indent-mode, it helped me to put makefile-mode onto the list of aggressive-indent-excluded-modes:

(add-to-list 'aggressive-indent-excluded-modes 'makefile-mode)

Note that this only works with global-aggressive-indent-mode turned on.

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.