Sometimes I want to have temporary comments fully left justified on a line (//) or a block of lines /* */. However, CC Mode overrides this by auto-indenting upon typing the second key. In general, I like auto-indent for keywords, etc, but I would prefer it to be disabled for comments. (update: ie. I want to disable the way comment indentation is triggered by the c-electric- key-bindings, but comments should still indent normally othewise)
I’ve tried putting these lines in .emacs, but it doesn’t prevent the behaviour.
(c-electric-slash nil)
(c-electric-star nil)
Short answer:
Here’s how I go about it:
Find out the function bound to
/:C-h k /It says “/ runs the command c-electric-slash, which is an interactive compiled Lisp
function in ‘cc-cmds.el'”.
(If you don’t see the link to
cc-cmds.el, then you don’t have the elisp sources installed. Assuming you’re not on Windows, you can use your system’s package manager to install theemacs-elpackage and try again.)Follow that link to open
cc-cmds.el. Searching forc-electric-slashdoesn’t find anything other than the function definition, so the keys aren’t bound in this file. Searching incc-mode.elfrom this directory reveals:Now we know the name of the “keymap” in which to override the
/keybinding.If you add something like this to your init file, you’ll probably get an error on startup:
…because your init file is loaded before cc-mode.el is, and
c-mode-base-mapis undefined. So we useeval-after-load(as at the top of my answer). The first argument,'cc-mode, has to match theprovidestatement at the very end of cc-mode.el. If you don’t know what theprognmeans, doC-h f progn.If you like this style of learning/discovering Emacs, you might consider reading my “How to learn Emacs”.