有没有办法通过按键将 Bash 或 zsh 从 Emacs 模式切换到 vi 模式?

发布于 2024-08-29 02:11:37 字数 300 浏览 4 评论 0原文

我希望能够暂时从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

忘东忘西忘不掉你 2024-09-05 02:11:37

您可以创建一个切换开关,因为 vi 模式和 emacs 模式之间的键绑定是分开的。

$ set -o emacs
$ bind '"\ee": vi-editing-mode'
$ set -o vi
$ bind '"\ee": emacs-editing-mode'

现在 Alt-e(或 Esc e)将在模式之间切换。

将其添加到 PS1 定义中的某个位置,这样您的提示中就会有一个指示符,指示您所处的模式。当您切换模式时,它不会立即显示更改,但当发出新的提示。

$(set -o | grep emacs.*on >/dev/null 2>&1 && echo E || echo V)

You can create a toggle since the key bindings are separate between vi mode and emacs mode.

$ set -o emacs
$ bind '"\ee": vi-editing-mode'
$ set -o vi
$ bind '"\ee": emacs-editing-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.

$(set -o | grep emacs.*on >/dev/null 2>&1 && echo E || echo V)
潇烟暮雨 2024-09-05 02:11:37

啊哈!我查看了 readline 源代码,发现你可以这样做:

 "\M-v": vi-editing-mode
 "\M-e": emacs-editing-mode

似乎没有切换,但这可能已经足够好了!

为了后代的缘故,这是我最初的答案,这对于尝试做没有 readline 功能的事情的人来说可能很有用。

您可以通过以下方式进行设置:清除进程中的当前命令行。我知道这不是你想要的,但也许它会帮助发现这个问题的其他人。在 ~/.inputrc:

"\M-v": "\C-k\C-uset -o vi\C-j" # alt (meta)-v: set vi mode
"\M-e": "\C-k\C-uset -o vi\C-j" # alt (meta)-e: set emacs mode

或切换...这应该有效:

"\M-t": "\C-k\C-u[[ \"$SHELLOPTS\" =~ '\\bemacs\\b' ]] && set -o vi || set -o emacs\C-j"

这些本质上是别名,进一步映射到 readline 中的键,这样您就不必键入别名并按 Enter 键。

Aha! I looked at the readline source, and found out that you can do this:

 "\M-v": vi-editing-mode
 "\M-e": emacs-editing-mode

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:

"\M-v": "\C-k\C-uset -o vi\C-j" # alt (meta)-v: set vi mode
"\M-e": "\C-k\C-uset -o vi\C-j" # alt (meta)-e: set emacs mode

or to toggle...this should work:

"\M-t": "\C-k\C-u[[ \"$SHELLOPTS\" =~ '\\bemacs\\b' ]] && set -o vi || set -o emacs\C-j"

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.

我喜欢麦丽素 2024-09-05 02:11:37

以下 .inputrc 行允许 Meta / Alt+Eemacsvi-insert 之间切换模式。

同时按下 jk 将进入 vi-command 模式。

注意:唯一带有“kj”的英文单词是“blackjack”,没有单词包含“jk”)

set keymap emacs
"\ee": vi-editing-mode
"jk": "\eejk"
"kj": "\eejk"

set keymap vi-insert
"\ee": emacs-editing-mode
"jk": vi-movement-mode
"kj": vi-movement-mode

set keymap vi-command
"\ee": emacs-editing-mode

注意:如果您将 keymap emacs 下的绑定添加到 vi-movement-mode code> 尝试直接切换到 vi-command 模式,如果您打开了 show-mode-in-prompt 则提示不会更新,因此上述工作 -需要周围。

The following .inputrc lines allow Meta / Alt+E to switch between emacs and vi-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")

set keymap emacs
"\ee": vi-editing-mode
"jk": "\eejk"
"kj": "\eejk"

set keymap vi-insert
"\ee": emacs-editing-mode
"jk": vi-movement-mode
"kj": vi-movement-mode

set keymap vi-command
"\ee": emacs-editing-mode

Note: If you add a binding under keymap emacs to vi-movement-mode to try to switch straight to vi-command mode, the prompt doesn't update if you have show-mode-in-prompt on, hence the above work-around is needed.

沧桑㈠ 2024-09-05 02:11:37

我终于找到了如何使用单个键切换 vi 和 emacs 模式,例如 zsh 中的 [alt]+[i]:

# in the .zshrc
# toggle vi and emacs mode
vi-mode() { set -o vi; }
emacs-mode() { set -o emacs; }
zle -N vi-mode
zle -N emacs-mode
bindkey '\ei' vi-mode              # switch to vi "insert" mode
bindkey -M viins 'jk' vi-cmd-mode  # (optionally) exit to vi "cmd" mode
bindkey -M viins '\ei' emacs-mode  # switch to emacs mode

I finally found out how to toggle vi and emacs mode with a single key e.g. [alt]+[i] in zsh:

# in the .zshrc
# toggle vi and emacs mode
vi-mode() { set -o vi; }
emacs-mode() { set -o emacs; }
zle -N vi-mode
zle -N emacs-mode
bindkey '\ei' vi-mode              # switch to vi "insert" mode
bindkey -M viins 'jk' vi-cmd-mode  # (optionally) exit to vi "cmd" mode
bindkey -M viins '\ei' emacs-mode  # switch to emacs mode
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文