33

I was working a bit in the python interpreter (python 2.4 on RHEL 5.3), and suddenly found myself in what seems to be a 'vi command mode'. That is, I can edit previous commands with typical vi key bindings, going left with h, deleting with x...

I love it - the only thing is, I don't know how I got here (perhaps it's through one of the modules I've imported: pylab/matplotlib?).

Can anyone shed some light on how to enable this mode in the interpreter?

codeforester
  • 39,467
  • 16
  • 112
  • 140
yungchin
  • 1,519
  • 2
  • 15
  • 17
  • 1
    More recent answers provided here that helped me - http://stackoverflow.com/questions/6636124/how-do-i-make-vi-editing-mode-work-in-irb-when-using-rvm – arcseldon Nov 24 '15 at 00:09

5 Answers5

37

Ctrl-Alt-J switches from Emacs mode to Vi mode in readline programs.

Alternatively add "set editing-mode vi" to your ~/.inputrc

Mike Lyons
  • 22,575
  • 2
  • 16
  • 19
  • 4
    Doesn't work for me -- just seems to produce a . I'm on a mac; does that make a difference? – John Fouhy Feb 11 '09 at 23:24
  • 2
    For anyone having enough rep to add to this answer, here's a link to the readline docs: http://tiswww.case.edu/php/chet/readline/rluserman.html#SEC22 ...so for emacs-editing-mode hit C-e, and for vi-editing-mode hit M-C-j – yungchin Feb 12 '09 at 14:27
  • 4
    @JohnFouhy I'm having the same issue. And `set editing-mode vi` in my `~/.inputrc` isn't helping, either. – root Jul 31 '13 at 17:17
34

This kind of all depends on a few things.

First of all, the python shell uses readline, and as such, your ~/.inputrc is important here. That's the same with psql the PostgreSQL command-line interpreter and mysql the MySQL shell. All of those can be configured to use vi-style command bindings, with history etc.

<ESC> will put you into vi mode at the python shell once you've got your editing mode set to vi

You may need the following definition in your ~/.inputrc

set editing-mode vi

OSX info

OSX uses libedit which uses ~/.editrc. You can man editrc for more information.

For example, to mimick a popular key combination which searches in your history, you can add the following to your .editrc

bind "^R" em-inc-search-prev
Philip Reynolds
  • 9,364
  • 3
  • 30
  • 37
  • did a line in your answer go missing, just before "will put you..."? thanks! – yungchin Feb 11 '09 at 16:44
  • Wow. Not only did I not know this, I didn't even know there was anything there to know! – John Fouhy Feb 11 '09 at 23:23
  • Thanks, both for the answer and for fixing the missing ESC in it. This is great info, but I'm accepting arcanex' answer because it explains what happened in my mysterious case... (I didn't have any .inputrc file) – yungchin Feb 12 '09 at 02:27
  • 2
    On MAC OS X - I still had problems after all the above suggestions. The answer provided here fixed my issue - http://stackoverflow.com/questions/6636124/how-do-i-make-vi-editing-mode-work-in-irb-when-using-rvm In summary, adding both bind -v to .editrc, and set editing-mode vi to inputrc worked. – arcseldon Nov 24 '15 at 00:04
7

For Mac OS X 10.10.3, python2.7, vi mode can be configured by placing bind -v in ~/.editrc. The last few paragraphs of the man page hint at this.

codeforester
  • 39,467
  • 16
  • 112
  • 140
Mark Beckner
  • 71
  • 1
  • 1
  • An important note is that this works even for MacPorts `python`, which does *not* mention `~/.editrc` in the manpage. Even if you set up `~/.inputrc` correctly, MacPorts `python` will not work as you expect. You *need* the `~/.editrc` line. – anahata Feb 15 '23 at 05:09
5

Use readline.parse_and_bind method. For example, try on python interactive console:

import readline
readline.parse_and_bind("set editing-mode vi")

It seems any command you can set in .inputrc you can set via this method too. I tried it in Python 2.7 and 3.5.1.

See also man readline


EDIT (Dec 21th, 2019): or maybe, to have a true vim you can manage to patch the python's readline with Athame. I did it with bash and it is very cool.

Iacchus
  • 2,671
  • 2
  • 29
  • 24
0

On macOS Monterey(12.5) with Python 3.10, I got vi bindings to work in the python interpreter by adding set editing-mode vi to ~/.inputrc(which I also created).

Harsh Verma
  • 529
  • 6
  • 10