简体   繁体   中英

Cannot disable emacs automatically changing how lines are indented

I would like to turn off electric-indent-mode for all major modes in emacs from my .emacs file.

I have tried adding these to my .emacs file (each separately):

(electric-indent-mode -1)

(add-hook 'after-change-major-mode-hook (lambda() (electric-indent-mode -1)))

(when (fboundp 'electric-indent-mode) (electric-indent-mode -1))

(add-hook 'c-mode-common-hook
  (lambda ()
    (add-hook 'electric-indent-functions
      (lambda () 'no-indent) nil 'local)))

None have worked on my simple .cpp test file.

To be a little more clear, here is what is happening:

This:

#include <iostream>
int main() {
             std::cout << "Hello World!";
  return 0;
}

Becomes:

#include <iostream>
int main() {
  std::cout << "Hello World!";
  return 0;
}

The line with std::cout << ... re-alignes itself to have 2 spaces when I press either the second : or the ;

I want to be able to control how my files are indented by entering the characters myself, not by emacs changing them. I believe this is with disabling electric-indent-mode (hence my attempts to disable), but there may be another way.

I am using emacs 24.3.1 (x86_64-redhat-linux-gnu, GTK+ Version 3.10.9)

I think you are barking up the wrong tree. In my Emacs (which is admittedly an older version), colon is bound to c-electric-colon in C++ mode. This behaves "electrically" regardless of electric-indent-mode . Read its documentation; there seems to be a variable c-electric-flag which you can set to nil to disable this behavior.

If all else fails, and you want to keep the other features of C++ mode, rebinding the keys you don't want to behave "electrically" in that mode seems to be the way to go.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM