根据vi-mode更改交互式IPython中的光标形状

时间:2017-05-25 23:59:07

标签: vim terminal cursor ipython

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

c.TerminalInteractiveShell.editing_mode = 'vi'

目前,光标始终是工字梁(|)。有没有办法可以在vi的正常模式下将光标改变为块,然后在插入模式下返回到工字梁?

我的终端模拟器(终结者,基于gnome-terminal)支持在光标格式之间切换,因为我可以通过在~./zshrcfrom the Unix SE)中添加以下内容来启用zsh中的行为:< / p>

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 ]] || [[ $1 = 'block' ]]; then
    echo -ne '\e[1 q'
  elif [[ $KEYMAP == main ]] || [[ $KEYMAP == viins ]] || [[ $KEYMAP = '' ]] || [[ $1 = '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'}

1 个答案:

答案 0 :(得分:1)

根据this comment in the GitHub issue(和this update),您可以将以下代码段添加到~/.ipython/profile_deafult/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'