问题:如何在* nix下的ipython中使用vi键?

当前在Bash中,我用于set -o vi在bash提示符中启用vi模式。

我如何在ipython中进行此操作?

注意:如果答案适用于所有* nix,我将从标题中删除OS X :)

Currently in Bash I use set -o vi to enable vi mode in my bash prompt.

How do I get this going in ipython?

Note: If an answer applies to all *nix, I’ll remove the OS X from the title :)


回答 0

如果有人最近在这里闲逛,那么IPython 5.0从readline切换到hint_toolkit,因此对此问题的更新答案是传递一个选项:

$ ipython --TerminalInteractiveShell.editing_mode=vi

…或在配置文件配置中进行全局设置(~/.ipython/profile_default/ipython_config.pyipython profile create如果没有,则使用创建):

c.TerminalInteractiveShell.editing_mode = 'vi'

In case someone’s wandering in here recently, IPython 5.0 switched from readline to prompt_toolkit, so an updated answer to this question is to pass an option:

$ ipython --TerminalInteractiveShell.editing_mode=vi

… or to set it globally in the profile configuration (~/.ipython/profile_default/ipython_config.py; create it with ipython profile create if you don’t have it) with:

c.TerminalInteractiveShell.editing_mode = 'vi'

回答 1

看来解决方案适用于许多其他与Readline兼容的应用程序:

~/.inputrc文件中设置以下内容:

set editing-mode vi
set keymap vi
set convert-meta on

资料来源:http : //www.jukie.net/bart/blog/20040326082602

Looks like a solution works for many other readline compatible apps:

Set the following in your ~/.inputrc file:

set editing-mode vi
set keymap vi
set convert-meta on

Source: http://www.jukie.net/bart/blog/20040326082602


回答 2

您也可以在Vi-mode和Emacs模式之间交互切换。根据readline文档在它们之间进行切换,您应该能够使用MCj组合键,但这似乎只允许我切换到vi模式-在Mac上(其中ESC被用作“ Meta”键) )是:ESC+ CTRL+ j。要切换回Emacs模式,可以使用Ce,但对我而言似乎不起作用-我不得不改用MCe-在Mac上是:ESC+ CTRL+ e

仅供参考我的〜/ .inputrc设置如下:

set meta-flag on
set input-meta on
set convert-meta off
set output-meta on

You can also interactively switch between Vi-mode and Emacs mode. According to the the readline docs to switch between them you are supposed to be able to use the M-C-j key combination but that only seems to allow me to switch to vi-mode – on my Mac (where ESC is used as the ‘Meta’ key) it is: ESC+CTRL+j. To switch back to Emacs mode one can use C-e but that didn’t appear to work for me – I had to instead do M-C-e – on my Mac it is: ESC+CTRL+e.

FYI my ~/.inputrc is set up as follows:

set meta-flag on
set input-meta on
set convert-meta off
set output-meta on

回答 3

ipython使用readline库,并且可以使用该~/.inputrc文件进行配置。你可以加

set editing-mode vi

到该文件,以使所有readline基础应用程序都使用vi样式键绑定而不是Emacs。

ipython uses the readline library and this is configurable using the ~/.inputrc file. You can add

set editing-mode vi

to that file to make all readline based applications use vi style keybindings instead of Emacs.


回答 4

我需要能够在IPython 5中交互地切换模式,我发现可以通过即时创建提示管理器来做到这一点:

a = get_ipython().configurables[0]; a.editing_mode='vi'; a.init_prompt_toolkit_cli()

I needed to be able to switch modes interactively in IPython 5 and I found you can do so by recreating the prompt manager on the fly:

a = get_ipython().configurables[0]; a.editing_mode='vi'; a.init_prompt_toolkit_cli()

回答 5

您可以在.ipython启动配置文件中设置vi。如果没有,则创建一个文件,方法是添加一个~/.ipython/profile_default/startup/名为的文件start.py。这是一个例子:

# Initializing script for ipython in ~/.ipython/profile_default/startup/
from IPython import get_ipython
ipython = get_ipython()

# If in ipython, set vi and load autoreload extension
if 'ipython' in globals():
    ipython.editing_mode = 'vi'
    ipython.magic('load_ext autoreload')
    ipython.magic('autoreload 2')
from Myapp.models import * 

最后一行是如果您将ipython与Django一起使用,并且要默认导入所有模型。

You may set vi in your .ipython start-up config file. Create one if you don’t have it by adding a file to ~/.ipython/profile_default/startup/ called something like start.py. Here’s an example:

# Initializing script for ipython in ~/.ipython/profile_default/startup/
from IPython import get_ipython
ipython = get_ipython()

# If in ipython, set vi and load autoreload extension
if 'ipython' in globals():
    ipython.editing_mode = 'vi'
    ipython.magic('load_ext autoreload')
    ipython.magic('autoreload 2')
from Myapp.models import * 

That last line is if you use ipython with Django, and want to import all your models by default.


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。