Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding fuzzy history search to IPython #12231

Open
NightMachinery opened this issue Apr 11, 2020 · 5 comments
Open

Adding fuzzy history search to IPython #12231

NightMachinery opened this issue Apr 11, 2020 · 5 comments

Comments

@NightMachinery
Copy link

NightMachinery commented Apr 11, 2020

I have cobbled up the following:

# Install https://github.com/nk412/pyfzf first
from IPython.core.history import HistoryAccessor
hista = HistoryAccessor()
from pyfzf.pyfzf import FzfPrompt
fzf = FzfPrompt()
def gethistf():
    return fzf.prompt([line for n1, n2, line in hista.get_tail(2000)], "--bind 'tab:toggle,shift-tab:toggle+beginning-of-line+kill-line,?:toggle-preview,ctrl-j:toggle+beginning-of-line+kill-line,ctrl-t:top' --multi")

Example usage:
image

In [1]: gethistf()
Out[1]: ["r = g.search('(from:dan@tldrnewsletter.com AND NOT label:auto/processed)', maxResults=20)"]

Now I have these problems:

  • How do I set the current line's command to list(gethistf())?
  • How do register a keyboard hotkey to call my function? (A widget thing, e.g., CTRL+r will call myfunction with the current line's contents.)
  • What's the best way of sourcing these code changes for all future IPython sessions?
@Carreau
Copy link
Member

Carreau commented Apr 13, 2020

Thanks for your question and the work you did, apologies my response is going to be a bit terse, but I'll try to come back to it later if you haven't figured it out

The CLI use Python Prompt toolkit for most shortcut and UI, so some documentation can be found there.

IPython/terminal/shortcuts.py is the file that handle shortcuts. Here is some docs about shortcuts (though it might be a bit old). https://ipython.readthedocs.io/en/stable/config/details.html#keyboard-shortcuts

You will see in IPython/terminal/interactiveshell.py ~line 314:

self.pt_app = PromptSession(
                            editing_mode=editing_mode,
                            key_bindings=key_bindings,
                            history=history,
                            completer=IPythonPTCompleter(shell=self),
                            enable_history_search = self.enable_history_search,
                            style=self.style,
                            include_default_pygments_style=False,
                            mouse_support=self.mouse_support,
                            enable_open_in_editor=self.extra_open_editor_shortcuts,
                            color_depth=self.color_depth,
                            tempfile_suffix=".py",
                            **self._extra_prompt_options())

That we pass the history to prompt toolkit, so I'm quasi-sure that history searching is handled by prompt toolkit, you might find more informations there.

As you will see prompt toolkit is not readline so might not work with the .prompt() method you use, but prompt toolkit should be able display a similar UI, it would be great.

@GHPS
Copy link

GHPS commented Jun 5, 2020

@ NightMachinary The idea is good - so good that someone had
it before:

https://github.com/tbxy09/ipython_plugin

Have you tried this plugin?

@NightMachinery
Copy link
Author

@GHPS I will take a look at it.

@infokiller
Copy link

I implemented fzf history integration a while ago: https://github.com/infokiller/config-public/blob/master/.config/ipython/profile_default/startup/ext/fzf_history.py

It supports preview with syntax highlighting via Pygments, selecting multiple commands, multi-line commands, and maybe other stuff.
I plan to release it as an IPython extension.

@iloveitaly
Copy link
Contributor

This post has a great script for this, including multi-line fzf search. Here's the full script in my dotfiles.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants