bash 如何在 *nix 下的 ipython 中使用 vi 键?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10394302/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I use vi keys in ipython under *nix?
提问by gak
Currently in Bash I use set -o vito enable vi mode in my bash prompt.
目前在 Bash 中,我用来set -o vi在我的 bash 提示符中启用 vi 模式。
How do I get this going in ipython?
我如何在 ipython 中做到这一点?
Note:If an answer applies to all *nix, I'll remove the OS X from the title :)
注意:如果答案适用于所有 *nix,我将从标题中删除 OS X :)
回答by imiric
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 5.0 从 readline 切换到 prompt_toolkit,所以这个问题的更新答案是传递一个选项:
$ 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 createif you don't have it) with:
... 或者在配置文件配置中全局设置它(如果没有~/.ipython/profile_default/ipython_config.py,ipython profile create则创建它):
c.TerminalInteractiveShell.editing_mode = 'vi'
回答by gak
Looks like a solution works for many other readline compatible apps:
看起来一个解决方案适用于许多其他与 readline 兼容的应用程序:
Set the following in your ~/.inputrcfile:
在您的~/.inputrc文件中设置以下内容:
set editing-mode vi
set keymap vi
set convert-meta on
回答by Pierz
You can also interactively switch between Vi-mode and Emacs mode. According to the the readline docsto 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.
您还可以在 Vi 模式和 Emacs 模式之间交互切换。根据在它们之间切换的readline 文档,您应该能够使用 MCj 组合键,但这似乎只允许我切换到 vi 模式 - 在我的 Mac 上(其中 ESC 用作“元”键) 它是:ESC+ CTRL+ j。要切换回 Emacs 模式,可以使用 Ce 但这似乎对我不起作用 - 我不得不改用 MCe - 在我的 Mac 上它是:ESC+ CTRL+ e。
FYI my ~/.inputrc is set up as follows:
仅供参考,我的 ~/.inputrc 设置如下:
set meta-flag on
set input-meta on
set convert-meta off
set output-meta on
回答by Noufal Ibrahim
ipythonuses the readline library and this is configurable using the ~/.inputrcfile. You can add
ipython使用 readline 库,这可以使用~/.inputrc文件进行配置。你可以加
set editing-mode vi
to that file to make all readlinebased applications use vi style keybindings instead of Emacs.
到该文件,使所有readline基于应用程序的应用程序使用 vi 样式键绑定而不是 Emacs。
回答by Lex R
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:
我需要能够在 IPython 5 中以交互方式切换模式,我发现您可以通过重新创建提示管理器来实现:
a = get_ipython().configurables[0]; a.editing_mode='vi'; a.init_prompt_toolkit_cli()
回答by gregory
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:
您可以在 .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 *
That last line is if you use ipython with Django, and want to import all your models by default.
最后一行是如果您将 ipython 与 Django 一起使用,并且希望默认导入所有模型。

