Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Standard python interpreter has a vi command mode?

Tags:

python

vi

People also ask

Is Python a command line interpreter?

Python is an interpreter language. It means it executes the code line by line. Python provides a Python Shell, which is used to execute a single Python command and display the result.

What is Python console mode?

Python console enables executing Python commands and scripts line by line, similar to your experience with Python Shell.

What is vi mode in shell?

Like emacs-mode, vi-mode essentially creates a one-line editing window into the history file. Vi-mode is popular because vi is the most standard Unix editor. But the function for which vi was designed, writing C programs, has different editing requirements from those of command interpreters.

How do I select Python interpreter in terminal?

By default, the Python extension looks for and uses the first Python interpreter it finds in the system path. To select a specific environment, use the Python: Select Interpreter command from the Command Palette (Ctrl+Shift+P). Note: If the Python extension doesn't find an interpreter, it issues a warning.


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

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


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

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.


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.