有没有办法通过按键将 Bash 或 zsh 从 Emacs 模式切换到 vi 模式?
我希望能够暂时从 emacs 模式切换到 vi 模式,因为 vi 模式有时更好,但我通常在输入内容到一半时才意识到我想使用 vi模式。
我不想永久切换到 vi 模式,因为我通常更喜欢命令行上的 emacs 模式,主要是因为这是我习惯的模式,多年来许多击键已经成为第二天性。 (作为一名编辑器,我通常在 viper 模式下使用 emacs,这样我就可以同时使用 vi 和 emacs 击键,因为我发现自己总是不小心在 vi 中使用它们,然后把事情搞砸了,而且因为在某些情况下,我发现 vi 击键更容易记住和方便,而在其他情况下,emacs 更容易记住。)
I'd like to be able to switch temporarily from emacs mode to vi mode, since vi mode is sometimes better, but I'm usually half-way through typing something before I realize I want to use vi mode.
I don't want to switch permanently to vi mode, because I normally prefer emacs mode on the command line, mostly because it's what I'm used to, and over the years many of the keystrokes have become second nature. (As an editor I generally use emacs in viper mode, so that I can use both vi and emacs keystrokes, since I found myself accidentally using them in vi all the time, and screwing things up, and because in some cases I find vi keystrokes more memorable and handy, and in other cases emacs.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以创建一个切换开关,因为 vi 模式和 emacs 模式之间的键绑定是分开的。
现在 Alt-e(或 Esc e)将在模式之间切换。
将其添加到
PS1
定义中的某个位置,这样您的提示中就会有一个指示符,指示您所处的模式。当您切换模式时,它不会立即显示更改,但当发出新的提示。You can create a toggle since the key bindings are separate between vi mode and emacs mode.
Now Alt-e (or Esc e) will toggle between modes.
Add this somewhere in your definition for
PS1
so you have an indicator in your prompt of which mode you're in. It won't show the change immediately when you toggle modes, but it will update when a new prompt is issued.啊哈!我查看了 readline 源代码,发现你可以这样做:
似乎没有切换,但这可能已经足够好了!
为了后代的缘故,这是我最初的答案,这对于尝试做没有 readline 功能的事情的人来说可能很有用。
您可以通过以下方式进行设置:清除进程中的当前命令行。我知道这不是你想要的,但也许它会帮助发现这个问题的其他人。在 ~/.inputrc:
或切换...这应该有效:
这些本质上是别名,进一步映射到 readline 中的键,这样您就不必键入别名并按 Enter 键。
Aha! I looked at the readline source, and found out that you can do this:
There doesn't appear to be a toggle, but that's probably good enough!
For posterity's sake, here's my original answer, which could be useful for people trying to do things for which there is no readline function.
Here's a way you could set it up, clearing the current command line in the process. Not what you want, I know, but maybe it'll help someone else who finds this question. In ~/.inputrc:
or to toggle...this should work:
These are essentially aliases, taken one step farther to map to keys in readline so that you don't have to type an alias name and hit enter.
以下
.inputrc
行允许 Meta / Alt+E 在emacs
和vi-insert 之间切换模式。
同时按下 j 和 k 将进入
vi-command
模式。注意:唯一带有“kj”的英文单词是“blackjack”,没有单词包含“jk”)
注意:如果您将
keymap emacs
下的绑定添加到vi-movement-mode
code> 尝试直接切换到 vi-command 模式,如果您打开了 show-mode-in-prompt 则提示不会更新,因此上述工作 -需要周围。The following
.inputrc
lines allow Meta / Alt+E to switch betweenemacs
andvi-insert
modes.Mooshing both j and k simultaneously will take you to
vi-command
mode.Note: The only English word with "kj" is "blackjack", no words contain "jk")
Note: If you add a binding under
keymap emacs
tovi-movement-mode
to try to switch straight tovi-command
mode, the prompt doesn't update if you haveshow-mode-in-prompt on
, hence the above work-around is needed.我终于找到了如何使用单个键切换 vi 和 emacs 模式,例如 zsh 中的 [alt]+[i]:
I finally found out how to toggle vi and emacs mode with a single key e.g. [alt]+[i] in zsh: