如何在 readline 中切换到 vi 编辑模式?

Eri*_*son 19 bash readline

我想在 readline 环境中切换到 vi 编辑模式。但我不想使用'set -o vi'。我想使用键盘快捷键临时切换。手册页说我可以用M-C-j. 但这对我不起作用。

我正在使用 Ubuntu 和一个 xterm。也不能在 gnome-terminal 下工作。

slm*_*slm 14

我想确认键盘映射Meta+ Control+j实际上是在你的系统上是正确的。您可以使用此命令列出各种 Bash 模式的所有键绑定。在我的系统上也没有键绑定。

$ bind -P| grep edit
edit-and-execute-command can be found on "\C-x\C-e".
emacs-editing-mode is not bound to any keys
vi-editing-mode is not bound to any keys
Run Code Online (Sandbox Code Playgroud)

您可以执行以下操作,以便当您键入Esc+ 时e,它将在 2 种模式之间切换。

$ set -o emacs
$ bind '"\ee": vi-editing-mode'
$ set -o vi
$ bind '"\ee": emacs-editing-mode'
Run Code Online (Sandbox Code Playgroud)

bind命令现在显示:

在 vi 模式下

$ bind -P |grep edit
edit-and-execute-command is not bound to any keys
emacs-editing-mode can be found on "\ee".
vi-editing-mode is not bound to any keys
Run Code Online (Sandbox Code Playgroud)

在 emacs 模式下

$ bind -P |grep edit
edit-and-execute-command can be found on "\C-x\C-e".
emacs-editing-mode is not bound to any keys
vi-editing-mode can be found on "\ee".
Run Code Online (Sandbox Code Playgroud)

现在您可以使用Esc+e在 2 种不同模式之间切换。


Eri*_*son 8

Bash 明确禁用了这个和其他一些 Readline 快捷方式。查看initialize_readline()bash源代码中的函数(http://www.catonmat.net/download/bashline.c):

   /* In Bash, the user can switch editing modes with "set -o [vi emacs]",
      so it is not necessary to allow C-M-j for context switching.  Turn
      off this occasionally confusing behaviour. */
   rl_unbind_key_in_map (CTRL('J'), emacs_meta_keymap);
   rl_unbind_key_in_map (CTRL('M'), emacs_meta_keymap);
#if defined (VI_MODE)
  rl_unbind_key_in_map (CTRL('E'), vi_movement_keymap);
#endif
Run Code Online (Sandbox Code Playgroud)

我似乎无法使用 Readline 配置文件 (.inputrc) 覆盖此行为。


spe*_*ufo 6

~/.inputrc根据 slm 的回答,这是我最终用于我的 .

set show-mode-in-prompt on

set keymap emacs
"\ea": vi-editing-mode

set keymap vi-command
"k": history-search-backward
"j": history-search-forward
"z": emacs-editing-mode
"\ea": emacs-editing-mode

set keymap vi-insert
"\ea": emacs-editing-mode
"\C-l": clear-screen
"\C-e": end-of-line
"\C-k": kill-line

set editing-mode vi
Run Code Online (Sandbox Code Playgroud)

我尝试了$if mode=语法,但我认为这是静态解析的(有一次,在读取文件时),所以它没有按我预期的那样工作。所以我们需要切换到每个键映射并修改它的键绑定,即使之前在另一个键映射上设置过。最后我会说我想从哪种模式开始。