How do I make Emacs auto-indent my C code?

后端 未结 7 1084
一向
一向 2021-01-31 18:53

I\'m just starting to get a feel for emacs, but I am frustrated with it\'s tendency to not indent when I press the return key. I know if I press C-j it will do it, but I can\'t

相关标签:
7条回答
  • 2021-01-31 19:17

    copying: move to start, CTRL-space, move to end, ALT-W

    pasting: CTRL-y

    on unix: left mouse to copy, right mouse to paste

    0 讨论(0)
  • 2021-01-31 19:20

    I set the return key to globally act as a new-line-and-intent in my ~/.emacs file:

    (global-set-key "\C-m" 'newline-and-indent)
    

    Works for me.

    0 讨论(0)
  • 2021-01-31 19:21

    Try [enter], (kbd "enter") or (read-kbd-macro "enter"), they all do subtly different things ;)

    Also, for me copy/pasting just works. Can you try running emacs -q, and see if it works? If it does, it may be a problem with your .emacs.

    I also have:

    (when window-system
      ;; use extended compound-text coding for X clipboard
      (set-selection-coding-system 'compound-text-with-extensions))
    

    but that might do something else ;)

    0 讨论(0)
  • 2021-01-31 19:22

    Have a look at emacswiki - autoindent

    As suggested there, put following code in your .emacs

    (defun set-newline-and-indent ()
      (local-set-key (kbd "RET") 'newline-and-indent))
    (add-hook 'c-mode-hook 'set-newline-and-indent)
    

    You can customize the variable C Default Styl. In your emacs go to Options->Customize Emacs->Specific Option, then type c-default-style and set to your choice. By doing this, you don't need to hit TAB. Type from start of the line and as you hit ";" , it will be automatically indented.

    Hope this helps

    0 讨论(0)
  • 2021-01-31 19:32

    The easy way to do that is to use electric-indent-mode. If you want to enable it manually, just do

    M-x electric-indent-mode
    

    In order to enable it automatically, say the following in your .emacs:

    (electric-indent-mode)
    

    This will be the default in Emacs 24.4.

    0 讨论(0)
  • 2021-01-31 19:35

    If you want to indent the whole c/c++ file, you just do

    1. mark everything: CTRL-x h
    2. press TAB

    That, btw, works also for xml, python code and other types.

    0 讨论(0)
提交回复
热议问题