简体   繁体   English

无法将Enter绑定到Emacs中的'换行符和缩进!!! 很烦人

[英]Cannot get to bind Enter to 'newline-and-indent in Emacs !!! Very annoying

Cannot get to bind Enter to newline-and-indent in Emacs !!! 无法在Emacs中将Enter绑定到newline-and-indent !!! Very annoying. 很烦人。

I already tried everything on the following thread by changing 'mode' to ruby and still nothing: 通过将'mode'更改为ruby并且仍然没有,我已经尝试了以下线程中的所有内容:

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

I know that the problem is the RETURN key, since if I bind to something else, works fine. 我知道问题是RETURN键,因为如果我绑定其他东西,工作正常。 I tried [enter] , (kbd "enter") , (read-kbd-macro "enter") , (kbd "RET") 我试过[enter](kbd "enter")(read-kbd-macro "enter")(kbd "RET")


Follow-up 1. 后续行动1。

This is what I get from Ch k RET 这是我从Ch k RET得到的

RET runs the command newline, which is an interactive compiled Lisp function. RET运行命令newline,这是一个交互式编译的Lisp函数。

It is bound to RET. 它与RET绑定。

(newline &optional ARG) (换行和可选的ARG)

Insert a newline, and move to left margin of the new line if it's blank. 插入换行符,如果换行,则移动到新行的左边距。 If use-hard-newlines' is non-nil, the newline is marked with the text-property hard'. 如果use-hard-newlines' is non-nil, the newline is marked with the text-property hard'。 With ARG, insert that many newlines. 使用ARG,插入许多换行符。 Call auto-fill-function' if the current column number is greater than the value of fill-column' and ARG is nil. auto-fill-function' if the current column number is greater than the value of fill-column'并且ARG为nil, auto-fill-function' if the current column number is greater than the value of调用auto-fill-function' if the current column number is greater than the value of

I dont know what to make of it or how to figure out if it's a global or local binding that gets in the way. 我不知道该怎么做或如何弄清楚它是否是一个全局或局部绑定阻碍了它。 trying to remap Cj also doesnt work. 试图重新映射Cj也不起作用。

As a previous comment says, use Ch k (describe-key) to see what the key is bound to at the point when it's not doing what you want. 正如先前的评论所说,使用Ch k (describe-key)来查看当键没有按照你想要的那样被绑定时。 The (kbd "foo") syntax will be correct for whichever foo describe-key refers to it as. 对于foo describe-key引用它, (kbd "foo")语法将是正确的。

Chances are that you are simply not defining that key in the appropriate keymap. 您可能无法在相应的键盘映射中定义该键。

Note that major and minor mode keymaps take precedence over the global keymap, so you shouldn't necessarily be surprised if a global binding is overridden. 请注意,主要和次要模式键盘映射优先于全局键映射,因此如果覆盖全局绑定,则不一定会感到惊讶。

edit: 编辑:

Myself, I have a hook function for common behaviours for all the programming modes I use, and it includes the sort of remapping you're after. 我自己,我有一个钩子函数用于我使用的所有编程模式的常见行为,它包括你正在重新映射的那种。 The relevant part looks like this: 相关部分如下所示:

(defun my-coding-config ()
  (local-set-key (kbd "RET") (key-binding (kbd "M-j")))
  (local-set-key (kbd "<S-return>") 'newline)
  )

(mapc
 (lambda (language-mode-hook)
   (add-hook language-mode-hook 'my-coding-config))
 '(cperl-mode-hook
   css-mode-hook
   emacs-lisp-mode-hook
   ;; etc...
   ))

See Daimrod's answer for the explanation of why I'm re-binding RET to the current binding of Mj -- although I'm using comment-indent-new-line (or similar) instead of newline-and-indent (or similar), which does what I want in both comments and non-comments. 请参阅Daimrod的答案,解释为什么我将RET重新绑定到Mj的当前绑定 - 尽管我使用的是comment-indent-new-line (或类似)而不是newline-and-indent (或类似) ,在评论和非评论中做我想要的。

In Emacs 24, programming modes seem to derive from prog-mode , so you could probably (un-tested) reduce that list to prog-mode-hook plus any exceptions for third-party modes which don't yet do that. 在Emacs 24中,编程模式似乎来自prog-mode ,因此您可能 (未经测试)将该列表减少为prog-mode-hook以及尚未执行此操作的第三方模式的任何异常。

As said earlier, use Ch k Cj because Cj is the standard key to do newline-and-indent . 如前所述,使用Ch k Cj,因为Cj是执行newline-and-indent的标准键。

If you open a new file, activate ruby-mode and try the previous command you will see why it doesn't work. 如果您打开一个新文件,请激活ruby-mode并尝试上一个命令,您将看到它为什么不起作用。 Because ruby-mode doesn't have newline-and-indent but rather reindent-then-newline-and-indent . 因为ruby-mode没有newline-and-indent而是reindent-then-newline-and-indent Yes that's stupid but you can either ask to the maintener to change it, or accept it. 是的,这是愚蠢的,但你可以要求维护者改变它,或接受它。

However I suggest you to use Cj to do it because ruby-mode is not the only mode to do so, like paredit-mode which uses paredit-newline . 但是我建议你使用Cj来做,因为ruby-mode不是唯一这样做paredit-mode ,比如使用paredit-newline paredit-mode

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM