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


16

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

我正在使用Ubuntu和xterm。在gnome-terminal下也不起作用。

Answers:


12

我确认键盘映射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

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

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

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

在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".

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


请注意,键入时必须快速ESC E。如果暂停,则将从vi-insert进入vi-command模式,或者只是取消当前的vi命令。
spelufo

6

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

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


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

我尝试了$if mode=语法,但是我认为这是静态解决的(一次,当读取文件时),因此它无法按我预期的那样工作。因此,即使先前已在另一个按键图上设置,我们也需要切换到每个按键图并修改其键绑定。最后,我说出我想从哪种模式开始。


2

我试图在vi模式下使用emacs样式的映射。我最终得到了:

set keymap vi-command
"k": history-search-backward
"j": history-search-forward

set keymap vi-insert
"\C-A": beginning-of-line
"\C-B": backward-char
"\C-D": delete-char
"\C-E": end-of-line
"\C-F": forward-char
"\C-K": kill-line
"\C-L": clear-screen
"\C-N": next-history
"\C-P": previous-history
"\C-O": operate-and-get-next

# Enable Readline not waiting for additional input when a key is pressed.
# Needed for the mappings below.
set keyseq-timeout 0

# `yank-last-arg` does not work exactly as in emacs mode
"\e.": yank-last-arg
"\e\177": backward-kill-word
"\e0": digit-argument
"\e1": digit-argument
"\e2": digit-argument
"\e3": digit-argument
"\e4": digit-argument
"\e5": digit-argument
"\e6": digit-argument
"\e7": digit-argument
"\e8": digit-argument
"\e9": digit-argument
"\eb": backward-word
"\ec": capitalize-word
"\ed": kill-word
"\ef": forward-word
"\el": downcase-word
"\en": non-incremental-forward-search-history
"\ep": non-incremental-reverse-search-history
"\et": transpose-words
"\eu": upcase-word
"\ey": yank-pop

# some other useful mappings

"\e/": complete-filename
"\ek": kill-whole-line
"\eo": "\C-v\C-j"
# quickly switch to "normal" mode
"\C-[": vi-movement-mode
# perserve the currently editing line so that we can 
# do something else before restoring it.
"\eg": insert-comment
"\er": "\C-R#\C-A\C-D\C-E"

set editing-mode vi

阅读手册页readline和手册页上的READLINE部分会很有帮助bash

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.