In my .emacs i have put following for cuda highlighting:

(setq c-default-style '(("c++" . "linux")
                        (java-mode . "java")
                        (awk-mode . "awk")
                        (other . "gnu")))

(setq c-default-style "k&r"
      c-basic-offset 4)

;; Use C-Mode for CUDA
(add-to-list 'auto-mode-alist '("\\.cu\\'" . c-mode))

Indentation 4 spaces works for c-mode or c files but for cuda files indentation is set to 2 spaces.
Please help me in setting indentation for cuda files to 4 spaces. Thanks!


The "gnu" style sets c-basic-offset to 2, so that's where that's coming from.

As you have specific needs for a specific filename extension, I'd probably just define a derived mode to use with auto-mode-alist:

(define-derived-mode cuda-mode c-mode "CUDA"
  "CUDA mode."
  (setq c-basic-offset 4))

(add-to-list 'auto-mode-alist '("\\.cu\\'" . cuda-mode))

You might alternatively call c-set-style in the mode body, for some appropriate style, or even have an empty mode body and add an entry to c-default-style for the new mode, depending on which approach you think will be the easiest to maintain.

Note also that c-basic-offset is automatically buffer-local, so the following isn't doing anything useful for that variable:

(setq c-default-style "k&r"
      c-basic-offset 4)

You could use (setq-default c-basic-offset 4) if you want to, but it's probably sensible to leave the default value alone.

Lastly, ("c++" . "linux") looks to me to be invalid as an entry in the c-default-style alist. That list maps major mode symbols to style names, and "c++" is not a symbol (nor even the name of a major mode symbol). I doubt that entry is doing anything?

  • Thanks @phils for quick reply! I removed the code that I mention in the question and added you code in .emacs. Still indentation for cuda .cu files remains 2 spaces. I wonder why it is not 4 spaces indentation. Please help – skaushal Aug 9 '18 at 9:48
  • My suggested code works for me. – phils Aug 9 '18 at 11:53
  • Any suggestion to debug this issue? – skaushal Aug 9 '18 at 14:44
  • Run emacs -Q to disable non-standard libraries, and then evaluate my suggestion in the *scratch* buffer, and then test and confirm whether or not it works. – phils Aug 9 '18 at 21:16
  • If it still doesn't work under emacs -Q, then let me know which version of Emacs you're using. Confirm that the "CUDA" lighter is displayed in the mode line (or that C-h m indicates the *.cu buffer is in cuda-mode), and check what C-h v c-basic-offset shows you (when used in the *.cu buffer). – phils Aug 9 '18 at 21:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.