Home

Awesome

RustyLine

Build Status dependency status Docs

Readline implementation in Rust that is based on Antirez' Linenoise

Supported Platforms

Note:

Example

use rustyline::error::ReadlineError;
use rustyline::{DefaultEditor, Result};

fn main() -> Result<()> {
    // `()` can be used when no completer is required
    let mut rl = DefaultEditor::new()?;
    #[cfg(feature = "with-file-history")]
    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_str());
                println!("Line: {}", line);
            },
            Err(ReadlineError::Interrupted) => {
                println!("CTRL-C");
                break
            },
            Err(ReadlineError::Eof) => {
                println!("CTRL-D");
                break
            },
            Err(err) => {
                println!("Error: {:?}", err);
                break
            }
        }
    }
    #[cfg(feature = "with-file-history")]
    rl.save_history("history.txt");
    Ok(())
}

crates.io

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

[dependencies]
rustyline = "14.0.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 performing 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
c<movement>Change text of a movement command
CChange text to the end of line (equivalent to c$)
d<movement>Delete 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
f<char>Move right to the next occurrence of char
F<char>Move left to the previous occurrence 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$)
t<char>Move right to the next occurrence of char, then one char backward
T<char>Move left to the previous occurrence 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
y<movement>Yank 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
$ # print out a terminfo description:
$ infocmp

Similar projects

LibraryLangOSTermUnicodeHistoryCompletionKeymapKill RingUndoColorsHint/Auto suggest
go-promptGoUx/winANSIYesYesanyEmacs/progNoNoYesYes
HaskelineHaskellUx/WinAnyYesYesanyEmacs/vi/confYesYes??
isoclineCUx/WinANSIYesYesanyEmacsNoYesYesYes
linefeedRustUx/WinAnyYesanyEmacs/confYesNo?No
linenoiseCUxANSINoYesonly lineEmacsNoNoUxYes
LinerRustUxANSINo inc searchonly wordEmacs/vi/progNoYesUxHistory based
prompt_toolkitPythonUx/WinANSIYesYesanyEmacs/vi/confYesYesUx/WinYes
reedlineRustUx/WinANSIYesYesanyEmacs/vi/bindNoYesUx/WinYes
replxxC/C++Ux/WinANSIYesYesonly lineEmacsYesNoUx/WinYes
RustylineRustUx/WinANSIYesYesanyEmacs/vi/bindYesYesUx/Win 10+Yes
termwizRustUx/WinAny?YesanyEmacsNoNoUx/WinNo

Multi line support

This is a very simple feature that simply causes lines that are longer than the current terminal width to be displayed on the next visual line instead of horizontally scrolling as more characters are typed. Currently, this feature is always enabled and there is no configuration option to disable it.

This feature does not allow the end user to hit a special key sequence and enter a mode where hitting the return key will cause a literal newline to be added to the input buffer.

The way to achieve multi-line editing is to implement the Validator trait.