如何使Emacs自动缩进我的C代码?

时间:2023-01-14 14:05:49

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 get into that habit. I just can't. I need to hit return, and I hate re-tabbing every time.

我刚开始对emacs有感觉,但是当我按回车键的时候,我就会对它不缩进的趋势感到沮丧。我知道如果我按下C-j,它就会这么做,但我不能养成这个习惯。我只是不能。我需要按回车键,每次我都不喜欢重复。

I went into the options and found C mode hook, and C++ mode hook, etc--and they defined two keymappings (10 and 13, and I remembered that 0A and 0D are CR/LF because I used them a lot in assembly)--I figured since one said "(lambda nil (define-key lisp-mode-map [13] (quote newline-and-indent))" and the other the same but with (quote newline) instead, I just put the -and-indent at the end of it and figured it would work.

我走进的选项,发现C模式钩,钩和c++模式等,他们定义了两个键盘映射(10 - 13,我记得0和0 d CR / LF因为我使用他们在组装),我想从一个说:“(λnil(定义关键lisp-mode-map[13](引用newline-and-indent))”和其他相同但(引用换行)相反,我只是把年底和缩进它,认为它是可行的。

But it doesn't: I set and saved it, tried, to no avail. Restarted emacs, still no success. How do I make it indent my code? It's terribly insane that emacs requires a degree in lisp just to configure it for your basic needs.

但它没有:我设置并保存它,尝试,但没有用。重启emacs,仍然没有成功。我如何使它缩进我的代码?emacs需要一个lisp的学位,只是为了满足您的基本需求,这是非常疯狂的。

Also, as a sort of side question: how do I copy to and paste from the clipboard? Killing/yanking is handy and all but I hate going edit->copy every time I want to paste to somewhere else.

另外,作为一个附带问题:我如何从剪贴板复制和粘贴?杀死/yanking是很方便的,但是我讨厌编辑->拷贝每次我想粘贴到别的地方。

-- EDIT --

——编辑

okay, I put the following lines into my .emacs and it worked:

好的,我把下面的几行写进了。emacs中,它起作用了:

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

thanks for the help

谢谢你的帮助

7 个解决方案

#1


14  

Have a look at emacswiki - autoindent

看一下emacswiki - autoindent ?

As suggested there, put following code in your .emacs

正如所建议的,在您的.emacs中添加以下代码。

(defun set-newline-and-indent ()
  (local-set-key (kbd "RET") 'newline-and-indent))
(add-hook 'c-mode '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.

您可以自定义变量C默认样式。在您的emacs中选择->定制emacs ->特定选项,然后键入c-default-style并设置为您的选择。这样做,您不需要按TAB键。从一行开始,然后按“;”,它会自动缩进。

Hope this helps

希望这有助于

#2


17  

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

我将返回键设置为在我的~/中作为一个新的行和意图。emacs文件:

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

Works for me.

为我工作。

#3


6  

In addition to the other suggestions, try C-c C-a to toggle the auto-newline feature so that your modeline reads C/la. Then you don't even need to press Enter most of the time; instead, when you type a semicolon or a brace, newlines get inserted and your code gets indented. Sometimes you do need to press Enter, and you don't get autoindentation (unless you rebind RET, as suggested in another answer) but if you just start typing the next line anyway, you may notice that it gets indented properly when you type some punctuation character.

除了其他建议,尝试C- C C-a来切换自动换行功能,这样你的modeline就会读取C/la。你甚至不需要在大部分时间里按回车键;相反,当您键入分号或括号时,会插入新行,代码会缩进。有时,您需要按Enter键,并且您不会获得自动缩进(除非您重新绑定RET,就像在另一个答案中所建议的那样),但是如果您只是开始键入下一行,您可能会注意到,当键入一些标点字符时,它会得到适当的缩进。

To get started with your "Lisp degree", type C-h i for the Info browser and go to the node for "CC Mode" (it is separate from the node for Emacs; type d for Directory and find CC Mode there).

要开始使用您的“Lisp学位”,在Info浏览器中键入C-h i,然后到“CC模式”的节点(它与Emacs的节点分离;d类型的目录,在那里找到CC模式。

#4


5  

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

如果你想缩进整个c/c++文件,你就可以。

  1. mark everything: CTRL-x h
  2. 马克一切:CTRL-x h
  3. press TAB
  4. 按TAB键

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

这也适用于xml、python代码和其他类型。

#5


1  

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

Try [enter], (kbd“enter”)或(read-kbd-macro“enter”),它们都有细微的不同之处;

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.

而且,对我来说,复制/粘贴只是工作。你能试着运行emacs -q,看看效果如何?如果是的话,这可能是你的。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 ;)

但那可能会有别的作用;

#6


0  

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

复制:移动到开始,CTRL-space,移动到end, ALT-W。

pasting: CTRL-y

ctrl - y粘贴:

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

在unix上:鼠标左键复制,鼠标右键粘贴。

#7


0  

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:

为了使它自动生效,请在您的.emacs中说:

(electric-indent-mode)

This will be the default in Emacs 24.4.

这将是Emacs 24.4中的缺省值。

#1


14  

Have a look at emacswiki - autoindent

看一下emacswiki - autoindent ?

As suggested there, put following code in your .emacs

正如所建议的,在您的.emacs中添加以下代码。

(defun set-newline-and-indent ()
  (local-set-key (kbd "RET") 'newline-and-indent))
(add-hook 'c-mode '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.

您可以自定义变量C默认样式。在您的emacs中选择->定制emacs ->特定选项,然后键入c-default-style并设置为您的选择。这样做,您不需要按TAB键。从一行开始,然后按“;”,它会自动缩进。

Hope this helps

希望这有助于

#2


17  

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

我将返回键设置为在我的~/中作为一个新的行和意图。emacs文件:

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

Works for me.

为我工作。

#3


6  

In addition to the other suggestions, try C-c C-a to toggle the auto-newline feature so that your modeline reads C/la. Then you don't even need to press Enter most of the time; instead, when you type a semicolon or a brace, newlines get inserted and your code gets indented. Sometimes you do need to press Enter, and you don't get autoindentation (unless you rebind RET, as suggested in another answer) but if you just start typing the next line anyway, you may notice that it gets indented properly when you type some punctuation character.

除了其他建议,尝试C- C C-a来切换自动换行功能,这样你的modeline就会读取C/la。你甚至不需要在大部分时间里按回车键;相反,当您键入分号或括号时,会插入新行,代码会缩进。有时,您需要按Enter键,并且您不会获得自动缩进(除非您重新绑定RET,就像在另一个答案中所建议的那样),但是如果您只是开始键入下一行,您可能会注意到,当键入一些标点字符时,它会得到适当的缩进。

To get started with your "Lisp degree", type C-h i for the Info browser and go to the node for "CC Mode" (it is separate from the node for Emacs; type d for Directory and find CC Mode there).

要开始使用您的“Lisp学位”,在Info浏览器中键入C-h i,然后到“CC模式”的节点(它与Emacs的节点分离;d类型的目录,在那里找到CC模式。

#4


5  

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

如果你想缩进整个c/c++文件,你就可以。

  1. mark everything: CTRL-x h
  2. 马克一切:CTRL-x h
  3. press TAB
  4. 按TAB键

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

这也适用于xml、python代码和其他类型。

#5


1  

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

Try [enter], (kbd“enter”)或(read-kbd-macro“enter”),它们都有细微的不同之处;

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.

而且,对我来说,复制/粘贴只是工作。你能试着运行emacs -q,看看效果如何?如果是的话,这可能是你的。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 ;)

但那可能会有别的作用;

#6


0  

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

复制:移动到开始,CTRL-space,移动到end, ALT-W。

pasting: CTRL-y

ctrl - y粘贴:

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

在unix上:鼠标左键复制,鼠标右键粘贴。

#7


0  

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:

为了使它自动生效,请在您的.emacs中说:

(electric-indent-mode)

This will be the default in Emacs 24.4.

这将是Emacs 24.4中的缺省值。