2

So I've decided to try emacs, and I find it really good. However, I'm mostly writing Python, so the indentation is a crucial thing for me. After couple of failures in configuring I've finally reached to the point when I press ENTER, it goes to new line and automatically indents it (with TAB - size 4). Okay. But lets say I've got a function and a loop inside it. If I want emacs to indent a for loop it does it not in a proper way (for me):

int main() { for (int i=0; i<=10; i++) { printf("lol\n"); } } 

cuz I want it to be:

int main() { for (int i=0; i<=10; i++) { printf("lol\n"); } } 

My .emacs file looks like this:

(load-theme `wombat) (setq-default standard-indent 4) (mouse-wheel-mode t) (setq make-backup-files nil) (line-number-mode 1) (setq indent-size 4) (setq-default tab-width 4) (setq-default indent-tabs-mode t) (setq-default indent-line-function 'insert-tab) (defvaralias 'c-basic-offset 'tab-width) (defvaralias 'cperl-indent-level 'tab-width) (require 'package) (add-to-list 'package-archives '("melpa" . "http://melpa.milkbox.net/packages/") t) 

Is there a way I can configure this, to make it like I want it to be?

5
  • You say you're mostly writing Python, but your examples look like they're from C++. Which is it? Commented Sep 22, 2016 at 3:50
  • Yes, it's C++, I just copied this, cuz I have a work to do for my college holiday project, and they force C++ Commented Sep 22, 2016 at 3:51
  • I think you want to set your c-indentation-style to something else like "linux" (instead of the default "gnu"), via c-set-style. It's been a while since I played with this, so I'll leave it to someone else to answer. It's documented somewhere in the cc-mode manual. Commented Sep 22, 2016 at 3:58
  • 3
    Read this: gnu.org/software/emacs/manual/html_node/emacs/… Commented Sep 22, 2016 at 3:59
  • You can use C-c C-s to check the syntax as it appears to emacs. Then you can use the function c-set-offset to change the indentation for that bit of syntax. Commented Sep 22, 2016 at 15:09

1 Answer 1

2

Yes. I met this problem before. Here, I recommend you to install a package named google-c-style. After finishing this, the only thing you should do is that put config below into your own init-file.

(require 'google-c-style) (defun my-c-mode-hook () (google-set-c-style) (setq c-basic-offset 4 indent-tabs-mode t default-tab-width 4)) (add-hook 'c-mode-hook 'my-c-mode-hook) (add-hook 'c++-mode-hook 'my-c-mode-hook)

It's my own config.

Additionally, I'm new for emacs two. It's so difficult to find a question that I can help. Thanks for your question!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.