gak
gak

Reputation: 32773

How do I use vi keys in ipython under *nix?

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

How do I get this going in iPython?

Upvotes: 117

Views: 25697

Answers (7)

Harsh Verma
Harsh Verma

Reputation: 933

Adding the following two configs in ~/.ipython/profile_default/ipython_config.py will help you use vi bindings and prevent an annoying slowness issue (related to switching between vim modes):

c.TerminalInteractiveShell.editing_mode = 'vi'
c.TerminalInteractiveShell.emacs_bindings_in_vi_insert_mode = False

Upvotes: 1

Pierz
Pierz

Reputation: 8148

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 'Meta'+CTRL+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 CTRL+e but that didn't appear to work for me - I had to instead do 'Meta'+CTRL+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

Upvotes: 13

gregory
gregory

Reputation: 12955

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.

Upvotes: 0

Lexi
Lexi

Reputation: 1730

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()

Upvotes: 10

imiric
imiric

Reputation: 9030

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'

Upvotes: 217

Noufal Ibrahim
Noufal Ibrahim

Reputation: 72795

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.

Upvotes: 11

gak
gak

Reputation: 32773

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

Upvotes: 32

Related Questions