CPython Interactive Reading Scheme - Better Deletion of Words Back

Is it possible to change word boundaries for readlinein CPython 2.7 or 3.3?

I want backward-kill-word(attached to comfortable C-w) and backward-wordhas exactly the same word boundaries as forward-wordand forward-kill-word. Currently C-werases half a line without regard to syntax, periods , etc. And stretched for M-DELfor a more reasonable removal back too much trouble. I also do not want to use IPython at the moment.

Just reconfiguring C-wto act like M-DELit would be nice (setting it to backward-kill-worddoes nothing, because the function M-DELis probably called something else.)

Update : it gets weird!

>>> import readline
>>> readline.parse_and_bind('"C-k": backward-kill-word')
(press up, press C-k a lot, witness it working)
>>> readline.parse_and_bind('"\\C-w": backward-kill-word')
(press up, press C-w, and see that its function did not change, it wasn't re-bound!)
+4
source share
2 answers

Answering my own question because I found the answer here:

https://superuser.com/questions/212446/binding-backward-kill-word-to-ctrlw

Short answer: besides adding

import readline
readline.parse_and_bind('"\\C-w": backward-kill-word')

before ~/.pythonrc.py, do the following:

stty werase undef

sometime before running python in the same terminal. This will gain control C-wof the terminal.

Do not delete the question if someone stumbles about a problem in an interactive Python context.

+3
source

Based on @ mischa-arefiev's answer, I now have the following in my file PYTHONSTARTUP, which works for ipython2 and ipython3:

import readline
import subprocess
readline.parse_and_bind('"\\C-w": backward-kill-word')
subprocess.call(['stty', 'werase', 'undef'])

I use it on Linux, and I'm not sure if it also works on Mac.

+1

Source: https://habr.com/ru/post/1525085/


All Articles