Question

I wonder if there's a way to use ipython-like completion in zsh?

What I mean is in ipython you could type several characters and then loop through your command history, but this will only affect those commands which start with characters you've typed in the first place.
If your history look like this:

token = 'something blah blah'    
import os
token.split()

..and then you type token and loop through history in ipython you will get only 1st and 3rd lines.

So is there any way to make zsh work this way?

Was it helpful?

Solution

You can use the widgets history-beginning-search-backward and history-beginning-search-forward for that. By default they are not bound to any keys so you'll have to do that with bindkey.

bindkey "^[[5~" history-beginning-search-backward
bindkey "^[[6~" history-beginning-search-forward

Where ^[[5~ is the code for page up and [[6~ for page down. These codes may be different for your terminal.

You can either use cat -v to show the codes for the non-printing characters. Or you can use the associative array terminfo from the zsh/terminfo module (which may already be loaded; see zshmodules(1) and zshbuiltins(1) for more infos on zsh modules) which should contain the correct codes in the keys knp (next-page key) and kpp (previous-page key):

if (( ${+terminfo[knp]} )) && (( ${+terminfo[kpp]} )); then
     bindkey "${terminfo[kpp]}" history-beginning-search-backward
     bindkey "${terminfo[knp]}" history-beginning-search-forward
fi

To be honest these widgets will not loop when they reach the beginning or end of history, but as you can go both directions (and considering that ipython does not loop either) that should not be a real problem.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top