Clone this repo:
  1. 03d39db [rustyline] Update to work with the latest nix. by Aaron Drew · 1 year, 4 months ago main master
  2. 3965fc7 [rust] Avoid warning for unused fields by Adrian Danis · 2 years, 6 months ago
  3. aa51ffe [rustyline] Add host support for unix systems. by Ben Wright · 3 years, 7 months ago
  4. 196fd4e [rust] Remove dependency on fuchsia-device library by Benjamin Brittain · 4 years, 3 months ago
  5. dddd9c9 Allow deprecated warnings until we remove try!() by Tyler Mandry · 4 years, 6 months ago

RustyLine

Build Status Build Status dependency status Docs

Readline implementation in Rust that is based on Antirez' Linenoise

Supported Platforms

  • Unix (tested on FreeBSD, Linux and macOS)
  • Windows
    • cmd.exe
    • Powershell

Note:

  • Powershell ISE is not supported, check issue #56
  • Mintty (Cygwin/Mingw) is not supported

Example

extern crate rustyline;

use rustyline::error::ReadlineError;
use rustyline::Editor;

fn main() {
    // `()` can be used when no completer is required
    let mut rl = Editor::<()>::new();
    if rl.load_history("history.txt").is_err() {
        println!("No previous history.");
    }
    loop {
        let readline = rl.readline(">> ");
        match readline {
            Ok(line) => {
                rl.add_history_entry(line.as_ref());
                println!("Line: {}", line);
            },
            Err(ReadlineError::Interrupted) => {
                println!("CTRL-C");
                break
            },
            Err(ReadlineError::Eof) => {
                println!("CTRL-D");
                break
            },
            Err(err) => {
                println!("Error: {:?}", err);
                break
            }
        }
    }
    rl.save_history("history.txt").unwrap();
}

crates.io

You can use this package in your project by adding the following to your Cargo.toml:

[dependencies]
rustyline = "2.1.0"

Features

Actions

For all modes:

KeystrokeAction
HomeMove cursor to the beginning of line
EndMove cursor to end of line
LeftMove cursor one character left
RightMove cursor one character right
Ctrl-CInterrupt/Cancel edition
Ctrl-D, Del(if line is not empty) Delete character under cursor
Ctrl-D(if line is empty) End of File
Ctrl-J, Ctrl-M, EnterFinish the line entry
Ctrl-RReverse Search history (Ctrl-S forward, Ctrl-G cancel)
Ctrl-TTranspose previous character with current character
Ctrl-UDelete from start of line to cursor
Ctrl-VInsert any special character without perfoming its associated action (#65)
Ctrl-WDelete word leading up to cursor (using white space as a word boundary)
Ctrl-YPaste from Yank buffer
Ctrl-ZSuspend (unix only)
Ctrl-_Undo

Emacs mode (default mode)

KeystrokeAction
Ctrl-A, HomeMove cursor to the beginning of line
Ctrl-B, LeftMove cursor one character left
Ctrl-E, EndMove cursor to end of line
Ctrl-F, RightMove cursor one character right
Ctrl-H, BackSpaceDelete character before cursor
Ctrl-I, TabNext completion
Ctrl-KDelete from cursor to end of line
Ctrl-LClear screen
Ctrl-N, DownNext match from history
Ctrl-P, UpPrevious match from history
Ctrl-X Ctrl-UUndo
Ctrl-YPaste from Yank buffer (Meta-Y to paste next yank instead)
Meta-<Move to first entry in history
Meta->Move to last entry in history
Meta-B, Alt-LeftMove cursor to previous word
Meta-CCapitalize the current word
Meta-DDelete forwards one word
Meta-F, Alt-RightMove cursor to next word
Meta-LLower-case the next word
Meta-TTranspose words
Meta-UUpper-case the next word
Meta-YSee Ctrl-Y
Meta-BackSpaceKill from the start of the current word, or, if between words, to the start of the previous word
Meta-0, 1, ..., -Specify the digit to the argument. starts a negative argument.

Readline Emacs Editing Mode Cheat Sheet

Vi command mode

KeystrokeAction
$, EndMove cursor to end of line
.Redo the last text modification
;Redo the last character finding command
,Redo the last character finding command in opposite direction
0, HomeMove cursor to the beginning of line
^Move to the first non-blank character of line
aInsert after cursor
AInsert at the end of line
bMove one word or token left
BMove one non-blank word left
cChange text of a movement command
CChange text to the end of line (equivalent to c$)
dDelete text of a movement command
D, Ctrl-KDelete to the end of the line
eMove to the end of the current word
EMove to the end of the current non-blank word
fMove right to the next occurance of char
FMove left to the previous occurance of char
h, Ctrl-H, BackSpaceMove one character left
l, SpaceMove one character right
Ctrl-LClear screen
iInsert before cursor
IInsert at the beginning of line
+, j, Ctrl-NMove forward one command in history
-, k, Ctrl-PMove backward one command in history
pInsert the yanked text at the cursor (paste)
PInsert the yanked text before the cursor
rReplaces a single character under the cursor (without leaving command mode)
sDelete a single character under the cursor and enter input mode
SChange current line (equivalent to 0c$)
tMove right to the next occurance of char, then one char backward
TMove left to the previous occurance of char, then one char forward
uUndo
wMove one word or token right
WMove one non-blank word right
xDelete a single character under the cursor
XDelete a character before the cursor
yYank a movement into buffer (copy)

Vi insert mode

KeystrokeAction
Ctrl-H, BackSpaceDelete character before cursor
Ctrl-I, TabNext completion
EscSwitch to command mode

Readline VI Editing Mode Cheat Sheet

Terminal codes (ANSI/VT100)

Wine

$ cargo run --example example --target 'x86_64-pc-windows-gnu'
...
Error: Io(Error { repr: Os { code: 6, message: "Invalid handle." } })
$ wineconsole --backend=curses target/x86_64-pc-windows-gnu/debug/examples/example.exe
...

Terminal checks

$ # current settings of all terminal attributes:
$ stty -a
$ # key bindings:
$ bind -p

Similar projects

LibraryLangOSTermUnicodeHistoryCompletionKeymapKill RingUndoColorsHint/Auto suggest
Go-promptGoUx/winANSIYesYesanyEmacs/progNoNoYesYes
HaskelineHaskellUx/WinAnyYesYesanyEmacs/Vi/confYesYes??
LinenoiseCUxANSINoYesonly lineEmacsNoNoUxYes
Linenoise-ngCUx/WinANSIYesYesonly lineEmacsYesNo??
LinefeedRustUx/WinAnyYesanyEmacs/confYesNo?No
LinerRustUxANSINo inc searchonly wordEmacs/Vi/progNoYesUxHistory based
Prompt-toolkitPythonUx/WinANSIYesYesanyEmacs/Vi/confYesYesUx/WinYes
Rb-readlineRubyUx/WinANSIYesYesonly wordEmacs/Vi/confYesYes?No
ReplxxC/C++Ux/WinANSIYesYesonly lineEmacsYesNoUx/WinYes
RustylineRustUx/WinANSIYesYesanyEmacs/Vi/bindYesYesUx/Win 10+Yes