根据 vi 模式更改交互式 IPython 控制台中的光标形状

Changing the cursor shape in the interactive IPython consle depending on vi-mode

我可以在交互式 IPython 控制台中从 emacs 更改为 vi 绑定,方法是将以下内容添加到 ~/.ipython/profile_deafult/ipython_config.py

c.TerminalInteractiveShell.editing_mode = 'vi'

目前,光标始终是工字梁 (|)。有什么办法可以让光标在 vi 的正常模式下变成方块形状,然后在插入模式下变回工字梁形状?


我的终端仿真器(终结器,基于 gnome-terminal)支持在光标格式之间切换,因为我可以在 zsh 中启用该行为,方法是将以下内容添加到我的 ~./zshrc (from the Unix SE) :

bindkey -v
# Remove delay when entering normal mode (vi)
KEYTIMEOUT=5
# Change cursor shape for different vi modes.
function zle-keymap-select {
  if [[ $KEYMAP == vicmd ]] || [[  = 'block' ]]; then
    echo -ne '\e[1 q'
  elif [[ $KEYMAP == main ]] || [[ $KEYMAP == viins ]] || [[ $KEYMAP = '' ]] || [[  = 'beam' ]]; then
    echo -ne '\e[5 q'
  fi
}
zle -N zle-keymap-select
# Start with beam shape cursor on zsh startup and after every command.
zle-line-init() { zle-keymap-select 'beam'}

根据 this comment in the GitHub issue (and this update),您可以将以下代码片段添加到 ~/.ipython/profile_default/ipython_config.py 以使光标在插入和正常模式之间切换时改变形状。

import sys
from prompt_toolkit.key_binding.vi_state import InputMode, ViState


def get_input_mode(self):
    return self._input_mode


def set_input_mode(self, mode):
    shape = {InputMode.NAVIGATION: 1, InputMode.REPLACE: 3}.get(mode, 5)
    raw = u'\x1b[{} q'.format(shape)
    if hasattr(sys.stdout, '_cli'):
        out = sys.stdout._cli.output.write_raw
    else:
        out = sys.stdout.write
    out(raw)
    sys.stdout.flush()
    self._input_mode = mode


ViState._input_mode = InputMode.INSERT
ViState.input_mode = property(get_input_mode, set_input_mode)
c.TerminalInteractiveShell.editing_mode = 'vi'