Irb::Autosuggestions

Gem Version RubyGems Downloads CI License Ruby

Irb::Autosuggestions

No need to explain. Fish-like autosuggestions for IRB — ghost text from history as you type.

Contents

Installation

Add to your Gemfile:

gem 'irb-autosuggestions'

~/.irbrc:

require 'irb-autosuggestions'

Usage

Start typing in IRB. Ghost text appears after the cursor, showing the most recent matching history entry with syntax coloring (or gray if colorization is unavailable or disabled):

irb(main):001* [1,2,3].map do |el|
irb(main):002*   el.succ      <- "cc" in gray
irb(main):003> end            <- "d" in gray

Press right arrow (->) to accept the full multiline suggestion.

Prefix-filtered history navigation

Up/down arrows navigate history filtered by the typed prefix (like zsh). Start typing and press up — only entries matching your prefix are shown. The prefix is frozen on the first press; subsequent presses keep searching within that prefix:

irb(main):001:0> def          <- type "def", press up
irb(main):001:0> def foo      <- press up again then older "def*" entry

Press right arrow to accept the suggestion and exit prefix mode. Any non-history key (letter, enter, etc.) resets the prefix anchor, returning arrows to normal unfiltered browsing.

Consecutive duplicate entries are collapsed during prefix search so each unique line appears once.

Regular unfiltered history browsing (empty buffer + up arrow) is unchanged — all entries are shown including duplicates.

To disable prefix navigation while keeping ghost text:

IRB.conf[:USE_PREFIX_HISTORY_NAVIGATION] = false

Or via environment variable:

export IRB_PREFIX_HISTORY_NAVIGATION=0

Configuration

Autosuggestions are enabled by default. To disable:

~/.irbrc:

IRB.conf[:USE_AUTOSUGGESTIONS] = false

Or via environment variable:

export IRB_AUTOSUGGESTIONS=0

Colors

Syntax-colored ghost text is enabled by default when IRB::Color is available and IRB.conf[:USE_COLORIZE] is true.

To disable colored ghost text (falls back to plain gray):

IRB.conf[:USE_COLORIZE] = false

Or from command line:

irb --nocolorize

[!NOTE] Colorized ghost rendering may behave differently across terminal emulators, Ruby versions, and IRB color schemes. If you notice visual artifacts (e.g., wrong colors, underlines, or unusual brightness), try disabling the feature or switch to the gray fallback or create new issue.

How it works

  • Each keystroke queries Reline::HISTORY for the most recent entry whose prefix matches the current buffer.
  • The suggestion is rendered inline as ghost text without modifying the buffer.
  • When available, the ghost uses IRB::Color.colorize_code to match IRB's syntax colors, dimmed via ANSI escape codes for visual distinction.
  • Extra ghost lines (for multiline history entries) are drawn below the prompt.
  • Ghost artifacts are cleared each frame using cursor save/restore (\e[s/\e[u) and per-line clearing (\e[2K), avoiding \e[J which interfered with Reline's cursor tracking.
  • Right arrow triggers ed_next_char, which replaces the buffer with the ghost text.
  • Up/down arrows use prefix-filtered navigation when the buffer is non-empty, falling back to unfiltered browsing when nothing was typed.

Development

bin/setup
bundle exec rake          # spec + rubocop

License

MIT