如何使Emacs自动缩进我的C代码? [英] How do I make Emacs auto-indent my C code?

查看:101
本文介绍了如何使Emacs自动缩进我的C代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始感受到emacs,但是当我按下返回键时,我很失望。我知道如果我按C-j它会做,但我不能陷入这种习惯。可新新旗新新新旗新新旗新新旗新新旗新新旗新新旗旗新新旗新新200新新我需要返回,我讨厌每次重新绑定。

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.

我进入选项,发现C模式挂钩,C ++模式挂钩等 - 和他们定义了两个关键字映射(10和13,我记得0A和0D是CR / LF,因为我在组装中使用了很多) - 我认为,因为一个说(lambda nil(define-key lisp-mode-map [13](引用newline-and-indent)),而另一个相同,但是与(报价换行符)相反我只是把它和缩进结束了,并且认为它会奏效。

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.

但是它不会:我设置并保存它,尝试,无法重新启动emacs,仍然没有成功我如何使其缩进我的代码?这是非常疯狂的,emacs需要一个学位在lisp只是为了配置它的基本需求。

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.

另外,作为一个侧面的问题:我如何复制和粘贴从剪贴板?杀死/打屁股是方便的,除了我讨厌去编辑 - >每次我想要复制粘贴到其他地方。

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.

- 编辑 -

好的,我将以下行放入我的.emacs,它的工作原理是:

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

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

感谢您的帮助

推荐答案

查看 emacswiki - autoindent

根据建议,将.emacs中的以下代码

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 'set-newline-and-indent)

您可以自定义变量C默认样式。在您的emacs中,选择 - >自定义Emacs->特定选项,然后键入c-default-style并设置为您的选择。通过这样做,你不需要打TAB。从行的开头输入,当你点击; ,它将自动缩进。

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.

希望这有帮助

这篇关于如何使Emacs自动缩进我的C代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆