6

I know that you can use the Python shell in Vi mode on Unix-like operating systems. For example, I have this line in my ~/.inputrc:

set editing-mode vi

This lets me use Vi-style editing inside the Python shell.

But can this be made to work when using Python on a Windows XP box? I'm using the pre-built Python for Windows downloaded directly from python.org.

I'm guessing that the Windows version does not use the GNU Readline library, but I'd be happy to be proven wrong. :)

Brian Neal
  • 31,821
  • 7
  • 55
  • 59

3 Answers3

4

Install PyReadline

Setup your pyreadlineconfig.ini and PYTHONSTARTUPFILE files as per the PyReadline instructions.

Then add the following to your pyreadlineconfig.ini file:

set_mode("vi")
history_filename("~/.pythonhistory")
history_length(200)

The history settings must be after the mode setting.

Commands such as ESC K to jump to the previous command now work.

Community
  • 1
  • 1
  • I just installed Python 2.7 on Windows 7, then followed your instructions. Although vi mode seems to be working, scrolling through history with ESC K doesn't work. After I exit the Python interpreter I see that it is creating a .pythonhistory file, but it is blank. Any ideas? – Brian Neal Mar 18 '12 at 18:42
  • @BrianNeal Looks like a bug in the code. I've written a patch for you. https://github.com/willbr/pyreadline Download then `python setup.py install` should fix it. Also I've sent the pyreadline author a pull request & logged a bug on github. – William Bettridge-Radford Mar 22 '12 at 21:26
  • Excellent work! I regret not being able to upvote this more than once! – Brian Neal Mar 24 '12 at 00:43
2

cygwin can give you many Unix-y advantages on your "Windows XP box" (using its own Python build, among other things -- avoiding some of the Windows-only limitations of the "standard Windows builds") -- try it out!

Alex Martelli
  • 854,459
  • 170
  • 1,222
  • 1,395
0

Answering my own question, it looks like you can use the PyReadline library in conjunction with IPython to get most of the Vi functionality I miss on Linux. I still haven't figured out how I can hit ESC and then k to scroll through the previous command history.

Brian Neal
  • 31,821
  • 7
  • 55
  • 59