Emacs 如何像普通IDE一样缩进行

Emacs 如何像普通IDE一样缩进行,emacs,Emacs,Emacs不会自动为我缩进行。例如: int main() { int a; int b; // this line still don't indent with int a } 那么,如何在Emacs中缩进行呢?此外,每次我键入{,如何使Emacs像其他IDE(eclipse、netbean…)一样自动缩进选项卡?如果您只想自动缩进C代码,请将其放入.Emacs文件中: (defun enable-newline-and-indent () (local-set-key (kbd

Emacs不会自动为我缩进行。例如:

int main() {
   int a;
int b; // this line still don't indent with int a
}

那么,如何在Emacs中缩进行呢?此外,每次我键入
{
,如何使Emacs像其他IDE(eclipse、netbean…)一样自动缩进选项卡?

如果您只想自动缩进C代码,请将其放入
.Emacs
文件中:

(defun enable-newline-and-indent ()
  (local-set-key (kbd "RET") 'newline-and-indent))
(add-hook 'c-mode 'enable-newline-and-indent)
或者,您可以为所有编程模式启用此行为,例如(它仅适用于Emacs 24+):


另一种选择是使用Emacs 24中引入的
电子缩进模式和
电子布局模式-它们基本上会在一些字符后触发换行或缩进,如
{

如果您只想自动缩进C代码,请将其放入
.Emacs
文件:

(defun enable-newline-and-indent ()
  (local-set-key (kbd "RET") 'newline-and-indent))
(add-hook 'c-mode 'enable-newline-and-indent)
或者,您可以为所有编程模式启用此行为,例如(它仅适用于Emacs 24+):


另一种选择是使用Emacs 24中引入的
电子缩进模式和
电子布局模式-它们基本上会在一些字符后触发换行或缩进,如
{
Emacs的正常行为是保留RET(Enter)用于插入回车符的键,使用C-j(Ctrl+j)进行输入和缩进。这也是因为有一系列其他键可以导致行重新缩进。按照传统,导致重新缩进的命令的名称中有“电气”部分。
C-mode
有一系列“电气”命令命令。要查找这些命令,在编辑C源代码时,可以执行以下操作:

  • C-HB(Ctrl+HB)-列出缓冲区中的所有键绑定

  • M-SO(Alt PC或Option Mac+SO)-调用
    发生
    命令,或者,您可以使用M-x
    发生

  • 出现时键入“electric”
    提示您输入(在上一步之后,该点将在minibuffer中,因此只需继续键入即可)
它将打开一个额外的缓冲区,其内容类似于:

12 matches for "electric" in buffer: *Help*
    706:C-d             c-electric-delete-forward
    709:#               c-electric-pound
    710:( .. )          c-electric-paren
    711:*               c-electric-star
    712:,               c-electric-semi&comma
    713:/               c-electric-slash
    714::               c-electric-colon
    715:;               c-electric-semi&comma
    716:{               c-electric-brace
    717:}               c-electric-brace
    718:DEL             c-electric-backspace
    726:C-c C-l         c-toggle-electric-state
这列出了所有命令和分配给它们的键,它们执行一些“电子”操作。您可以将点移动到其中任何一个,然后按C-h f(Ctrl+h f)RET,它将打开帮助页面,描述该命令的具体功能。例如,如果您请求有关
C-electric-colon
的帮助,它将显示以下内容:

c-electric-colon is an interactive compiled Lisp function in
`cc-cmds.el'.

(c-electric-colon ARG)

Insert a colon.

If `c-electric-flag' is non-nil, the colon is not inside a literal and a
numeric ARG hasn't been supplied, the command performs several electric
actions:

(a) If the auto-newline feature is turned on (indicated by "/la" on
the mode line) newlines are inserted before and after the colon based on
the settings in `c-hanging-colons-alist'.

(b) Any auto-newlines are indented.  The original line is also
reindented unless `c-syntactic-indentation' is nil.

(c) If auto-newline is turned on, whitespace between two colons will be
"cleaned up" leaving a scope operator, if this action is set in
`c-cleanup-list'.

[back]

您可以从此处继续阅读手册,方法是将点移动到看起来像超链接的项目(通常它带有下划线,或具有不同的视觉外观,然后是文本的其余部分),然后点击RET。您可以在
*帮助*
模式下使用C-b和C-f在已访问的页面之间来回导航(或将点移动到
[后退]
[前进]
按钮,然后按RET)。

Emacs的正常行为是保留RET(回车)键,以便在使用C-j(Ctrl+j)时按字面插入回车符用于输入和缩进。这也是因为一系列其他键可能导致行重新缩进。按照传统,导致重新缩进的命令的名称中包含“电气”命令。
c-mode
有一系列“电气”命令。要查找这些命令,在编辑c源代码时,可以执行以下操作:

  • C-HB(Ctrl+HB)-列出缓冲区中的所有键绑定

  • M-SO(Alt PC或Option Mac+SO)-调用
    发生
    命令,或者,您可以使用M-x
    发生

  • 出现时键入“electric”
    提示您输入(在上一步之后,该点将在minibuffer中,因此只需继续键入即可)
它将打开一个额外的缓冲区,其内容类似于:

12 matches for "electric" in buffer: *Help*
    706:C-d             c-electric-delete-forward
    709:#               c-electric-pound
    710:( .. )          c-electric-paren
    711:*               c-electric-star
    712:,               c-electric-semi&comma
    713:/               c-electric-slash
    714::               c-electric-colon
    715:;               c-electric-semi&comma
    716:{               c-electric-brace
    717:}               c-electric-brace
    718:DEL             c-electric-backspace
    726:C-c C-l         c-toggle-electric-state
这列出了所有命令和分配给它们的键,它们执行一些“电子”操作。您可以将点移动到其中任何一个,然后按C-h f(Ctrl+h f)RET,它将打开帮助页面,描述该命令的具体功能。例如,如果您请求有关
C-electric-colon
的帮助,它将显示以下内容:

c-electric-colon is an interactive compiled Lisp function in
`cc-cmds.el'.

(c-electric-colon ARG)

Insert a colon.

If `c-electric-flag' is non-nil, the colon is not inside a literal and a
numeric ARG hasn't been supplied, the command performs several electric
actions:

(a) If the auto-newline feature is turned on (indicated by "/la" on
the mode line) newlines are inserted before and after the colon based on
the settings in `c-hanging-colons-alist'.

(b) Any auto-newlines are indented.  The original line is also
reindented unless `c-syntactic-indentation' is nil.

(c) If auto-newline is turned on, whitespace between two colons will be
"cleaned up" leaving a scope operator, if this action is set in
`c-cleanup-list'.

[back]

您可以从此处继续阅读手册,方法是将点移动到看起来像超链接的项目(通常它带有下划线,或具有不同的视觉外观,然后是文本的其余部分),然后点击RET。您可以在
*帮助*
模式下使用C-b和C-f在已访问的页面之间来回导航(或将点移动到
[后退]
[前进]
按钮并按RET)。

当我使用时:
M-x发生
它将显示行:
列出与regexp匹配的行(默认发生:)
,并且在“结果窗口”中,没有与缓冲区中的“电气”匹配的
,正如我在您的示例中看到的:(我执行此步骤:
M-x occure RET
。Emacs来到行:
列出匹配regxp的行
,我键入
electric
。Emacs注意事项:
搜索1缓冲区
-->
未找到匹配项
-->
您可以使用M-s o运行命令:(当我使用:
M-x occure
时,它将显示行:
列出与regexp匹配的行(默认发生:)
,并且在“结果窗口”中,缓冲区中没有与“electric”匹配的
,正如我在您的示例中看到的:(我执行此步骤:
M-x occure RET
。Emacs来到行:
列出匹配regxp的行
,我键入
electric
。Emacs注意事项:
搜索1缓冲区
-->
未找到匹配项
-->
您可以使用M-s o运行命令:(@close voter:请给我评论一下我帖子中的哪个问题?也是重复的?还是不适合
StackOverFlow
?或者很简单,第一次投票时,你会有一顶冬季帽子?⁺当然,我也想知道为什么这个问题被认为是一个离题的问题。@close voter:请给我评论一下我文章中的哪个问题?同样重复?或者不适合
StackOverFlow
?或者很简单,当第一次进行离题投票时,你将