如何在 Linux 中使用 Ctrl+M 关闭替代 Enter
为什么 Ubuntu Jaunty 中 Ctrl+M 绑定到 Enter?如何关闭它?
我正在使用 Emacs,并且想将 Ctrl+M 绑定到其他命令。
Why is Ctrl+M bound to Enter in Ubuntu Jaunty? How to turn it off?
I'm using Emacs and would like to bind Ctrl+M to some other command.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我认为你的问题问反了。绑定到 Enter 的不是 Cm,而是 Enter 绑定到 Cm。而Cm与
RET
相同。如果运行 Ch k Cm,您将看到类似“
RET 运行命令...
”的内容。 Cm 发送RET
因为它是控制代码,请参阅 http://en.wikipedia.org/wiki/Control_character。Enter 键绑定到 Cm;如果您运行Ch k Enter,您将看到类似“然后该密钥将被转换为 Cm。
RET(翻译自)运行命令...
”的内容。请注意,Enter 被 emacs 解释为您要做的就是首先从中删除翻译通过将其直接绑定到当前间接绑定的内容来实现
RET
,例如(global-set-key (kbd "") 'newline)
。然后你就可以随意将 Cm 绑定到你想要的任何内容,而不会影响 Enter 键。这假设您使用的是图形化 emacs。如果您在终端中运行它,这将不起作用,因为 Enter 将发送 Cm,而不是。不过,您可以使用
window-system
变量进行检查。I think your question is backwards. It is not C-m that is bound to Enter, it is Enter that is bound to C-m. And C-m is the same as
RET
.If you run C-h k C-m, you will see something like "
RET runs the command ...
". C-m sendsRET
because it is a control code, see http://en.wikipedia.org/wiki/Control_character.The Enter key is bound to C-m; if you run C-h k Enter, you will see something like "
RET (translated from <return>) runs the command ...
". See, Enter is being interpreted by emacs as <return> and then that key is getting translated to C-m.What you want to do is first remove the translation from <return> to
RET
by binding it directly to what it's currently indirectly bound, e.g.(global-set-key (kbd "<return>") 'newline)
. Then you're free to bind C-m to whatever you want without affecting Enter.This assumes you're using the graphical emacs. If you're running it in a terminal, this won't work, because Enter will send C-m, not <return>. You can check that using the
window-system
variable though.注意:该问题不仅限于 Linux,它也存在于 Windows(可能还有 Mac)上。阅读所有知识的其他(非堆栈溢出)来源:有关回车的维基百科。
如果您想重新绑定 Cm,请务必全部绑定 ,否则您将面临无法再使用 Enter/Return 键的风险。此外,在终端中,Emacs 无法区分两者(Cm 和 )。
在普通的 Emacs 中,Enter/Return 键绑定到 ,它(默认情况下)转换为 RET (与 Cm< 相同) /kbd>)。如果您只反弹 Cm,您也会影响 Enter/Return 键。
尝试 Ch k 你会看到
因此,在适当的键盘映射中重新绑定两者,以确保获得所需的行为。
使用以下代码可能会有所帮助:
将其放入
*scratch*
缓冲区中,并在每行后按 Cj (以评估 sexp)。然后使用 Enter/Return 键和 Cm 进行操作。Note: The issue isn't limited to Linux, it exists on Windows (and presumably Mac) as well. Read the other (non stack-overflow) source of all knowledge: Wikipedia on Carriage Return.
If you want to rebind C-m, be sure to all bind <return> otherwise you run the risk of no longer being able to use the Enter/Return key. Also, in a terminal, Emacs cannot distinguish between the two (C-m and <return>).
In a plain Emacs, the Enter/Return key is bound to <return>, which is (by default) translated to RET (same thing as C-m). If you only rebound the C-m, you'd also be affecting the Enter/Return key.
Try C-h k <return> and you'll see
So, rebind both in the appropriate keymap to make sure you get the behavior you want.
It might be instructive to play with the following code:
Put that in your
*scratch*
buffer and press C-j after each line (to evaluate the sexp). Then play with the Enter/Return keys and C-m.input-decode-map
就可以了。引用 emacs 手册:这与我上面介绍的原理相同,将 Ctrl+m 转换为 Ctrl+1
并将 Ctrl+1 映射到您的命令。我使用 Ctrl+m 来表示
backward-kill-word
。干得好:
input-decode-map
does the trick. Quote from the emacs manual:It's the same principle like I've presented above, transforming Ctrl+m to something, say Ctrl+1
and map Ctrl+1 to your command. I use Ctrl+m for
backward-kill-word
.Here you go:
目前还不清楚前面的答案是否已经解决了这个问题,所以这里有另一个问题:
历史上,“return”通常意味着两件事:回车和换行。
引用维基百科:
长话短说,有两个 ASCII 代码与行尾相关(因此也可能与返回键相关):CR(ASCII 十进制 13,或 Ctrl-m)和 LF (ASCII 十进制 10,或 Ctrl-J)。
我认为现在的通用约定是将 return 或 enter 键映射到 ASCII 13,因此是“return”( (即按我的 return/enter 键),我得到:
RET
emacs 消息传递)。但如果您从终端模拟器运行 emacs,这可能意味着您无法选择使用与RET
不同的 Cm 绑定。当我尝试在终端(MacOS X 上的 Terminal.app 中的 GNU Emacs 23.2.1)中运行 emacs 时,我尝试输入 Ch c如果我输入 Ch c Cm (即按住 control 并按 M),我得到:
换句话说,完全相同的事情。 Emacs(或从终端运行的任何其他程序)无法区分两者之间的区别。 (了解这一点有时会很方便 - 如果您登录的系统与您所在的系统映射的内容不同,您可以键入 Cm 或 Cj 得到你想要的东西,这取决于映射被搞砸的方式。)
说到映射被搞砸,我只想提到有一些相关的 stty 设置诸如此类:
inlcr
、inlcr
和igncr
(这三个与输入的处理方式有关 - 还有一些与输出相关)的)。我怀疑进行 stty 更改对于这个特定问题是否有用,但我认为值得一提。最后,我认为你的选择是:
生活在没有Cm的情况下,这意味着与返回/输入不同的东西
运行 emacs 的图形版本,而不是终端版本。这应该可以访问不同的键码,终端仿真器不会传递这些键码(因为它正在仿真终端,而终端没有像现代计算机那样具有此类功能)。
It's unclear whether the previous answers have solved this question, so here's another spin on it:
Historically, "return" frequently meant two things: Carriage Return, and Line Feed.
Quoting wikipedia:
Long story short, there are two ASCII codes that are relevant to end-of-line (and, therefore, potentially to the return key): CR (ASCII decimal 13, or Ctrl-m) and LF (ASCII decimal 10, or Ctrl-J).
I think the general convention these days is for the return or enter keys to map to ASCII 13, and thus be "return" (
RET
in emacs messaging). But if you're running emacs from a terminal emulator, this may mean that you don't have the option of having a C-m binding that's different fromRET
. When I try running emacs in a terminal (GNU Emacs 23.2.1 on MacOS X in Terminal.app), and I try typing C-h c <return> (i.e. pressing my return/enter key), I get:If I type, instead, C-h c C-m (i.e. holding down control and pressing M), I get:
In other words, the exact same thing. Emacs (nor any other program run from the terminal) can't tell the difference between the two. (Knowing this can sometimes be handy -- if you're logged in to a system that maps things differently than the system you're coming from, you can type C-m or C-j to get the thing you want, depending on the way the mapping is screwed up.)
And speaking of having the mapping screwed up, I'll just mention that there are some
stty
settings that can be relevant to such things:inlcr
,inlcr
, andigncr
(these three are related to how input is processed -- there are also some output-related ones). I doubt that making stty changes is going to be useful to this particular problem, but I thought it worth mentioning.In the end, I think your choices are:
Live with not having C-m that means anything different than return/enter
Run a graphical version of emacs, instead of the terminal version. This should have access to the distinct keycodes, which the terminal emulator isn't passing along (because it's emulating a terminal, which didn't have such things in the same way as modern computers do).
(全局设置键(kbd“Cm”)'cmd);
其中cmd是你的命令应该重新映射控制m...
至于为什么ctrl+m一定要输入。我相信这与一些旧键盘没有输入,制表符,退格键等有关...(我可能是严重错误的)
例如ctrl + h是退格键,一些unix操作系统在你按退格键时会输出^H在他们身上!
(global-set-key (kbd "C-m") 'cmd) ;
Where cmd is your command should remap control m...
As for why ctrl+m is bound to enter. I believe it had something to do with some older keyboard not having enter,tab, backspace, etc... ( I could be grossly mistaken)
For example ctrl+h is backspace, some unix operating systems will output ^H when you hit backspace on them!
问题的主要根源在于 Enter 和 Ctrl-M 都映射到相同的 ASCII 代码 (13)。您只能在可以区分它们的系统上清楚地映射它们。
The main source of the problem is that Enter and Ctrl-M both map to the same ASCII code (13). You would only be able to map them distinctly on a system that can distinguish them.
事实上,这是一个非常棘手的问题,你不会回答正确的:
(global-set-key (kbd "") 'newline)
因为返回 (RET) 在某些特定情况下是换行符。如果您在 .emacs 中尝试,您会发现我所说的奇怪之处,
我通过使用一些 KDE 事件应用程序找到了一个丑陋但有效的解决方案,并将 Ctrl+m 绑定到 Ctrl+1 。我选择它是因为我不会使用该组合 (Ctrl+1),但您可以选择其他组合。
这样,在 emacs 中(但在我的 X 环境中),当我按 Ctrl+m 时,我不会得到 RET(或换行符),而是得到 Ctrl+1。
然后,我做了类似的事情:
(global-set-key "\C-1" 'mycmd)
问题是,现在我使用很棒的窗口管理器,但我不知道如何再次进行 X 映射。
如果您不使用 KDE,您可以在 Gnome 中搜索类似的内容。
Actually, this is a very tricky question, you won't get it right with:
(global-set-key (kbd "") 'newline)
because that return (RET) is newline in just some particular cases. You will see the weirdness I'm talking about if you try that in your .emacs
I've found an ugly but working solution by using some KDE events application, and bound Ctrl+m to Ctrl+1 . I've chosen that because I wouldn't use that combination (Ctrl+1) but you can choose something else.
This way, in emacs (but in my X environment) I don't get the RET (or linefeed character) when I press Ctrl+m, instead I get Ctrl+1.
And then, I did something like:
(global-set-key "\C-1" 'mycmd)
The problem is, that now I use awesome window manager, and I don't know how to do that X mapping again.
If you don't use KDE, you search for something similar in Gnome.
关于问题的本质,这里有一些很好的解释,我不会阐述这些问题。但是,我想直接回答原来的问题。我通过
~/.xemacs/custom.el'newline
命令的(control m)
序列的问题code> 文件:在上面的示例中,我已将
(control m)
更改为在 < code>c-mode 或c++-mode
处于活动状态。请注意,您也可以全局更改
(control m)
的行为,而无需模式相关的绑定。在这种情况下,只需将以下内容添加到您的~/.xemacs/custom.el
文件中:Some very good explanations here as to the nature of the problem, and I won't expound on those issues. However, I want to address the original question directly. I solved the problem for changing the
(control m)
sequence bound to'newline
command by the following code in my~/.xemacs/custom.el
file:In the above example, I have changed
(control m)
to run the'compile
command (M-x compile
) when thec-mode
orc++-mode
is active.Note that you can change the behavior of
(control m)
globally as well, without the mode related bindings. In that case, just add the following to your~/.xemacs/custom.el
file: