Search code examples
emacscomments

Change emacs comment markers from ;; to //


When I am using Emacs to edit a ".s" file, I want to change the comment from ";;" to "//". I can't seem to find out how to change the comment identifiers?

For example, when I comment-region.

More information: I appear to be in ASM-MODE which is the default mode for editing assembler files. I made sure that I was in asm-mode by

 (setq auto-mode-alist                                                                                                 
       (append '(("\\.s$"   . asm-mode)auto-mode-alist))

Because assembly programs typically use ; as the comment indicator, asm-mode uses this. However, for some reason I cannot figure the GNU assembler (GNU Binutils for Raspbian) 2.35.2 uses // or @ or # for comments NOT a ;

Therefore, I would like to change the behaviour such that when I select a region and M-X comment-region it uses either // or @ for comments. I cannot use the default comment character, I need to change it to either double-slashes // or an at symbol @

The question is really how do I go about changing the default comment character for in a mode?


Solution

  • Assuming that the major mode of the .s file is asm-mode, you can use the mode hook to tweak the comment start string:

    (defun my/asm-comment-tweak ()
      (setq-local comment-start "// "))
    
    (eval-after-load "asm"
      (add-hook 'asm-mode-hook #'my/asm-comment-tweak))
    

    Adding the above to your init file should let you open a .s file, which will be in asm-mode. The last thing that is done by asm-mode is to run the mode hook which will call the function my/asm-comment-tweak: the function will set the buffer-local variable comment-start to the string you specified.

    This pattern is very common: many problems in customizing emacs are solved in exactly the same way. You define a function that tweaks a variable and you arrange for the function to be called by the appropriate mode hook.