Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Post History

80%
+6 −0
Q&A Vim key bindings not working in terminal python interpreter

Python's interactive interpreter uses GNU Readline, so Bash's line interpreter, not Zle, which is Zsh's. Therefore, when you enter python, the cursor either won't change or will change accordingly...

posted 2y ago by Quasímodo‭

Answer
#1: Initial revision by user avatar Quasímodo‭ · 2023-01-31T21:30:24Z (almost 2 years ago)
[Python's interactive interpreter uses GNU Readline][1], so Bash's line interpreter, not Zle, which is Zsh's.

Therefore, when you enter `python`, the cursor either won't change or will change accordingly to what you have in [`.inputrc`, which is GNU Readline's initialization file][2]. Likewise for the Vi or Emacs input mode.

For a visual proof that `zle-line-init` and `zle-keymap-select` don't get to run while you are in Python, just modify the cursor variables to

```
#Warning: This will be annoying
cursor_block=a
cursor_beam=b
```

Restart Zsh and you will see that, while those characters are inserted as expected in the Zsh prompt, they are never inserted in the Python prompt.

Luckily, you can (at least according to my quick inspection) clone that Zle's behavior for GNU Readline:

```
# This goes in $HOME/.inputrc
set editing-mode vi
set show-mode-in-prompt on
set vi-cmd-mode-string "\1\e[2 q\2"
set vi-ins-mode-string "\1\e[6 q\2"
# Previous three lines are from https://stackoverflow.com/a/48449104
```

and then Python will behave consistently.


[1]: https://docs.python.org/3.8/library/readline.html
[2]: https://www.gnu.org/software/bash/manual/html_node/Readline-Init-File.html