11cb0ef41Sopenharmony_ci<!DOCTYPE html> 21cb0ef41Sopenharmony_ci<html lang="en"> 31cb0ef41Sopenharmony_ci<head> 41cb0ef41Sopenharmony_ci <meta charset="utf-8"> 51cb0ef41Sopenharmony_ci <meta name="viewport" content="width=device-width"> 61cb0ef41Sopenharmony_ci <meta name="nodejs.org:node-version" content="v18.20.1"> 71cb0ef41Sopenharmony_ci <title>Readline | Node.js v18.20.1 Documentation</title> 81cb0ef41Sopenharmony_ci <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:400,700,400italic&display=fallback"> 91cb0ef41Sopenharmony_ci <link rel="stylesheet" href="assets/style.css"> 101cb0ef41Sopenharmony_ci <link rel="stylesheet" href="assets/hljs.css"> 111cb0ef41Sopenharmony_ci <link rel="canonical" href="https://nodejs.org/api/readline.html"> 121cb0ef41Sopenharmony_ci <script async defer src="assets/api.js" type="text/javascript"></script> 131cb0ef41Sopenharmony_ci <style>@media(max-width:630px){.with-51-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}@media(max-width:558px){.with-42-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}@media(max-width:742px){.with-65-chars>.js-flavor-selector{float:none;margin:0 0 1em auto;}}</style> 141cb0ef41Sopenharmony_ci</head> 151cb0ef41Sopenharmony_ci<body class="alt apidoc" id="api-section-readline"> 161cb0ef41Sopenharmony_ci <div id="content" class="clearfix"> 171cb0ef41Sopenharmony_ci <div id="column2" class="interior"> 181cb0ef41Sopenharmony_ci <div id="intro" class="interior"> 191cb0ef41Sopenharmony_ci <a href="/" title="Go back to the home page"> 201cb0ef41Sopenharmony_ci Node.js 211cb0ef41Sopenharmony_ci </a> 221cb0ef41Sopenharmony_ci </div> 231cb0ef41Sopenharmony_ci <ul> 241cb0ef41Sopenharmony_ci<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li> 251cb0ef41Sopenharmony_ci<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li> 261cb0ef41Sopenharmony_ci</ul> 271cb0ef41Sopenharmony_ci<hr class="line"> 281cb0ef41Sopenharmony_ci<ul> 291cb0ef41Sopenharmony_ci<li><a href="assert.html" class="nav-assert">Assertion testing</a></li> 301cb0ef41Sopenharmony_ci<li><a href="async_context.html" class="nav-async_context">Asynchronous context tracking</a></li> 311cb0ef41Sopenharmony_ci<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li> 321cb0ef41Sopenharmony_ci<li><a href="buffer.html" class="nav-buffer">Buffer</a></li> 331cb0ef41Sopenharmony_ci<li><a href="addons.html" class="nav-addons">C++ addons</a></li> 341cb0ef41Sopenharmony_ci<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li> 351cb0ef41Sopenharmony_ci<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li> 361cb0ef41Sopenharmony_ci<li><a href="child_process.html" class="nav-child_process">Child processes</a></li> 371cb0ef41Sopenharmony_ci<li><a href="cluster.html" class="nav-cluster">Cluster</a></li> 381cb0ef41Sopenharmony_ci<li><a href="cli.html" class="nav-cli">Command-line options</a></li> 391cb0ef41Sopenharmony_ci<li><a href="console.html" class="nav-console">Console</a></li> 401cb0ef41Sopenharmony_ci<li><a href="corepack.html" class="nav-corepack">Corepack</a></li> 411cb0ef41Sopenharmony_ci<li><a href="crypto.html" class="nav-crypto">Crypto</a></li> 421cb0ef41Sopenharmony_ci<li><a href="debugger.html" class="nav-debugger">Debugger</a></li> 431cb0ef41Sopenharmony_ci<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li> 441cb0ef41Sopenharmony_ci<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li> 451cb0ef41Sopenharmony_ci<li><a href="dns.html" class="nav-dns">DNS</a></li> 461cb0ef41Sopenharmony_ci<li><a href="domain.html" class="nav-domain">Domain</a></li> 471cb0ef41Sopenharmony_ci<li><a href="errors.html" class="nav-errors">Errors</a></li> 481cb0ef41Sopenharmony_ci<li><a href="events.html" class="nav-events">Events</a></li> 491cb0ef41Sopenharmony_ci<li><a href="fs.html" class="nav-fs">File system</a></li> 501cb0ef41Sopenharmony_ci<li><a href="globals.html" class="nav-globals">Globals</a></li> 511cb0ef41Sopenharmony_ci<li><a href="http.html" class="nav-http">HTTP</a></li> 521cb0ef41Sopenharmony_ci<li><a href="http2.html" class="nav-http2">HTTP/2</a></li> 531cb0ef41Sopenharmony_ci<li><a href="https.html" class="nav-https">HTTPS</a></li> 541cb0ef41Sopenharmony_ci<li><a href="inspector.html" class="nav-inspector">Inspector</a></li> 551cb0ef41Sopenharmony_ci<li><a href="intl.html" class="nav-intl">Internationalization</a></li> 561cb0ef41Sopenharmony_ci<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li> 571cb0ef41Sopenharmony_ci<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li> 581cb0ef41Sopenharmony_ci<li><a href="module.html" class="nav-module">Modules: <code>node:module</code> API</a></li> 591cb0ef41Sopenharmony_ci<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li> 601cb0ef41Sopenharmony_ci<li><a href="net.html" class="nav-net">Net</a></li> 611cb0ef41Sopenharmony_ci<li><a href="os.html" class="nav-os">OS</a></li> 621cb0ef41Sopenharmony_ci<li><a href="path.html" class="nav-path">Path</a></li> 631cb0ef41Sopenharmony_ci<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li> 641cb0ef41Sopenharmony_ci<li><a href="permissions.html" class="nav-permissions">Permissions</a></li> 651cb0ef41Sopenharmony_ci<li><a href="process.html" class="nav-process">Process</a></li> 661cb0ef41Sopenharmony_ci<li><a href="punycode.html" class="nav-punycode">Punycode</a></li> 671cb0ef41Sopenharmony_ci<li><a href="querystring.html" class="nav-querystring">Query strings</a></li> 681cb0ef41Sopenharmony_ci<li><a href="readline.html" class="nav-readline active">Readline</a></li> 691cb0ef41Sopenharmony_ci<li><a href="repl.html" class="nav-repl">REPL</a></li> 701cb0ef41Sopenharmony_ci<li><a href="report.html" class="nav-report">Report</a></li> 711cb0ef41Sopenharmony_ci<li><a href="single-executable-applications.html" class="nav-single-executable-applications">Single executable applications</a></li> 721cb0ef41Sopenharmony_ci<li><a href="stream.html" class="nav-stream">Stream</a></li> 731cb0ef41Sopenharmony_ci<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li> 741cb0ef41Sopenharmony_ci<li><a href="test.html" class="nav-test">Test runner</a></li> 751cb0ef41Sopenharmony_ci<li><a href="timers.html" class="nav-timers">Timers</a></li> 761cb0ef41Sopenharmony_ci<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li> 771cb0ef41Sopenharmony_ci<li><a href="tracing.html" class="nav-tracing">Trace events</a></li> 781cb0ef41Sopenharmony_ci<li><a href="tty.html" class="nav-tty">TTY</a></li> 791cb0ef41Sopenharmony_ci<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li> 801cb0ef41Sopenharmony_ci<li><a href="url.html" class="nav-url">URL</a></li> 811cb0ef41Sopenharmony_ci<li><a href="util.html" class="nav-util">Utilities</a></li> 821cb0ef41Sopenharmony_ci<li><a href="v8.html" class="nav-v8">V8</a></li> 831cb0ef41Sopenharmony_ci<li><a href="vm.html" class="nav-vm">VM</a></li> 841cb0ef41Sopenharmony_ci<li><a href="wasi.html" class="nav-wasi">WASI</a></li> 851cb0ef41Sopenharmony_ci<li><a href="webcrypto.html" class="nav-webcrypto">Web Crypto API</a></li> 861cb0ef41Sopenharmony_ci<li><a href="webstreams.html" class="nav-webstreams">Web Streams API</a></li> 871cb0ef41Sopenharmony_ci<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li> 881cb0ef41Sopenharmony_ci<li><a href="zlib.html" class="nav-zlib">Zlib</a></li> 891cb0ef41Sopenharmony_ci</ul> 901cb0ef41Sopenharmony_ci<hr class="line"> 911cb0ef41Sopenharmony_ci<ul> 921cb0ef41Sopenharmony_ci<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li> 931cb0ef41Sopenharmony_ci</ul> 941cb0ef41Sopenharmony_ci </div> 951cb0ef41Sopenharmony_ci 961cb0ef41Sopenharmony_ci <div id="column1" data-id="readline" class="interior"> 971cb0ef41Sopenharmony_ci <header class="header"> 981cb0ef41Sopenharmony_ci <div class="header-container"> 991cb0ef41Sopenharmony_ci <h1>Node.js v18.20.1 documentation</h1> 1001cb0ef41Sopenharmony_ci <button class="theme-toggle-btn" id="theme-toggle-btn" title="Toggle dark mode/light mode" aria-label="Toggle dark mode/light mode" hidden> 1011cb0ef41Sopenharmony_ci <svg xmlns="http://www.w3.org/2000/svg" class="icon dark-icon" height="24" width="24"> 1021cb0ef41Sopenharmony_ci <path fill="none" d="M0 0h24v24H0z" /> 1031cb0ef41Sopenharmony_ci <path d="M11.1 12.08c-2.33-4.51-.5-8.48.53-10.07C6.27 2.2 1.98 6.59 1.98 12c0 .14.02.28.02.42.62-.27 1.29-.42 2-.42 1.66 0 3.18.83 4.1 2.15A4.01 4.01 0 0111 18c0 1.52-.87 2.83-2.12 3.51.98.32 2.03.5 3.11.5 3.5 0 6.58-1.8 8.37-4.52-2.36.23-6.98-.97-9.26-5.41z"/> 1041cb0ef41Sopenharmony_ci <path d="M7 16h-.18C6.4 14.84 5.3 14 4 14c-1.66 0-3 1.34-3 3s1.34 3 3 3h3c1.1 0 2-.9 2-2s-.9-2-2-2z"/> 1051cb0ef41Sopenharmony_ci </svg> 1061cb0ef41Sopenharmony_ci <svg xmlns="http://www.w3.org/2000/svg" class="icon light-icon" height="24" width="24"> 1071cb0ef41Sopenharmony_ci <path d="M0 0h24v24H0z" fill="none" /> 1081cb0ef41Sopenharmony_ci <path d="M6.76 4.84l-1.8-1.79-1.41 1.41 1.79 1.79 1.42-1.41zM4 10.5H1v2h3v-2zm9-9.95h-2V3.5h2V.55zm7.45 3.91l-1.41-1.41-1.79 1.79 1.41 1.41 1.79-1.79zm-3.21 13.7l1.79 1.8 1.41-1.41-1.8-1.79-1.4 1.4zM20 10.5v2h3v-2h-3zm-8-5c-3.31 0-6 2.69-6 6s2.69 6 6 6 6-2.69 6-6-2.69-6-6-6zm-1 16.95h2V19.5h-2v2.95zm-7.45-3.91l1.41 1.41 1.79-1.8-1.41-1.41-1.79 1.8z"/> 1091cb0ef41Sopenharmony_ci </svg> 1101cb0ef41Sopenharmony_ci </button> 1111cb0ef41Sopenharmony_ci </div> 1121cb0ef41Sopenharmony_ci <div id="gtoc"> 1131cb0ef41Sopenharmony_ci <ul> 1141cb0ef41Sopenharmony_ci <li class="pinned-header">Node.js v18.20.1</li> 1151cb0ef41Sopenharmony_ci 1161cb0ef41Sopenharmony_ci <li class="picker-header"> 1171cb0ef41Sopenharmony_ci <a href="#"> 1181cb0ef41Sopenharmony_ci <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> 1191cb0ef41Sopenharmony_ci Table of contents 1201cb0ef41Sopenharmony_ci </a> 1211cb0ef41Sopenharmony_ci 1221cb0ef41Sopenharmony_ci <div class="picker"><div class="toc"><ul> 1231cb0ef41Sopenharmony_ci<li><span class="stability_2"><a href="#readline">Readline</a></span> 1241cb0ef41Sopenharmony_ci<ul> 1251cb0ef41Sopenharmony_ci<li><a href="#class-interfaceconstructor">Class: <code>InterfaceConstructor</code></a> 1261cb0ef41Sopenharmony_ci<ul> 1271cb0ef41Sopenharmony_ci<li><a href="#event-close">Event: <code>'close'</code></a></li> 1281cb0ef41Sopenharmony_ci<li><a href="#event-line">Event: <code>'line'</code></a></li> 1291cb0ef41Sopenharmony_ci<li><a href="#event-history">Event: <code>'history'</code></a></li> 1301cb0ef41Sopenharmony_ci<li><a href="#event-pause">Event: <code>'pause'</code></a></li> 1311cb0ef41Sopenharmony_ci<li><a href="#event-resume">Event: <code>'resume'</code></a></li> 1321cb0ef41Sopenharmony_ci<li><a href="#event-sigcont">Event: <code>'SIGCONT'</code></a></li> 1331cb0ef41Sopenharmony_ci<li><a href="#event-sigint">Event: <code>'SIGINT'</code></a></li> 1341cb0ef41Sopenharmony_ci<li><a href="#event-sigtstp">Event: <code>'SIGTSTP'</code></a></li> 1351cb0ef41Sopenharmony_ci<li><a href="#rlclose"><code>rl.close()</code></a></li> 1361cb0ef41Sopenharmony_ci<li><a href="#rlpause"><code>rl.pause()</code></a></li> 1371cb0ef41Sopenharmony_ci<li><a href="#rlpromptpreservecursor"><code>rl.prompt([preserveCursor])</code></a></li> 1381cb0ef41Sopenharmony_ci<li><a href="#rlquestionquery-options-callback"><code>rl.question(query[, options], callback)</code></a></li> 1391cb0ef41Sopenharmony_ci<li><a href="#rlresume"><code>rl.resume()</code></a></li> 1401cb0ef41Sopenharmony_ci<li><a href="#rlsetpromptprompt"><code>rl.setPrompt(prompt)</code></a></li> 1411cb0ef41Sopenharmony_ci<li><a href="#rlgetprompt"><code>rl.getPrompt()</code></a></li> 1421cb0ef41Sopenharmony_ci<li><a href="#rlwritedata-key"><code>rl.write(data[, key])</code></a></li> 1431cb0ef41Sopenharmony_ci<li><a href="#rlsymbolasynciterator"><code>rl[Symbol.asyncIterator]()</code></a></li> 1441cb0ef41Sopenharmony_ci<li><a href="#rlline"><code>rl.line</code></a></li> 1451cb0ef41Sopenharmony_ci<li><a href="#rlcursor"><code>rl.cursor</code></a></li> 1461cb0ef41Sopenharmony_ci<li><a href="#rlgetcursorpos"><code>rl.getCursorPos()</code></a></li> 1471cb0ef41Sopenharmony_ci</ul> 1481cb0ef41Sopenharmony_ci</li> 1491cb0ef41Sopenharmony_ci<li><span class="stability_1"><a href="#promises-api">Promises API</a></span> 1501cb0ef41Sopenharmony_ci<ul> 1511cb0ef41Sopenharmony_ci<li><a href="#class-readlinepromisesinterface">Class: <code>readlinePromises.Interface</code></a> 1521cb0ef41Sopenharmony_ci<ul> 1531cb0ef41Sopenharmony_ci<li><a href="#rlquestionquery-options"><code>rl.question(query[, options])</code></a></li> 1541cb0ef41Sopenharmony_ci</ul> 1551cb0ef41Sopenharmony_ci</li> 1561cb0ef41Sopenharmony_ci<li><a href="#class-readlinepromisesreadline">Class: <code>readlinePromises.Readline</code></a> 1571cb0ef41Sopenharmony_ci<ul> 1581cb0ef41Sopenharmony_ci<li><a href="#new-readlinepromisesreadlinestream-options"><code>new readlinePromises.Readline(stream[, options])</code></a></li> 1591cb0ef41Sopenharmony_ci<li><a href="#rlclearlinedir"><code>rl.clearLine(dir)</code></a></li> 1601cb0ef41Sopenharmony_ci<li><a href="#rlclearscreendown"><code>rl.clearScreenDown()</code></a></li> 1611cb0ef41Sopenharmony_ci<li><a href="#rlcommit"><code>rl.commit()</code></a></li> 1621cb0ef41Sopenharmony_ci<li><a href="#rlcursortox-y"><code>rl.cursorTo(x[, y])</code></a></li> 1631cb0ef41Sopenharmony_ci<li><a href="#rlmovecursordx-dy"><code>rl.moveCursor(dx, dy)</code></a></li> 1641cb0ef41Sopenharmony_ci<li><a href="#rlrollback"><code>rl.rollback()</code></a></li> 1651cb0ef41Sopenharmony_ci</ul> 1661cb0ef41Sopenharmony_ci</li> 1671cb0ef41Sopenharmony_ci<li><a href="#readlinepromisescreateinterfaceoptions"><code>readlinePromises.createInterface(options)</code></a> 1681cb0ef41Sopenharmony_ci<ul> 1691cb0ef41Sopenharmony_ci<li><a href="#use-of-the-completer-function">Use of the <code>completer</code> function</a></li> 1701cb0ef41Sopenharmony_ci</ul> 1711cb0ef41Sopenharmony_ci</li> 1721cb0ef41Sopenharmony_ci</ul> 1731cb0ef41Sopenharmony_ci</li> 1741cb0ef41Sopenharmony_ci<li><a href="#callback-api">Callback API</a> 1751cb0ef41Sopenharmony_ci<ul> 1761cb0ef41Sopenharmony_ci<li><a href="#class-readlineinterface">Class: <code>readline.Interface</code></a> 1771cb0ef41Sopenharmony_ci<ul> 1781cb0ef41Sopenharmony_ci<li><a href="#rlquestionquery-options-callback_1"><code>rl.question(query[, options], callback)</code></a></li> 1791cb0ef41Sopenharmony_ci</ul> 1801cb0ef41Sopenharmony_ci</li> 1811cb0ef41Sopenharmony_ci<li><a href="#readlineclearlinestream-dir-callback"><code>readline.clearLine(stream, dir[, callback])</code></a></li> 1821cb0ef41Sopenharmony_ci<li><a href="#readlineclearscreendownstream-callback"><code>readline.clearScreenDown(stream[, callback])</code></a></li> 1831cb0ef41Sopenharmony_ci<li><a href="#readlinecreateinterfaceoptions"><code>readline.createInterface(options)</code></a> 1841cb0ef41Sopenharmony_ci<ul> 1851cb0ef41Sopenharmony_ci<li><a href="#use-of-the-completer-function_1">Use of the <code>completer</code> function</a></li> 1861cb0ef41Sopenharmony_ci</ul> 1871cb0ef41Sopenharmony_ci</li> 1881cb0ef41Sopenharmony_ci<li><a href="#readlinecursortostream-x-y-callback"><code>readline.cursorTo(stream, x[, y][, callback])</code></a></li> 1891cb0ef41Sopenharmony_ci<li><a href="#readlinemovecursorstream-dx-dy-callback"><code>readline.moveCursor(stream, dx, dy[, callback])</code></a></li> 1901cb0ef41Sopenharmony_ci</ul> 1911cb0ef41Sopenharmony_ci</li> 1921cb0ef41Sopenharmony_ci<li><a href="#readlineemitkeypresseventsstream-interface"><code>readline.emitKeypressEvents(stream[, interface])</code></a></li> 1931cb0ef41Sopenharmony_ci<li><a href="#example-tiny-cli">Example: Tiny CLI</a></li> 1941cb0ef41Sopenharmony_ci<li><a href="#example-read-file-stream-line-by-line">Example: Read file stream line-by-Line</a></li> 1951cb0ef41Sopenharmony_ci<li><a href="#tty-keybindings">TTY keybindings</a></li> 1961cb0ef41Sopenharmony_ci</ul> 1971cb0ef41Sopenharmony_ci</li> 1981cb0ef41Sopenharmony_ci</ul></div></div> 1991cb0ef41Sopenharmony_ci </li> 2001cb0ef41Sopenharmony_ci 2011cb0ef41Sopenharmony_ci 2021cb0ef41Sopenharmony_ci <li class="picker-header"> 2031cb0ef41Sopenharmony_ci <a href="#"> 2041cb0ef41Sopenharmony_ci <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> 2051cb0ef41Sopenharmony_ci Index 2061cb0ef41Sopenharmony_ci </a> 2071cb0ef41Sopenharmony_ci 2081cb0ef41Sopenharmony_ci <div class="picker"><ul> 2091cb0ef41Sopenharmony_ci<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li> 2101cb0ef41Sopenharmony_ci<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li> 2111cb0ef41Sopenharmony_ci 2121cb0ef41Sopenharmony_ci <li> 2131cb0ef41Sopenharmony_ci <a href="index.html">Index</a> 2141cb0ef41Sopenharmony_ci </li> 2151cb0ef41Sopenharmony_ci </ul> 2161cb0ef41Sopenharmony_ci 2171cb0ef41Sopenharmony_ci<hr class="line"> 2181cb0ef41Sopenharmony_ci<ul> 2191cb0ef41Sopenharmony_ci<li><a href="assert.html" class="nav-assert">Assertion testing</a></li> 2201cb0ef41Sopenharmony_ci<li><a href="async_context.html" class="nav-async_context">Asynchronous context tracking</a></li> 2211cb0ef41Sopenharmony_ci<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li> 2221cb0ef41Sopenharmony_ci<li><a href="buffer.html" class="nav-buffer">Buffer</a></li> 2231cb0ef41Sopenharmony_ci<li><a href="addons.html" class="nav-addons">C++ addons</a></li> 2241cb0ef41Sopenharmony_ci<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li> 2251cb0ef41Sopenharmony_ci<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li> 2261cb0ef41Sopenharmony_ci<li><a href="child_process.html" class="nav-child_process">Child processes</a></li> 2271cb0ef41Sopenharmony_ci<li><a href="cluster.html" class="nav-cluster">Cluster</a></li> 2281cb0ef41Sopenharmony_ci<li><a href="cli.html" class="nav-cli">Command-line options</a></li> 2291cb0ef41Sopenharmony_ci<li><a href="console.html" class="nav-console">Console</a></li> 2301cb0ef41Sopenharmony_ci<li><a href="corepack.html" class="nav-corepack">Corepack</a></li> 2311cb0ef41Sopenharmony_ci<li><a href="crypto.html" class="nav-crypto">Crypto</a></li> 2321cb0ef41Sopenharmony_ci<li><a href="debugger.html" class="nav-debugger">Debugger</a></li> 2331cb0ef41Sopenharmony_ci<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li> 2341cb0ef41Sopenharmony_ci<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li> 2351cb0ef41Sopenharmony_ci<li><a href="dns.html" class="nav-dns">DNS</a></li> 2361cb0ef41Sopenharmony_ci<li><a href="domain.html" class="nav-domain">Domain</a></li> 2371cb0ef41Sopenharmony_ci<li><a href="errors.html" class="nav-errors">Errors</a></li> 2381cb0ef41Sopenharmony_ci<li><a href="events.html" class="nav-events">Events</a></li> 2391cb0ef41Sopenharmony_ci<li><a href="fs.html" class="nav-fs">File system</a></li> 2401cb0ef41Sopenharmony_ci<li><a href="globals.html" class="nav-globals">Globals</a></li> 2411cb0ef41Sopenharmony_ci<li><a href="http.html" class="nav-http">HTTP</a></li> 2421cb0ef41Sopenharmony_ci<li><a href="http2.html" class="nav-http2">HTTP/2</a></li> 2431cb0ef41Sopenharmony_ci<li><a href="https.html" class="nav-https">HTTPS</a></li> 2441cb0ef41Sopenharmony_ci<li><a href="inspector.html" class="nav-inspector">Inspector</a></li> 2451cb0ef41Sopenharmony_ci<li><a href="intl.html" class="nav-intl">Internationalization</a></li> 2461cb0ef41Sopenharmony_ci<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li> 2471cb0ef41Sopenharmony_ci<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li> 2481cb0ef41Sopenharmony_ci<li><a href="module.html" class="nav-module">Modules: <code>node:module</code> API</a></li> 2491cb0ef41Sopenharmony_ci<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li> 2501cb0ef41Sopenharmony_ci<li><a href="net.html" class="nav-net">Net</a></li> 2511cb0ef41Sopenharmony_ci<li><a href="os.html" class="nav-os">OS</a></li> 2521cb0ef41Sopenharmony_ci<li><a href="path.html" class="nav-path">Path</a></li> 2531cb0ef41Sopenharmony_ci<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li> 2541cb0ef41Sopenharmony_ci<li><a href="permissions.html" class="nav-permissions">Permissions</a></li> 2551cb0ef41Sopenharmony_ci<li><a href="process.html" class="nav-process">Process</a></li> 2561cb0ef41Sopenharmony_ci<li><a href="punycode.html" class="nav-punycode">Punycode</a></li> 2571cb0ef41Sopenharmony_ci<li><a href="querystring.html" class="nav-querystring">Query strings</a></li> 2581cb0ef41Sopenharmony_ci<li><a href="readline.html" class="nav-readline active">Readline</a></li> 2591cb0ef41Sopenharmony_ci<li><a href="repl.html" class="nav-repl">REPL</a></li> 2601cb0ef41Sopenharmony_ci<li><a href="report.html" class="nav-report">Report</a></li> 2611cb0ef41Sopenharmony_ci<li><a href="single-executable-applications.html" class="nav-single-executable-applications">Single executable applications</a></li> 2621cb0ef41Sopenharmony_ci<li><a href="stream.html" class="nav-stream">Stream</a></li> 2631cb0ef41Sopenharmony_ci<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li> 2641cb0ef41Sopenharmony_ci<li><a href="test.html" class="nav-test">Test runner</a></li> 2651cb0ef41Sopenharmony_ci<li><a href="timers.html" class="nav-timers">Timers</a></li> 2661cb0ef41Sopenharmony_ci<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li> 2671cb0ef41Sopenharmony_ci<li><a href="tracing.html" class="nav-tracing">Trace events</a></li> 2681cb0ef41Sopenharmony_ci<li><a href="tty.html" class="nav-tty">TTY</a></li> 2691cb0ef41Sopenharmony_ci<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li> 2701cb0ef41Sopenharmony_ci<li><a href="url.html" class="nav-url">URL</a></li> 2711cb0ef41Sopenharmony_ci<li><a href="util.html" class="nav-util">Utilities</a></li> 2721cb0ef41Sopenharmony_ci<li><a href="v8.html" class="nav-v8">V8</a></li> 2731cb0ef41Sopenharmony_ci<li><a href="vm.html" class="nav-vm">VM</a></li> 2741cb0ef41Sopenharmony_ci<li><a href="wasi.html" class="nav-wasi">WASI</a></li> 2751cb0ef41Sopenharmony_ci<li><a href="webcrypto.html" class="nav-webcrypto">Web Crypto API</a></li> 2761cb0ef41Sopenharmony_ci<li><a href="webstreams.html" class="nav-webstreams">Web Streams API</a></li> 2771cb0ef41Sopenharmony_ci<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li> 2781cb0ef41Sopenharmony_ci<li><a href="zlib.html" class="nav-zlib">Zlib</a></li> 2791cb0ef41Sopenharmony_ci</ul> 2801cb0ef41Sopenharmony_ci<hr class="line"> 2811cb0ef41Sopenharmony_ci<ul> 2821cb0ef41Sopenharmony_ci<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li> 2831cb0ef41Sopenharmony_ci</ul></div> 2841cb0ef41Sopenharmony_ci </li> 2851cb0ef41Sopenharmony_ci 2861cb0ef41Sopenharmony_ci 2871cb0ef41Sopenharmony_ci <li class="picker-header"> 2881cb0ef41Sopenharmony_ci <a href="#"> 2891cb0ef41Sopenharmony_ci <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> 2901cb0ef41Sopenharmony_ci Other versions 2911cb0ef41Sopenharmony_ci </a> 2921cb0ef41Sopenharmony_ci <div class="picker"><ol id="alt-docs"><li><a href="https://nodejs.org/docs/latest-v21.x/api/readline.html">21.x</a></li> 2931cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v20.x/api/readline.html">20.x <b>LTS</b></a></li> 2941cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v19.x/api/readline.html">19.x</a></li> 2951cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v18.x/api/readline.html">18.x <b>LTS</b></a></li> 2961cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v17.x/api/readline.html">17.x</a></li> 2971cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v16.x/api/readline.html">16.x</a></li> 2981cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v15.x/api/readline.html">15.x</a></li> 2991cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v14.x/api/readline.html">14.x</a></li> 3001cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v13.x/api/readline.html">13.x</a></li> 3011cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v12.x/api/readline.html">12.x</a></li> 3021cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v11.x/api/readline.html">11.x</a></li> 3031cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v10.x/api/readline.html">10.x</a></li> 3041cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v9.x/api/readline.html">9.x</a></li> 3051cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v8.x/api/readline.html">8.x</a></li> 3061cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v7.x/api/readline.html">7.x</a></li> 3071cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v6.x/api/readline.html">6.x</a></li> 3081cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v5.x/api/readline.html">5.x</a></li> 3091cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v4.x/api/readline.html">4.x</a></li> 3101cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v0.12.x/api/readline.html">0.12.x</a></li> 3111cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v0.10.x/api/readline.html">0.10.x</a></li></ol></div> 3121cb0ef41Sopenharmony_ci </li> 3131cb0ef41Sopenharmony_ci 3141cb0ef41Sopenharmony_ci <li class="picker-header"> 3151cb0ef41Sopenharmony_ci <a href="#"> 3161cb0ef41Sopenharmony_ci <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> 3171cb0ef41Sopenharmony_ci Options 3181cb0ef41Sopenharmony_ci </a> 3191cb0ef41Sopenharmony_ci 3201cb0ef41Sopenharmony_ci <div class="picker"> 3211cb0ef41Sopenharmony_ci <ul> 3221cb0ef41Sopenharmony_ci <li> 3231cb0ef41Sopenharmony_ci <a href="all.html">View on single page</a> 3241cb0ef41Sopenharmony_ci </li> 3251cb0ef41Sopenharmony_ci <li> 3261cb0ef41Sopenharmony_ci <a href="readline.json">View as JSON</a> 3271cb0ef41Sopenharmony_ci </li> 3281cb0ef41Sopenharmony_ci <li class="edit_on_github"><a href="https://github.com/nodejs/node/edit/main/doc/api/readline.md">Edit on GitHub</a></li> 3291cb0ef41Sopenharmony_ci </ul> 3301cb0ef41Sopenharmony_ci </div> 3311cb0ef41Sopenharmony_ci </li> 3321cb0ef41Sopenharmony_ci </ul> 3331cb0ef41Sopenharmony_ci </div> 3341cb0ef41Sopenharmony_ci <hr> 3351cb0ef41Sopenharmony_ci </header> 3361cb0ef41Sopenharmony_ci 3371cb0ef41Sopenharmony_ci <details id="toc" open><summary>Table of contents</summary><ul> 3381cb0ef41Sopenharmony_ci<li><span class="stability_2"><a href="#readline">Readline</a></span> 3391cb0ef41Sopenharmony_ci<ul> 3401cb0ef41Sopenharmony_ci<li><a href="#class-interfaceconstructor">Class: <code>InterfaceConstructor</code></a> 3411cb0ef41Sopenharmony_ci<ul> 3421cb0ef41Sopenharmony_ci<li><a href="#event-close">Event: <code>'close'</code></a></li> 3431cb0ef41Sopenharmony_ci<li><a href="#event-line">Event: <code>'line'</code></a></li> 3441cb0ef41Sopenharmony_ci<li><a href="#event-history">Event: <code>'history'</code></a></li> 3451cb0ef41Sopenharmony_ci<li><a href="#event-pause">Event: <code>'pause'</code></a></li> 3461cb0ef41Sopenharmony_ci<li><a href="#event-resume">Event: <code>'resume'</code></a></li> 3471cb0ef41Sopenharmony_ci<li><a href="#event-sigcont">Event: <code>'SIGCONT'</code></a></li> 3481cb0ef41Sopenharmony_ci<li><a href="#event-sigint">Event: <code>'SIGINT'</code></a></li> 3491cb0ef41Sopenharmony_ci<li><a href="#event-sigtstp">Event: <code>'SIGTSTP'</code></a></li> 3501cb0ef41Sopenharmony_ci<li><a href="#rlclose"><code>rl.close()</code></a></li> 3511cb0ef41Sopenharmony_ci<li><a href="#rlpause"><code>rl.pause()</code></a></li> 3521cb0ef41Sopenharmony_ci<li><a href="#rlpromptpreservecursor"><code>rl.prompt([preserveCursor])</code></a></li> 3531cb0ef41Sopenharmony_ci<li><a href="#rlquestionquery-options-callback"><code>rl.question(query[, options], callback)</code></a></li> 3541cb0ef41Sopenharmony_ci<li><a href="#rlresume"><code>rl.resume()</code></a></li> 3551cb0ef41Sopenharmony_ci<li><a href="#rlsetpromptprompt"><code>rl.setPrompt(prompt)</code></a></li> 3561cb0ef41Sopenharmony_ci<li><a href="#rlgetprompt"><code>rl.getPrompt()</code></a></li> 3571cb0ef41Sopenharmony_ci<li><a href="#rlwritedata-key"><code>rl.write(data[, key])</code></a></li> 3581cb0ef41Sopenharmony_ci<li><a href="#rlsymbolasynciterator"><code>rl[Symbol.asyncIterator]()</code></a></li> 3591cb0ef41Sopenharmony_ci<li><a href="#rlline"><code>rl.line</code></a></li> 3601cb0ef41Sopenharmony_ci<li><a href="#rlcursor"><code>rl.cursor</code></a></li> 3611cb0ef41Sopenharmony_ci<li><a href="#rlgetcursorpos"><code>rl.getCursorPos()</code></a></li> 3621cb0ef41Sopenharmony_ci</ul> 3631cb0ef41Sopenharmony_ci</li> 3641cb0ef41Sopenharmony_ci<li><span class="stability_1"><a href="#promises-api">Promises API</a></span> 3651cb0ef41Sopenharmony_ci<ul> 3661cb0ef41Sopenharmony_ci<li><a href="#class-readlinepromisesinterface">Class: <code>readlinePromises.Interface</code></a> 3671cb0ef41Sopenharmony_ci<ul> 3681cb0ef41Sopenharmony_ci<li><a href="#rlquestionquery-options"><code>rl.question(query[, options])</code></a></li> 3691cb0ef41Sopenharmony_ci</ul> 3701cb0ef41Sopenharmony_ci</li> 3711cb0ef41Sopenharmony_ci<li><a href="#class-readlinepromisesreadline">Class: <code>readlinePromises.Readline</code></a> 3721cb0ef41Sopenharmony_ci<ul> 3731cb0ef41Sopenharmony_ci<li><a href="#new-readlinepromisesreadlinestream-options"><code>new readlinePromises.Readline(stream[, options])</code></a></li> 3741cb0ef41Sopenharmony_ci<li><a href="#rlclearlinedir"><code>rl.clearLine(dir)</code></a></li> 3751cb0ef41Sopenharmony_ci<li><a href="#rlclearscreendown"><code>rl.clearScreenDown()</code></a></li> 3761cb0ef41Sopenharmony_ci<li><a href="#rlcommit"><code>rl.commit()</code></a></li> 3771cb0ef41Sopenharmony_ci<li><a href="#rlcursortox-y"><code>rl.cursorTo(x[, y])</code></a></li> 3781cb0ef41Sopenharmony_ci<li><a href="#rlmovecursordx-dy"><code>rl.moveCursor(dx, dy)</code></a></li> 3791cb0ef41Sopenharmony_ci<li><a href="#rlrollback"><code>rl.rollback()</code></a></li> 3801cb0ef41Sopenharmony_ci</ul> 3811cb0ef41Sopenharmony_ci</li> 3821cb0ef41Sopenharmony_ci<li><a href="#readlinepromisescreateinterfaceoptions"><code>readlinePromises.createInterface(options)</code></a> 3831cb0ef41Sopenharmony_ci<ul> 3841cb0ef41Sopenharmony_ci<li><a href="#use-of-the-completer-function">Use of the <code>completer</code> function</a></li> 3851cb0ef41Sopenharmony_ci</ul> 3861cb0ef41Sopenharmony_ci</li> 3871cb0ef41Sopenharmony_ci</ul> 3881cb0ef41Sopenharmony_ci</li> 3891cb0ef41Sopenharmony_ci<li><a href="#callback-api">Callback API</a> 3901cb0ef41Sopenharmony_ci<ul> 3911cb0ef41Sopenharmony_ci<li><a href="#class-readlineinterface">Class: <code>readline.Interface</code></a> 3921cb0ef41Sopenharmony_ci<ul> 3931cb0ef41Sopenharmony_ci<li><a href="#rlquestionquery-options-callback_1"><code>rl.question(query[, options], callback)</code></a></li> 3941cb0ef41Sopenharmony_ci</ul> 3951cb0ef41Sopenharmony_ci</li> 3961cb0ef41Sopenharmony_ci<li><a href="#readlineclearlinestream-dir-callback"><code>readline.clearLine(stream, dir[, callback])</code></a></li> 3971cb0ef41Sopenharmony_ci<li><a href="#readlineclearscreendownstream-callback"><code>readline.clearScreenDown(stream[, callback])</code></a></li> 3981cb0ef41Sopenharmony_ci<li><a href="#readlinecreateinterfaceoptions"><code>readline.createInterface(options)</code></a> 3991cb0ef41Sopenharmony_ci<ul> 4001cb0ef41Sopenharmony_ci<li><a href="#use-of-the-completer-function_1">Use of the <code>completer</code> function</a></li> 4011cb0ef41Sopenharmony_ci</ul> 4021cb0ef41Sopenharmony_ci</li> 4031cb0ef41Sopenharmony_ci<li><a href="#readlinecursortostream-x-y-callback"><code>readline.cursorTo(stream, x[, y][, callback])</code></a></li> 4041cb0ef41Sopenharmony_ci<li><a href="#readlinemovecursorstream-dx-dy-callback"><code>readline.moveCursor(stream, dx, dy[, callback])</code></a></li> 4051cb0ef41Sopenharmony_ci</ul> 4061cb0ef41Sopenharmony_ci</li> 4071cb0ef41Sopenharmony_ci<li><a href="#readlineemitkeypresseventsstream-interface"><code>readline.emitKeypressEvents(stream[, interface])</code></a></li> 4081cb0ef41Sopenharmony_ci<li><a href="#example-tiny-cli">Example: Tiny CLI</a></li> 4091cb0ef41Sopenharmony_ci<li><a href="#example-read-file-stream-line-by-line">Example: Read file stream line-by-Line</a></li> 4101cb0ef41Sopenharmony_ci<li><a href="#tty-keybindings">TTY keybindings</a></li> 4111cb0ef41Sopenharmony_ci</ul> 4121cb0ef41Sopenharmony_ci</li> 4131cb0ef41Sopenharmony_ci</ul></details> 4141cb0ef41Sopenharmony_ci 4151cb0ef41Sopenharmony_ci <div id="apicontent"> 4161cb0ef41Sopenharmony_ci <h2>Readline<span><a class="mark" href="#readline" id="readline">#</a></span><a aria-hidden="true" class="legacy" id="readline_readline"></a></h2> 4171cb0ef41Sopenharmony_ci 4181cb0ef41Sopenharmony_ci<p></p><div class="api_stability api_stability_2"><a href="documentation.html#stability-index">Stability: 2</a> - Stable</div><p></p> 4191cb0ef41Sopenharmony_ci<p><strong>Source Code:</strong> <a href="https://github.com/nodejs/node/blob/v18.20.1/lib/readline.js">lib/readline.js</a></p> 4201cb0ef41Sopenharmony_ci<p>The <code>node:readline</code> module provides an interface for reading data from a 4211cb0ef41Sopenharmony_ci<a href="stream.html#readable-streams">Readable</a> stream (such as <a href="process.html#processstdin"><code>process.stdin</code></a>) one line at a time.</p> 4221cb0ef41Sopenharmony_ci<p>To use the promise-based APIs:</p> 4231cb0ef41Sopenharmony_ci 4241cb0ef41Sopenharmony_ci<pre class="with-51-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> * <span class="hljs-keyword">as</span> readline <span class="hljs-keyword">from</span> <span class="hljs-string">'node:readline/promises'</span>;</code><code class="language-js cjs"><span class="hljs-keyword">const</span> readline = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:readline/promises'</span>);</code><button class="copy-button">copy</button></pre> 4251cb0ef41Sopenharmony_ci<p>To use the callback and sync APIs:</p> 4261cb0ef41Sopenharmony_ci 4271cb0ef41Sopenharmony_ci<pre class="with-42-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> * <span class="hljs-keyword">as</span> readline <span class="hljs-keyword">from</span> <span class="hljs-string">'node:readline'</span>;</code><code class="language-js cjs"><span class="hljs-keyword">const</span> readline = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:readline'</span>);</code><button class="copy-button">copy</button></pre> 4281cb0ef41Sopenharmony_ci<p>The following simple example illustrates the basic use of the <code>node:readline</code> 4291cb0ef41Sopenharmony_cimodule.</p> 4301cb0ef41Sopenharmony_ci 4311cb0ef41Sopenharmony_ci<pre class="with-65-chars"><input class="js-flavor-selector" type="checkbox" checked aria-label="Show modern ES modules syntax"><code class="language-js mjs"><span class="hljs-keyword">import</span> * <span class="hljs-keyword">as</span> readline <span class="hljs-keyword">from</span> <span class="hljs-string">'node:readline/promises'</span>; 4321cb0ef41Sopenharmony_ci<span class="hljs-keyword">import</span> { stdin <span class="hljs-keyword">as</span> input, stdout <span class="hljs-keyword">as</span> output } <span class="hljs-keyword">from</span> <span class="hljs-string">'node:process'</span>; 4331cb0ef41Sopenharmony_ci 4341cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> rl = readline.<span class="hljs-title function_">createInterface</span>({ input, output }); 4351cb0ef41Sopenharmony_ci 4361cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> answer = <span class="hljs-keyword">await</span> rl.<span class="hljs-title function_">question</span>(<span class="hljs-string">'What do you think of Node.js? '</span>); 4371cb0ef41Sopenharmony_ci 4381cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`Thank you for your valuable feedback: <span class="hljs-subst">${answer}</span>`</span>); 4391cb0ef41Sopenharmony_ci 4401cb0ef41Sopenharmony_cirl.<span class="hljs-title function_">close</span>();</code><code class="language-js cjs"><span class="hljs-keyword">const</span> readline = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:readline'</span>); 4411cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> { <span class="hljs-attr">stdin</span>: input, <span class="hljs-attr">stdout</span>: output } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:process'</span>); 4421cb0ef41Sopenharmony_ci 4431cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> rl = readline.<span class="hljs-title function_">createInterface</span>({ input, output }); 4441cb0ef41Sopenharmony_ci 4451cb0ef41Sopenharmony_cirl.<span class="hljs-title function_">question</span>(<span class="hljs-string">'What do you think of Node.js? '</span>, <span class="hljs-function">(<span class="hljs-params">answer</span>) =></span> { 4461cb0ef41Sopenharmony_ci <span class="hljs-comment">// <span class="hljs-doctag">TODO:</span> Log the answer in a database</span> 4471cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`Thank you for your valuable feedback: <span class="hljs-subst">${answer}</span>`</span>); 4481cb0ef41Sopenharmony_ci 4491cb0ef41Sopenharmony_ci rl.<span class="hljs-title function_">close</span>(); 4501cb0ef41Sopenharmony_ci});</code><button class="copy-button">copy</button></pre> 4511cb0ef41Sopenharmony_ci<p>Once this code is invoked, the Node.js application will not terminate until the 4521cb0ef41Sopenharmony_ci<code>readline.Interface</code> is closed because the interface waits for data to be 4531cb0ef41Sopenharmony_cireceived on the <code>input</code> stream.</p> 4541cb0ef41Sopenharmony_ci<p><a id="readline_class_interface"></a></p> 4551cb0ef41Sopenharmony_ci<section><h3>Class: <code>InterfaceConstructor</code><span><a class="mark" href="#class-interfaceconstructor" id="class-interfaceconstructor">#</a></span><a aria-hidden="true" class="legacy" id="readline_class_interfaceconstructor"></a></h3> 4561cb0ef41Sopenharmony_ci<div class="api_metadata"> 4571cb0ef41Sopenharmony_ci<span>Added in: v0.1.104</span> 4581cb0ef41Sopenharmony_ci</div> 4591cb0ef41Sopenharmony_ci<ul> 4601cb0ef41Sopenharmony_ci<li>Extends: <a href="events.html#class-eventemitter" class="type"><EventEmitter></a></li> 4611cb0ef41Sopenharmony_ci</ul> 4621cb0ef41Sopenharmony_ci<p>Instances of the <code>InterfaceConstructor</code> class are constructed using the 4631cb0ef41Sopenharmony_ci<code>readlinePromises.createInterface()</code> or <code>readline.createInterface()</code> method. 4641cb0ef41Sopenharmony_ciEvery instance is associated with a single <code>input</code> <a href="stream.html#readable-streams">Readable</a> stream and a 4651cb0ef41Sopenharmony_cisingle <code>output</code> <a href="stream.html#writable-streams">Writable</a> stream. 4661cb0ef41Sopenharmony_ciThe <code>output</code> stream is used to print prompts for user input that arrives on, 4671cb0ef41Sopenharmony_ciand is read from, the <code>input</code> stream.</p> 4681cb0ef41Sopenharmony_ci<h4>Event: <code>'close'</code><span><a class="mark" href="#event-close" id="event-close">#</a></span><a aria-hidden="true" class="legacy" id="readline_event_close"></a></h4> 4691cb0ef41Sopenharmony_ci<div class="api_metadata"> 4701cb0ef41Sopenharmony_ci<span>Added in: v0.1.98</span> 4711cb0ef41Sopenharmony_ci</div> 4721cb0ef41Sopenharmony_ci<p>The <code>'close'</code> event is emitted when one of the following occur:</p> 4731cb0ef41Sopenharmony_ci<ul> 4741cb0ef41Sopenharmony_ci<li>The <code>rl.close()</code> method is called and the <code>InterfaceConstructor</code> instance has 4751cb0ef41Sopenharmony_cirelinquished control over the <code>input</code> and <code>output</code> streams;</li> 4761cb0ef41Sopenharmony_ci<li>The <code>input</code> stream receives its <code>'end'</code> event;</li> 4771cb0ef41Sopenharmony_ci<li>The <code>input</code> stream receives <kbd>Ctrl</kbd>+<kbd>D</kbd> to signal 4781cb0ef41Sopenharmony_ciend-of-transmission (EOT);</li> 4791cb0ef41Sopenharmony_ci<li>The <code>input</code> stream receives <kbd>Ctrl</kbd>+<kbd>C</kbd> to signal <code>SIGINT</code> 4801cb0ef41Sopenharmony_ciand there is no <code>'SIGINT'</code> event listener registered on the 4811cb0ef41Sopenharmony_ci<code>InterfaceConstructor</code> instance.</li> 4821cb0ef41Sopenharmony_ci</ul> 4831cb0ef41Sopenharmony_ci<p>The listener function is called without passing any arguments.</p> 4841cb0ef41Sopenharmony_ci<p>The <code>InterfaceConstructor</code> instance is finished once the <code>'close'</code> event is 4851cb0ef41Sopenharmony_ciemitted.</p> 4861cb0ef41Sopenharmony_ci<h4>Event: <code>'line'</code><span><a class="mark" href="#event-line" id="event-line">#</a></span><a aria-hidden="true" class="legacy" id="readline_event_line"></a></h4> 4871cb0ef41Sopenharmony_ci<div class="api_metadata"> 4881cb0ef41Sopenharmony_ci<span>Added in: v0.1.98</span> 4891cb0ef41Sopenharmony_ci</div> 4901cb0ef41Sopenharmony_ci<p>The <code>'line'</code> event is emitted whenever the <code>input</code> stream receives an 4911cb0ef41Sopenharmony_ciend-of-line input (<code>\n</code>, <code>\r</code>, or <code>\r\n</code>). This usually occurs when the user 4921cb0ef41Sopenharmony_cipresses <kbd>Enter</kbd> or <kbd>Return</kbd>.</p> 4931cb0ef41Sopenharmony_ci<p>The <code>'line'</code> event is also emitted if new data has been read from a stream and 4941cb0ef41Sopenharmony_cithat stream ends without a final end-of-line marker.</p> 4951cb0ef41Sopenharmony_ci<p>The listener function is called with a string containing the single line of 4961cb0ef41Sopenharmony_cireceived input.</p> 4971cb0ef41Sopenharmony_ci<pre><code class="language-js">rl.<span class="hljs-title function_">on</span>(<span class="hljs-string">'line'</span>, <span class="hljs-function">(<span class="hljs-params">input</span>) =></span> { 4981cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`Received: <span class="hljs-subst">${input}</span>`</span>); 4991cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre> 5001cb0ef41Sopenharmony_ci<h4>Event: <code>'history'</code><span><a class="mark" href="#event-history" id="event-history">#</a></span><a aria-hidden="true" class="legacy" id="readline_event_history"></a></h4> 5011cb0ef41Sopenharmony_ci<div class="api_metadata"> 5021cb0ef41Sopenharmony_ci<span>Added in: v15.8.0, v14.18.0</span> 5031cb0ef41Sopenharmony_ci</div> 5041cb0ef41Sopenharmony_ci<p>The <code>'history'</code> event is emitted whenever the history array has changed.</p> 5051cb0ef41Sopenharmony_ci<p>The listener function is called with an array containing the history array. 5061cb0ef41Sopenharmony_ciIt will reflect all changes, added lines and removed lines due to 5071cb0ef41Sopenharmony_ci<code>historySize</code> and <code>removeHistoryDuplicates</code>.</p> 5081cb0ef41Sopenharmony_ci<p>The primary purpose is to allow a listener to persist the history. 5091cb0ef41Sopenharmony_ciIt is also possible for the listener to change the history object. This 5101cb0ef41Sopenharmony_cicould be useful to prevent certain lines to be added to the history, like 5111cb0ef41Sopenharmony_cia password.</p> 5121cb0ef41Sopenharmony_ci<pre><code class="language-js">rl.<span class="hljs-title function_">on</span>(<span class="hljs-string">'history'</span>, <span class="hljs-function">(<span class="hljs-params">history</span>) =></span> { 5131cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`Received: <span class="hljs-subst">${history}</span>`</span>); 5141cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre> 5151cb0ef41Sopenharmony_ci<h4>Event: <code>'pause'</code><span><a class="mark" href="#event-pause" id="event-pause">#</a></span><a aria-hidden="true" class="legacy" id="readline_event_pause"></a></h4> 5161cb0ef41Sopenharmony_ci<div class="api_metadata"> 5171cb0ef41Sopenharmony_ci<span>Added in: v0.7.5</span> 5181cb0ef41Sopenharmony_ci</div> 5191cb0ef41Sopenharmony_ci<p>The <code>'pause'</code> event is emitted when one of the following occur:</p> 5201cb0ef41Sopenharmony_ci<ul> 5211cb0ef41Sopenharmony_ci<li>The <code>input</code> stream is paused.</li> 5221cb0ef41Sopenharmony_ci<li>The <code>input</code> stream is not paused and receives the <code>'SIGCONT'</code> event. (See 5231cb0ef41Sopenharmony_cievents <a href="#event-sigtstp"><code>'SIGTSTP'</code></a> and <a href="#event-sigcont"><code>'SIGCONT'</code></a>.)</li> 5241cb0ef41Sopenharmony_ci</ul> 5251cb0ef41Sopenharmony_ci<p>The listener function is called without passing any arguments.</p> 5261cb0ef41Sopenharmony_ci<pre><code class="language-js">rl.<span class="hljs-title function_">on</span>(<span class="hljs-string">'pause'</span>, <span class="hljs-function">() =></span> { 5271cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'Readline paused.'</span>); 5281cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre> 5291cb0ef41Sopenharmony_ci<h4>Event: <code>'resume'</code><span><a class="mark" href="#event-resume" id="event-resume">#</a></span><a aria-hidden="true" class="legacy" id="readline_event_resume"></a></h4> 5301cb0ef41Sopenharmony_ci<div class="api_metadata"> 5311cb0ef41Sopenharmony_ci<span>Added in: v0.7.5</span> 5321cb0ef41Sopenharmony_ci</div> 5331cb0ef41Sopenharmony_ci<p>The <code>'resume'</code> event is emitted whenever the <code>input</code> stream is resumed.</p> 5341cb0ef41Sopenharmony_ci<p>The listener function is called without passing any arguments.</p> 5351cb0ef41Sopenharmony_ci<pre><code class="language-js">rl.<span class="hljs-title function_">on</span>(<span class="hljs-string">'resume'</span>, <span class="hljs-function">() =></span> { 5361cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'Readline resumed.'</span>); 5371cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre> 5381cb0ef41Sopenharmony_ci<h4>Event: <code>'SIGCONT'</code><span><a class="mark" href="#event-sigcont" id="event-sigcont">#</a></span><a aria-hidden="true" class="legacy" id="readline_event_sigcont"></a></h4> 5391cb0ef41Sopenharmony_ci<div class="api_metadata"> 5401cb0ef41Sopenharmony_ci<span>Added in: v0.7.5</span> 5411cb0ef41Sopenharmony_ci</div> 5421cb0ef41Sopenharmony_ci<p>The <code>'SIGCONT'</code> event is emitted when a Node.js process previously moved into 5431cb0ef41Sopenharmony_cithe background using <kbd>Ctrl</kbd>+<kbd>Z</kbd> (i.e. <code>SIGTSTP</code>) is then 5441cb0ef41Sopenharmony_cibrought back to the foreground using <a href="http://man7.org/linux/man-pages/man1/fg.1p.html"><code>fg(1p)</code></a>.</p> 5451cb0ef41Sopenharmony_ci<p>If the <code>input</code> stream was paused <em>before</em> the <code>SIGTSTP</code> request, this event will 5461cb0ef41Sopenharmony_cinot be emitted.</p> 5471cb0ef41Sopenharmony_ci<p>The listener function is invoked without passing any arguments.</p> 5481cb0ef41Sopenharmony_ci<pre><code class="language-js">rl.<span class="hljs-title function_">on</span>(<span class="hljs-string">'SIGCONT'</span>, <span class="hljs-function">() =></span> { 5491cb0ef41Sopenharmony_ci <span class="hljs-comment">// `prompt` will automatically resume the stream</span> 5501cb0ef41Sopenharmony_ci rl.<span class="hljs-title function_">prompt</span>(); 5511cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre> 5521cb0ef41Sopenharmony_ci<p>The <code>'SIGCONT'</code> event is <em>not</em> supported on Windows.</p> 5531cb0ef41Sopenharmony_ci<h4>Event: <code>'SIGINT'</code><span><a class="mark" href="#event-sigint" id="event-sigint">#</a></span><a aria-hidden="true" class="legacy" id="readline_event_sigint"></a></h4> 5541cb0ef41Sopenharmony_ci<div class="api_metadata"> 5551cb0ef41Sopenharmony_ci<span>Added in: v0.3.0</span> 5561cb0ef41Sopenharmony_ci</div> 5571cb0ef41Sopenharmony_ci<p>The <code>'SIGINT'</code> event is emitted whenever the <code>input</code> stream receives 5581cb0ef41Sopenharmony_cia <kbd>Ctrl+C</kbd> input, known typically as <code>SIGINT</code>. If there are no 5591cb0ef41Sopenharmony_ci<code>'SIGINT'</code> event listeners registered when the <code>input</code> stream receives a 5601cb0ef41Sopenharmony_ci<code>SIGINT</code>, the <code>'pause'</code> event will be emitted.</p> 5611cb0ef41Sopenharmony_ci<p>The listener function is invoked without passing any arguments.</p> 5621cb0ef41Sopenharmony_ci<pre><code class="language-js">rl.<span class="hljs-title function_">on</span>(<span class="hljs-string">'SIGINT'</span>, <span class="hljs-function">() =></span> { 5631cb0ef41Sopenharmony_ci rl.<span class="hljs-title function_">question</span>(<span class="hljs-string">'Are you sure you want to exit? '</span>, <span class="hljs-function">(<span class="hljs-params">answer</span>) =></span> { 5641cb0ef41Sopenharmony_ci <span class="hljs-keyword">if</span> (answer.<span class="hljs-title function_">match</span>(<span class="hljs-regexp">/^y(es)?$/i</span>)) rl.<span class="hljs-title function_">pause</span>(); 5651cb0ef41Sopenharmony_ci }); 5661cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre> 5671cb0ef41Sopenharmony_ci<h4>Event: <code>'SIGTSTP'</code><span><a class="mark" href="#event-sigtstp" id="event-sigtstp">#</a></span><a aria-hidden="true" class="legacy" id="readline_event_sigtstp"></a></h4> 5681cb0ef41Sopenharmony_ci<div class="api_metadata"> 5691cb0ef41Sopenharmony_ci<span>Added in: v0.7.5</span> 5701cb0ef41Sopenharmony_ci</div> 5711cb0ef41Sopenharmony_ci<p>The <code>'SIGTSTP'</code> event is emitted when the <code>input</code> stream receives 5721cb0ef41Sopenharmony_cia <kbd>Ctrl</kbd>+<kbd>Z</kbd> input, typically known as <code>SIGTSTP</code>. If there are 5731cb0ef41Sopenharmony_cino <code>'SIGTSTP'</code> event listeners registered when the <code>input</code> stream receives a 5741cb0ef41Sopenharmony_ci<code>SIGTSTP</code>, the Node.js process will be sent to the background.</p> 5751cb0ef41Sopenharmony_ci<p>When the program is resumed using <a href="http://man7.org/linux/man-pages/man1/fg.1p.html"><code>fg(1p)</code></a>, the <code>'pause'</code> and <code>'SIGCONT'</code> events 5761cb0ef41Sopenharmony_ciwill be emitted. These can be used to resume the <code>input</code> stream.</p> 5771cb0ef41Sopenharmony_ci<p>The <code>'pause'</code> and <code>'SIGCONT'</code> events will not be emitted if the <code>input</code> was 5781cb0ef41Sopenharmony_cipaused before the process was sent to the background.</p> 5791cb0ef41Sopenharmony_ci<p>The listener function is invoked without passing any arguments.</p> 5801cb0ef41Sopenharmony_ci<pre><code class="language-js">rl.<span class="hljs-title function_">on</span>(<span class="hljs-string">'SIGTSTP'</span>, <span class="hljs-function">() =></span> { 5811cb0ef41Sopenharmony_ci <span class="hljs-comment">// This will override SIGTSTP and prevent the program from going to the</span> 5821cb0ef41Sopenharmony_ci <span class="hljs-comment">// background.</span> 5831cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'Caught SIGTSTP.'</span>); 5841cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre> 5851cb0ef41Sopenharmony_ci<p>The <code>'SIGTSTP'</code> event is <em>not</em> supported on Windows.</p> 5861cb0ef41Sopenharmony_ci<h4><code>rl.close()</code><span><a class="mark" href="#rlclose" id="rlclose">#</a></span><a aria-hidden="true" class="legacy" id="readline_rl_close"></a></h4> 5871cb0ef41Sopenharmony_ci<div class="api_metadata"> 5881cb0ef41Sopenharmony_ci<span>Added in: v0.1.98</span> 5891cb0ef41Sopenharmony_ci</div> 5901cb0ef41Sopenharmony_ci<p>The <code>rl.close()</code> method closes the <code>InterfaceConstructor</code> instance and 5911cb0ef41Sopenharmony_cirelinquishes control over the <code>input</code> and <code>output</code> streams. When called, 5921cb0ef41Sopenharmony_cithe <code>'close'</code> event will be emitted.</p> 5931cb0ef41Sopenharmony_ci<p>Calling <code>rl.close()</code> does not immediately stop other events (including <code>'line'</code>) 5941cb0ef41Sopenharmony_cifrom being emitted by the <code>InterfaceConstructor</code> instance.</p> 5951cb0ef41Sopenharmony_ci<h4><code>rl.pause()</code><span><a class="mark" href="#rlpause" id="rlpause">#</a></span><a aria-hidden="true" class="legacy" id="readline_rl_pause"></a></h4> 5961cb0ef41Sopenharmony_ci<div class="api_metadata"> 5971cb0ef41Sopenharmony_ci<span>Added in: v0.3.4</span> 5981cb0ef41Sopenharmony_ci</div> 5991cb0ef41Sopenharmony_ci<p>The <code>rl.pause()</code> method pauses the <code>input</code> stream, allowing it to be resumed 6001cb0ef41Sopenharmony_cilater if necessary.</p> 6011cb0ef41Sopenharmony_ci<p>Calling <code>rl.pause()</code> does not immediately pause other events (including 6021cb0ef41Sopenharmony_ci<code>'line'</code>) from being emitted by the <code>InterfaceConstructor</code> instance.</p> 6031cb0ef41Sopenharmony_ci<h4><code>rl.prompt([preserveCursor])</code><span><a class="mark" href="#rlpromptpreservecursor" id="rlpromptpreservecursor">#</a></span><a aria-hidden="true" class="legacy" id="readline_rl_prompt_preservecursor"></a></h4> 6041cb0ef41Sopenharmony_ci<div class="api_metadata"> 6051cb0ef41Sopenharmony_ci<span>Added in: v0.1.98</span> 6061cb0ef41Sopenharmony_ci</div> 6071cb0ef41Sopenharmony_ci<ul> 6081cb0ef41Sopenharmony_ci<li><code>preserveCursor</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> If <code>true</code>, prevents the cursor placement from 6091cb0ef41Sopenharmony_cibeing reset to <code>0</code>.</li> 6101cb0ef41Sopenharmony_ci</ul> 6111cb0ef41Sopenharmony_ci<p>The <code>rl.prompt()</code> method writes the <code>InterfaceConstructor</code> instances configured 6121cb0ef41Sopenharmony_ci<code>prompt</code> to a new line in <code>output</code> in order to provide a user with a new 6131cb0ef41Sopenharmony_cilocation at which to provide input.</p> 6141cb0ef41Sopenharmony_ci<p>When called, <code>rl.prompt()</code> will resume the <code>input</code> stream if it has been 6151cb0ef41Sopenharmony_cipaused.</p> 6161cb0ef41Sopenharmony_ci<p>If the <code>InterfaceConstructor</code> was created with <code>output</code> set to <code>null</code> or 6171cb0ef41Sopenharmony_ci<code>undefined</code> the prompt is not written.</p> 6181cb0ef41Sopenharmony_ci<h4><code>rl.question(query[, options], callback)</code><span><a class="mark" href="#rlquestionquery-options-callback" id="rlquestionquery-options-callback">#</a></span><a aria-hidden="true" class="legacy" id="readline_rl_question_query_options_callback"></a></h4> 6191cb0ef41Sopenharmony_ci<div class="api_metadata"> 6201cb0ef41Sopenharmony_ci<span>Added in: v0.3.3</span> 6211cb0ef41Sopenharmony_ci</div> 6221cb0ef41Sopenharmony_ci<ul> 6231cb0ef41Sopenharmony_ci<li><code>query</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> A statement or query to write to <code>output</code>, prepended to the 6241cb0ef41Sopenharmony_ciprompt.</li> 6251cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 6261cb0ef41Sopenharmony_ci<ul> 6271cb0ef41Sopenharmony_ci<li><code>signal</code> <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a> Optionally allows the <code>question()</code> to be canceled 6281cb0ef41Sopenharmony_ciusing an <code>AbortController</code>.</li> 6291cb0ef41Sopenharmony_ci</ul> 6301cb0ef41Sopenharmony_ci</li> 6311cb0ef41Sopenharmony_ci<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> A callback function that is invoked with the user's 6321cb0ef41Sopenharmony_ciinput in response to the <code>query</code>.</li> 6331cb0ef41Sopenharmony_ci</ul> 6341cb0ef41Sopenharmony_ci<p>The <code>rl.question()</code> method displays the <code>query</code> by writing it to the <code>output</code>, 6351cb0ef41Sopenharmony_ciwaits for user input to be provided on <code>input</code>, then invokes the <code>callback</code> 6361cb0ef41Sopenharmony_cifunction passing the provided input as the first argument.</p> 6371cb0ef41Sopenharmony_ci<p>When called, <code>rl.question()</code> will resume the <code>input</code> stream if it has been 6381cb0ef41Sopenharmony_cipaused.</p> 6391cb0ef41Sopenharmony_ci<p>If the <code>InterfaceConstructor</code> was created with <code>output</code> set to <code>null</code> or 6401cb0ef41Sopenharmony_ci<code>undefined</code> the <code>query</code> is not written.</p> 6411cb0ef41Sopenharmony_ci<p>The <code>callback</code> function passed to <code>rl.question()</code> does not follow the typical 6421cb0ef41Sopenharmony_cipattern of accepting an <code>Error</code> object or <code>null</code> as the first argument. 6431cb0ef41Sopenharmony_ciThe <code>callback</code> is called with the provided answer as the only argument.</p> 6441cb0ef41Sopenharmony_ci<p>An error will be thrown if calling <code>rl.question()</code> after <code>rl.close()</code>.</p> 6451cb0ef41Sopenharmony_ci<p>Example usage:</p> 6461cb0ef41Sopenharmony_ci<pre><code class="language-js">rl.<span class="hljs-title function_">question</span>(<span class="hljs-string">'What is your favorite food? '</span>, <span class="hljs-function">(<span class="hljs-params">answer</span>) =></span> { 6471cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`Oh, so your favorite food is <span class="hljs-subst">${answer}</span>`</span>); 6481cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre> 6491cb0ef41Sopenharmony_ci<p>Using an <code>AbortController</code> to cancel a question.</p> 6501cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">const</span> ac = <span class="hljs-keyword">new</span> <span class="hljs-title class_">AbortController</span>(); 6511cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> signal = ac.<span class="hljs-property">signal</span>; 6521cb0ef41Sopenharmony_ci 6531cb0ef41Sopenharmony_cirl.<span class="hljs-title function_">question</span>(<span class="hljs-string">'What is your favorite food? '</span>, { signal }, <span class="hljs-function">(<span class="hljs-params">answer</span>) =></span> { 6541cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`Oh, so your favorite food is <span class="hljs-subst">${answer}</span>`</span>); 6551cb0ef41Sopenharmony_ci}); 6561cb0ef41Sopenharmony_ci 6571cb0ef41Sopenharmony_cisignal.<span class="hljs-title function_">addEventListener</span>(<span class="hljs-string">'abort'</span>, <span class="hljs-function">() =></span> { 6581cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'The food question timed out'</span>); 6591cb0ef41Sopenharmony_ci}, { <span class="hljs-attr">once</span>: <span class="hljs-literal">true</span> }); 6601cb0ef41Sopenharmony_ci 6611cb0ef41Sopenharmony_ci<span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> ac.<span class="hljs-title function_">abort</span>(), <span class="hljs-number">10000</span>);</code> <button class="copy-button">copy</button></pre> 6621cb0ef41Sopenharmony_ci<h4><code>rl.resume()</code><span><a class="mark" href="#rlresume" id="rlresume">#</a></span><a aria-hidden="true" class="legacy" id="readline_rl_resume"></a></h4> 6631cb0ef41Sopenharmony_ci<div class="api_metadata"> 6641cb0ef41Sopenharmony_ci<span>Added in: v0.3.4</span> 6651cb0ef41Sopenharmony_ci</div> 6661cb0ef41Sopenharmony_ci<p>The <code>rl.resume()</code> method resumes the <code>input</code> stream if it has been paused.</p> 6671cb0ef41Sopenharmony_ci<h4><code>rl.setPrompt(prompt)</code><span><a class="mark" href="#rlsetpromptprompt" id="rlsetpromptprompt">#</a></span><a aria-hidden="true" class="legacy" id="readline_rl_setprompt_prompt"></a></h4> 6681cb0ef41Sopenharmony_ci<div class="api_metadata"> 6691cb0ef41Sopenharmony_ci<span>Added in: v0.1.98</span> 6701cb0ef41Sopenharmony_ci</div> 6711cb0ef41Sopenharmony_ci<ul> 6721cb0ef41Sopenharmony_ci<li><code>prompt</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 6731cb0ef41Sopenharmony_ci</ul> 6741cb0ef41Sopenharmony_ci<p>The <code>rl.setPrompt()</code> method sets the prompt that will be written to <code>output</code> 6751cb0ef41Sopenharmony_ciwhenever <code>rl.prompt()</code> is called.</p> 6761cb0ef41Sopenharmony_ci<h4><code>rl.getPrompt()</code><span><a class="mark" href="#rlgetprompt" id="rlgetprompt">#</a></span><a aria-hidden="true" class="legacy" id="readline_rl_getprompt"></a></h4> 6771cb0ef41Sopenharmony_ci<div class="api_metadata"> 6781cb0ef41Sopenharmony_ci<span>Added in: v15.3.0, v14.17.0</span> 6791cb0ef41Sopenharmony_ci</div> 6801cb0ef41Sopenharmony_ci<ul> 6811cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> the current prompt string</li> 6821cb0ef41Sopenharmony_ci</ul> 6831cb0ef41Sopenharmony_ci<p>The <code>rl.getPrompt()</code> method returns the current prompt used by <code>rl.prompt()</code>.</p> 6841cb0ef41Sopenharmony_ci<h4><code>rl.write(data[, key])</code><span><a class="mark" href="#rlwritedata-key" id="rlwritedata-key">#</a></span><a aria-hidden="true" class="legacy" id="readline_rl_write_data_key"></a></h4> 6851cb0ef41Sopenharmony_ci<div class="api_metadata"> 6861cb0ef41Sopenharmony_ci<span>Added in: v0.1.98</span> 6871cb0ef41Sopenharmony_ci</div> 6881cb0ef41Sopenharmony_ci<ul> 6891cb0ef41Sopenharmony_ci<li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 6901cb0ef41Sopenharmony_ci<li><code>key</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 6911cb0ef41Sopenharmony_ci<ul> 6921cb0ef41Sopenharmony_ci<li><code>ctrl</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <code>true</code> to indicate the <kbd>Ctrl</kbd> key.</li> 6931cb0ef41Sopenharmony_ci<li><code>meta</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <code>true</code> to indicate the <kbd>Meta</kbd> key.</li> 6941cb0ef41Sopenharmony_ci<li><code>shift</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <code>true</code> to indicate the <kbd>Shift</kbd> key.</li> 6951cb0ef41Sopenharmony_ci<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The name of the a key.</li> 6961cb0ef41Sopenharmony_ci</ul> 6971cb0ef41Sopenharmony_ci</li> 6981cb0ef41Sopenharmony_ci</ul> 6991cb0ef41Sopenharmony_ci<p>The <code>rl.write()</code> method will write either <code>data</code> or a key sequence identified 7001cb0ef41Sopenharmony_ciby <code>key</code> to the <code>output</code>. The <code>key</code> argument is supported only if <code>output</code> is 7011cb0ef41Sopenharmony_cia <a href="tty.html">TTY</a> text terminal. See <a href="#tty-keybindings">TTY keybindings</a> for a list of key 7021cb0ef41Sopenharmony_cicombinations.</p> 7031cb0ef41Sopenharmony_ci<p>If <code>key</code> is specified, <code>data</code> is ignored.</p> 7041cb0ef41Sopenharmony_ci<p>When called, <code>rl.write()</code> will resume the <code>input</code> stream if it has been 7051cb0ef41Sopenharmony_cipaused.</p> 7061cb0ef41Sopenharmony_ci<p>If the <code>InterfaceConstructor</code> was created with <code>output</code> set to <code>null</code> or 7071cb0ef41Sopenharmony_ci<code>undefined</code> the <code>data</code> and <code>key</code> are not written.</p> 7081cb0ef41Sopenharmony_ci<pre><code class="language-js">rl.<span class="hljs-title function_">write</span>(<span class="hljs-string">'Delete this!'</span>); 7091cb0ef41Sopenharmony_ci<span class="hljs-comment">// Simulate Ctrl+U to delete the line written previously</span> 7101cb0ef41Sopenharmony_cirl.<span class="hljs-title function_">write</span>(<span class="hljs-literal">null</span>, { <span class="hljs-attr">ctrl</span>: <span class="hljs-literal">true</span>, <span class="hljs-attr">name</span>: <span class="hljs-string">'u'</span> });</code> <button class="copy-button">copy</button></pre> 7111cb0ef41Sopenharmony_ci<p>The <code>rl.write()</code> method will write the data to the <code>readline</code> <code>Interface</code>'s 7121cb0ef41Sopenharmony_ci<code>input</code> <em>as if it were provided by the user</em>.</p> 7131cb0ef41Sopenharmony_ci<h4><code>rl[Symbol.asyncIterator]()</code><span><a class="mark" href="#rlsymbolasynciterator" id="rlsymbolasynciterator">#</a></span><a aria-hidden="true" class="legacy" id="readline_rl_symbol_asynciterator"></a></h4> 7141cb0ef41Sopenharmony_ci<div class="api_metadata"> 7151cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 7161cb0ef41Sopenharmony_ci<table> 7171cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 7181cb0ef41Sopenharmony_ci<tr><td>v11.14.0, v10.17.0</td> 7191cb0ef41Sopenharmony_ci<td><p>Symbol.asyncIterator support is no longer experimental.</p></td></tr> 7201cb0ef41Sopenharmony_ci<tr><td>v11.4.0, v10.16.0</td> 7211cb0ef41Sopenharmony_ci<td><p><span>Added in: v11.4.0, v10.16.0</span></p></td></tr> 7221cb0ef41Sopenharmony_ci</tbody></table> 7231cb0ef41Sopenharmony_ci</details> 7241cb0ef41Sopenharmony_ci</div> 7251cb0ef41Sopenharmony_ci<ul> 7261cb0ef41Sopenharmony_ci<li>Returns: <a href="https://tc39.github.io/ecma262/#sec-asynciterator-interface" class="type"><AsyncIterator></a></li> 7271cb0ef41Sopenharmony_ci</ul> 7281cb0ef41Sopenharmony_ci<p>Create an <code>AsyncIterator</code> object that iterates through each line in the input 7291cb0ef41Sopenharmony_cistream as a string. This method allows asynchronous iteration of 7301cb0ef41Sopenharmony_ci<code>InterfaceConstructor</code> objects through <code>for await...of</code> loops.</p> 7311cb0ef41Sopenharmony_ci<p>Errors in the input stream are not forwarded.</p> 7321cb0ef41Sopenharmony_ci<p>If the loop is terminated with <code>break</code>, <code>throw</code>, or <code>return</code>, 7331cb0ef41Sopenharmony_ci<a href="#rlclose"><code>rl.close()</code></a> will be called. In other words, iterating over a 7341cb0ef41Sopenharmony_ci<code>InterfaceConstructor</code> will always consume the input stream fully.</p> 7351cb0ef41Sopenharmony_ci<p>Performance is not on par with the traditional <code>'line'</code> event API. Use <code>'line'</code> 7361cb0ef41Sopenharmony_ciinstead for performance-sensitive applications.</p> 7371cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-title function_">processLineByLine</span>(<span class="hljs-params"></span>) { 7381cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> rl = readline.<span class="hljs-title function_">createInterface</span>({ 7391cb0ef41Sopenharmony_ci <span class="hljs-comment">// ...</span> 7401cb0ef41Sopenharmony_ci }); 7411cb0ef41Sopenharmony_ci 7421cb0ef41Sopenharmony_ci <span class="hljs-keyword">for</span> <span class="hljs-keyword">await</span> (<span class="hljs-keyword">const</span> line <span class="hljs-keyword">of</span> rl) { 7431cb0ef41Sopenharmony_ci <span class="hljs-comment">// Each line in the readline input will be successively available here as</span> 7441cb0ef41Sopenharmony_ci <span class="hljs-comment">// `line`.</span> 7451cb0ef41Sopenharmony_ci } 7461cb0ef41Sopenharmony_ci}</code> <button class="copy-button">copy</button></pre> 7471cb0ef41Sopenharmony_ci<p><code>readline.createInterface()</code> will start to consume the input stream once 7481cb0ef41Sopenharmony_ciinvoked. Having asynchronous operations between interface creation and 7491cb0ef41Sopenharmony_ciasynchronous iteration may result in missed lines.</p> 7501cb0ef41Sopenharmony_ci<h4><code>rl.line</code><span><a class="mark" href="#rlline" id="rlline">#</a></span><a aria-hidden="true" class="legacy" id="readline_rl_line"></a></h4> 7511cb0ef41Sopenharmony_ci<div class="api_metadata"> 7521cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 7531cb0ef41Sopenharmony_ci<table> 7541cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 7551cb0ef41Sopenharmony_ci<tr><td>v15.8.0, v14.18.0</td> 7561cb0ef41Sopenharmony_ci<td><p>Value will always be a string, never undefined.</p></td></tr> 7571cb0ef41Sopenharmony_ci<tr><td>v0.1.98</td> 7581cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.1.98</span></p></td></tr> 7591cb0ef41Sopenharmony_ci</tbody></table> 7601cb0ef41Sopenharmony_ci</details> 7611cb0ef41Sopenharmony_ci</div> 7621cb0ef41Sopenharmony_ci<ul> 7631cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 7641cb0ef41Sopenharmony_ci</ul> 7651cb0ef41Sopenharmony_ci<p>The current input data being processed by node.</p> 7661cb0ef41Sopenharmony_ci<p>This can be used when collecting input from a TTY stream to retrieve the 7671cb0ef41Sopenharmony_cicurrent value that has been processed thus far, prior to the <code>line</code> event 7681cb0ef41Sopenharmony_cibeing emitted. Once the <code>line</code> event has been emitted, this property will 7691cb0ef41Sopenharmony_cibe an empty string.</p> 7701cb0ef41Sopenharmony_ci<p>Be aware that modifying the value during the instance runtime may have 7711cb0ef41Sopenharmony_ciunintended consequences if <code>rl.cursor</code> is not also controlled.</p> 7721cb0ef41Sopenharmony_ci<p><strong>If not using a TTY stream for input, use the <a href="#event-line"><code>'line'</code></a> event.</strong></p> 7731cb0ef41Sopenharmony_ci<p>One possible use case would be as follows:</p> 7741cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">const</span> values = [<span class="hljs-string">'lorem ipsum'</span>, <span class="hljs-string">'dolor sit amet'</span>]; 7751cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> rl = readline.<span class="hljs-title function_">createInterface</span>(process.<span class="hljs-property">stdin</span>); 7761cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> showResults = <span class="hljs-title function_">debounce</span>(<span class="hljs-function">() =></span> { 7771cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>( 7781cb0ef41Sopenharmony_ci <span class="hljs-string">'\n'</span>, 7791cb0ef41Sopenharmony_ci values.<span class="hljs-title function_">filter</span>(<span class="hljs-function">(<span class="hljs-params">val</span>) =></span> val.<span class="hljs-title function_">startsWith</span>(rl.<span class="hljs-property">line</span>)).<span class="hljs-title function_">join</span>(<span class="hljs-string">' '</span>), 7801cb0ef41Sopenharmony_ci ); 7811cb0ef41Sopenharmony_ci}, <span class="hljs-number">300</span>); 7821cb0ef41Sopenharmony_ciprocess.<span class="hljs-property">stdin</span>.<span class="hljs-title function_">on</span>(<span class="hljs-string">'keypress'</span>, <span class="hljs-function">(<span class="hljs-params">c, k</span>) =></span> { 7831cb0ef41Sopenharmony_ci <span class="hljs-title function_">showResults</span>(); 7841cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre> 7851cb0ef41Sopenharmony_ci<h4><code>rl.cursor</code><span><a class="mark" href="#rlcursor" id="rlcursor">#</a></span><a aria-hidden="true" class="legacy" id="readline_rl_cursor"></a></h4> 7861cb0ef41Sopenharmony_ci<div class="api_metadata"> 7871cb0ef41Sopenharmony_ci<span>Added in: v0.1.98</span> 7881cb0ef41Sopenharmony_ci</div> 7891cb0ef41Sopenharmony_ci<ul> 7901cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Undefined_type" class="type"><undefined></a></li> 7911cb0ef41Sopenharmony_ci</ul> 7921cb0ef41Sopenharmony_ci<p>The cursor position relative to <code>rl.line</code>.</p> 7931cb0ef41Sopenharmony_ci<p>This will track where the current cursor lands in the input string, when 7941cb0ef41Sopenharmony_cireading input from a TTY stream. The position of cursor determines the 7951cb0ef41Sopenharmony_ciportion of the input string that will be modified as input is processed, 7961cb0ef41Sopenharmony_cias well as the column where the terminal caret will be rendered.</p> 7971cb0ef41Sopenharmony_ci<h4><code>rl.getCursorPos()</code><span><a class="mark" href="#rlgetcursorpos" id="rlgetcursorpos">#</a></span><a aria-hidden="true" class="legacy" id="readline_rl_getcursorpos"></a></h4> 7981cb0ef41Sopenharmony_ci<div class="api_metadata"> 7991cb0ef41Sopenharmony_ci<span>Added in: v13.5.0, v12.16.0</span> 8001cb0ef41Sopenharmony_ci</div> 8011cb0ef41Sopenharmony_ci<ul> 8021cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 8031cb0ef41Sopenharmony_ci<ul> 8041cb0ef41Sopenharmony_ci<li><code>rows</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> the row of the prompt the cursor currently lands on</li> 8051cb0ef41Sopenharmony_ci<li><code>cols</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> the screen column the cursor currently lands on</li> 8061cb0ef41Sopenharmony_ci</ul> 8071cb0ef41Sopenharmony_ci</li> 8081cb0ef41Sopenharmony_ci</ul> 8091cb0ef41Sopenharmony_ci<p>Returns the real position of the cursor in relation to the input 8101cb0ef41Sopenharmony_ciprompt + string. Long input (wrapping) strings, as well as multiple 8111cb0ef41Sopenharmony_ciline prompts are included in the calculations.</p> 8121cb0ef41Sopenharmony_ci</section><section><h3>Promises API<span><a class="mark" href="#promises-api" id="promises-api">#</a></span><a aria-hidden="true" class="legacy" id="readline_promises_api"></a></h3> 8131cb0ef41Sopenharmony_ci<div class="api_metadata"> 8141cb0ef41Sopenharmony_ci<span>Added in: v17.0.0</span> 8151cb0ef41Sopenharmony_ci</div> 8161cb0ef41Sopenharmony_ci<p></p><div class="api_stability api_stability_1"><a href="documentation.html#stability-index">Stability: 1</a> - Experimental</div><p></p> 8171cb0ef41Sopenharmony_ci<h4>Class: <code>readlinePromises.Interface</code><span><a class="mark" href="#class-readlinepromisesinterface" id="class-readlinepromisesinterface">#</a></span><a aria-hidden="true" class="legacy" id="readline_class_readlinepromises_interface"></a></h4> 8181cb0ef41Sopenharmony_ci<div class="api_metadata"> 8191cb0ef41Sopenharmony_ci<span>Added in: v17.0.0</span> 8201cb0ef41Sopenharmony_ci</div> 8211cb0ef41Sopenharmony_ci<ul> 8221cb0ef41Sopenharmony_ci<li>Extends: <a href="readline.html#class-interfaceconstructor" class="type"><readline.InterfaceConstructor></a></li> 8231cb0ef41Sopenharmony_ci</ul> 8241cb0ef41Sopenharmony_ci<p>Instances of the <code>readlinePromises.Interface</code> class are constructed using the 8251cb0ef41Sopenharmony_ci<code>readlinePromises.createInterface()</code> method. Every instance is associated with a 8261cb0ef41Sopenharmony_cisingle <code>input</code> <a href="stream.html#readable-streams">Readable</a> stream and a single <code>output</code> <a href="stream.html#writable-streams">Writable</a> stream. 8271cb0ef41Sopenharmony_ciThe <code>output</code> stream is used to print prompts for user input that arrives on, 8281cb0ef41Sopenharmony_ciand is read from, the <code>input</code> stream.</p> 8291cb0ef41Sopenharmony_ci<h5><code>rl.question(query[, options])</code><span><a class="mark" href="#rlquestionquery-options" id="rlquestionquery-options">#</a></span><a aria-hidden="true" class="legacy" id="readline_rl_question_query_options"></a></h5> 8301cb0ef41Sopenharmony_ci<div class="api_metadata"> 8311cb0ef41Sopenharmony_ci<span>Added in: v17.0.0</span> 8321cb0ef41Sopenharmony_ci</div> 8331cb0ef41Sopenharmony_ci<ul> 8341cb0ef41Sopenharmony_ci<li><code>query</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> A statement or query to write to <code>output</code>, prepended to the 8351cb0ef41Sopenharmony_ciprompt.</li> 8361cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 8371cb0ef41Sopenharmony_ci<ul> 8381cb0ef41Sopenharmony_ci<li><code>signal</code> <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a> Optionally allows the <code>question()</code> to be canceled 8391cb0ef41Sopenharmony_ciusing an <code>AbortSignal</code>.</li> 8401cb0ef41Sopenharmony_ci</ul> 8411cb0ef41Sopenharmony_ci</li> 8421cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a> A promise that is fulfilled with the user's 8431cb0ef41Sopenharmony_ciinput in response to the <code>query</code>.</li> 8441cb0ef41Sopenharmony_ci</ul> 8451cb0ef41Sopenharmony_ci<p>The <code>rl.question()</code> method displays the <code>query</code> by writing it to the <code>output</code>, 8461cb0ef41Sopenharmony_ciwaits for user input to be provided on <code>input</code>, then invokes the <code>callback</code> 8471cb0ef41Sopenharmony_cifunction passing the provided input as the first argument.</p> 8481cb0ef41Sopenharmony_ci<p>When called, <code>rl.question()</code> will resume the <code>input</code> stream if it has been 8491cb0ef41Sopenharmony_cipaused.</p> 8501cb0ef41Sopenharmony_ci<p>If the <code>readlinePromises.Interface</code> was created with <code>output</code> set to <code>null</code> or 8511cb0ef41Sopenharmony_ci<code>undefined</code> the <code>query</code> is not written.</p> 8521cb0ef41Sopenharmony_ci<p>If the question is called after <code>rl.close()</code>, it returns a rejected promise.</p> 8531cb0ef41Sopenharmony_ci<p>Example usage:</p> 8541cb0ef41Sopenharmony_ci<pre><code class="language-js mjs"><span class="hljs-keyword">const</span> answer = <span class="hljs-keyword">await</span> rl.<span class="hljs-title function_">question</span>(<span class="hljs-string">'What is your favorite food? '</span>); 8551cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`Oh, so your favorite food is <span class="hljs-subst">${answer}</span>`</span>);</code> <button class="copy-button">copy</button></pre> 8561cb0ef41Sopenharmony_ci<p>Using an <code>AbortSignal</code> to cancel a question.</p> 8571cb0ef41Sopenharmony_ci<pre><code class="language-js mjs"><span class="hljs-keyword">const</span> signal = <span class="hljs-title class_">AbortSignal</span>.<span class="hljs-title function_">timeout</span>(<span class="hljs-number">10_000</span>); 8581cb0ef41Sopenharmony_ci 8591cb0ef41Sopenharmony_cisignal.<span class="hljs-title function_">addEventListener</span>(<span class="hljs-string">'abort'</span>, <span class="hljs-function">() =></span> { 8601cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'The food question timed out'</span>); 8611cb0ef41Sopenharmony_ci}, { <span class="hljs-attr">once</span>: <span class="hljs-literal">true</span> }); 8621cb0ef41Sopenharmony_ci 8631cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> answer = <span class="hljs-keyword">await</span> rl.<span class="hljs-title function_">question</span>(<span class="hljs-string">'What is your favorite food? '</span>, { signal }); 8641cb0ef41Sopenharmony_ci<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`Oh, so your favorite food is <span class="hljs-subst">${answer}</span>`</span>);</code> <button class="copy-button">copy</button></pre> 8651cb0ef41Sopenharmony_ci<h4>Class: <code>readlinePromises.Readline</code><span><a class="mark" href="#class-readlinepromisesreadline" id="class-readlinepromisesreadline">#</a></span><a aria-hidden="true" class="legacy" id="readline_class_readlinepromises_readline"></a></h4> 8661cb0ef41Sopenharmony_ci<div class="api_metadata"> 8671cb0ef41Sopenharmony_ci<span>Added in: v17.0.0</span> 8681cb0ef41Sopenharmony_ci</div> 8691cb0ef41Sopenharmony_ci<h5><code>new readlinePromises.Readline(stream[, options])</code><span><a class="mark" href="#new-readlinepromisesreadlinestream-options" id="new-readlinepromisesreadlinestream-options">#</a></span><a aria-hidden="true" class="legacy" id="readline_new_readlinepromises_readline_stream_options"></a></h5> 8701cb0ef41Sopenharmony_ci<div class="api_metadata"> 8711cb0ef41Sopenharmony_ci<span>Added in: v17.0.0</span> 8721cb0ef41Sopenharmony_ci</div> 8731cb0ef41Sopenharmony_ci<ul> 8741cb0ef41Sopenharmony_ci<li><code>stream</code> <a href="stream.html#class-streamwritable" class="type"><stream.Writable></a> A <a href="tty.html">TTY</a> stream.</li> 8751cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 8761cb0ef41Sopenharmony_ci<ul> 8771cb0ef41Sopenharmony_ci<li><code>autoCommit</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> If <code>true</code>, no need to call <code>rl.commit()</code>.</li> 8781cb0ef41Sopenharmony_ci</ul> 8791cb0ef41Sopenharmony_ci</li> 8801cb0ef41Sopenharmony_ci</ul> 8811cb0ef41Sopenharmony_ci<h5><code>rl.clearLine(dir)</code><span><a class="mark" href="#rlclearlinedir" id="rlclearlinedir">#</a></span><a aria-hidden="true" class="legacy" id="readline_rl_clearline_dir"></a></h5> 8821cb0ef41Sopenharmony_ci<div class="api_metadata"> 8831cb0ef41Sopenharmony_ci<span>Added in: v17.0.0</span> 8841cb0ef41Sopenharmony_ci</div> 8851cb0ef41Sopenharmony_ci<ul> 8861cb0ef41Sopenharmony_ci<li><code>dir</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> 8871cb0ef41Sopenharmony_ci<ul> 8881cb0ef41Sopenharmony_ci<li><code>-1</code>: to the left from cursor</li> 8891cb0ef41Sopenharmony_ci<li><code>1</code>: to the right from cursor</li> 8901cb0ef41Sopenharmony_ci<li><code>0</code>: the entire line</li> 8911cb0ef41Sopenharmony_ci</ul> 8921cb0ef41Sopenharmony_ci</li> 8931cb0ef41Sopenharmony_ci<li>Returns: this</li> 8941cb0ef41Sopenharmony_ci</ul> 8951cb0ef41Sopenharmony_ci<p>The <code>rl.clearLine()</code> method adds to the internal list of pending action an 8961cb0ef41Sopenharmony_ciaction that clears current line of the associated <code>stream</code> in a specified 8971cb0ef41Sopenharmony_cidirection identified by <code>dir</code>. 8981cb0ef41Sopenharmony_ciCall <code>rl.commit()</code> to see the effect of this method, unless <code>autoCommit: true</code> 8991cb0ef41Sopenharmony_ciwas passed to the constructor.</p> 9001cb0ef41Sopenharmony_ci<h5><code>rl.clearScreenDown()</code><span><a class="mark" href="#rlclearscreendown" id="rlclearscreendown">#</a></span><a aria-hidden="true" class="legacy" id="readline_rl_clearscreendown"></a></h5> 9011cb0ef41Sopenharmony_ci<div class="api_metadata"> 9021cb0ef41Sopenharmony_ci<span>Added in: v17.0.0</span> 9031cb0ef41Sopenharmony_ci</div> 9041cb0ef41Sopenharmony_ci<ul> 9051cb0ef41Sopenharmony_ci<li>Returns: this</li> 9061cb0ef41Sopenharmony_ci</ul> 9071cb0ef41Sopenharmony_ci<p>The <code>rl.clearScreenDown()</code> method adds to the internal list of pending action an 9081cb0ef41Sopenharmony_ciaction that clears the associated stream from the current position of the 9091cb0ef41Sopenharmony_cicursor down. 9101cb0ef41Sopenharmony_ciCall <code>rl.commit()</code> to see the effect of this method, unless <code>autoCommit: true</code> 9111cb0ef41Sopenharmony_ciwas passed to the constructor.</p> 9121cb0ef41Sopenharmony_ci<h5><code>rl.commit()</code><span><a class="mark" href="#rlcommit" id="rlcommit">#</a></span><a aria-hidden="true" class="legacy" id="readline_rl_commit"></a></h5> 9131cb0ef41Sopenharmony_ci<div class="api_metadata"> 9141cb0ef41Sopenharmony_ci<span>Added in: v17.0.0</span> 9151cb0ef41Sopenharmony_ci</div> 9161cb0ef41Sopenharmony_ci<ul> 9171cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a></li> 9181cb0ef41Sopenharmony_ci</ul> 9191cb0ef41Sopenharmony_ci<p>The <code>rl.commit()</code> method sends all the pending actions to the associated 9201cb0ef41Sopenharmony_ci<code>stream</code> and clears the internal list of pending actions.</p> 9211cb0ef41Sopenharmony_ci<h5><code>rl.cursorTo(x[, y])</code><span><a class="mark" href="#rlcursortox-y" id="rlcursortox-y">#</a></span><a aria-hidden="true" class="legacy" id="readline_rl_cursorto_x_y"></a></h5> 9221cb0ef41Sopenharmony_ci<div class="api_metadata"> 9231cb0ef41Sopenharmony_ci<span>Added in: v17.0.0</span> 9241cb0ef41Sopenharmony_ci</div> 9251cb0ef41Sopenharmony_ci<ul> 9261cb0ef41Sopenharmony_ci<li><code>x</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 9271cb0ef41Sopenharmony_ci<li><code>y</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 9281cb0ef41Sopenharmony_ci<li>Returns: this</li> 9291cb0ef41Sopenharmony_ci</ul> 9301cb0ef41Sopenharmony_ci<p>The <code>rl.cursorTo()</code> method adds to the internal list of pending action an action 9311cb0ef41Sopenharmony_cithat moves cursor to the specified position in the associated <code>stream</code>. 9321cb0ef41Sopenharmony_ciCall <code>rl.commit()</code> to see the effect of this method, unless <code>autoCommit: true</code> 9331cb0ef41Sopenharmony_ciwas passed to the constructor.</p> 9341cb0ef41Sopenharmony_ci<h5><code>rl.moveCursor(dx, dy)</code><span><a class="mark" href="#rlmovecursordx-dy" id="rlmovecursordx-dy">#</a></span><a aria-hidden="true" class="legacy" id="readline_rl_movecursor_dx_dy"></a></h5> 9351cb0ef41Sopenharmony_ci<div class="api_metadata"> 9361cb0ef41Sopenharmony_ci<span>Added in: v17.0.0</span> 9371cb0ef41Sopenharmony_ci</div> 9381cb0ef41Sopenharmony_ci<ul> 9391cb0ef41Sopenharmony_ci<li><code>dx</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 9401cb0ef41Sopenharmony_ci<li><code>dy</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a></li> 9411cb0ef41Sopenharmony_ci<li>Returns: this</li> 9421cb0ef41Sopenharmony_ci</ul> 9431cb0ef41Sopenharmony_ci<p>The <code>rl.moveCursor()</code> method adds to the internal list of pending action an 9441cb0ef41Sopenharmony_ciaction that moves the cursor <em>relative</em> to its current position in the 9451cb0ef41Sopenharmony_ciassociated <code>stream</code>. 9461cb0ef41Sopenharmony_ciCall <code>rl.commit()</code> to see the effect of this method, unless <code>autoCommit: true</code> 9471cb0ef41Sopenharmony_ciwas passed to the constructor.</p> 9481cb0ef41Sopenharmony_ci<h5><code>rl.rollback()</code><span><a class="mark" href="#rlrollback" id="rlrollback">#</a></span><a aria-hidden="true" class="legacy" id="readline_rl_rollback"></a></h5> 9491cb0ef41Sopenharmony_ci<div class="api_metadata"> 9501cb0ef41Sopenharmony_ci<span>Added in: v17.0.0</span> 9511cb0ef41Sopenharmony_ci</div> 9521cb0ef41Sopenharmony_ci<ul> 9531cb0ef41Sopenharmony_ci<li>Returns: this</li> 9541cb0ef41Sopenharmony_ci</ul> 9551cb0ef41Sopenharmony_ci<p>The <code>rl.rollback</code> methods clears the internal list of pending actions without 9561cb0ef41Sopenharmony_cisending it to the associated <code>stream</code>.</p> 9571cb0ef41Sopenharmony_ci<h4><code>readlinePromises.createInterface(options)</code><span><a class="mark" href="#readlinepromisescreateinterfaceoptions" id="readlinepromisescreateinterfaceoptions">#</a></span><a aria-hidden="true" class="legacy" id="readline_readlinepromises_createinterface_options"></a></h4> 9581cb0ef41Sopenharmony_ci<div class="api_metadata"> 9591cb0ef41Sopenharmony_ci<span>Added in: v17.0.0</span> 9601cb0ef41Sopenharmony_ci</div> 9611cb0ef41Sopenharmony_ci<ul> 9621cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 9631cb0ef41Sopenharmony_ci<ul> 9641cb0ef41Sopenharmony_ci<li><code>input</code> <a href="stream.html#class-streamreadable" class="type"><stream.Readable></a> The <a href="stream.html#readable-streams">Readable</a> stream to listen to. This option 9651cb0ef41Sopenharmony_ciis <em>required</em>.</li> 9661cb0ef41Sopenharmony_ci<li><code>output</code> <a href="stream.html#class-streamwritable" class="type"><stream.Writable></a> The <a href="stream.html#writable-streams">Writable</a> stream to write readline data 9671cb0ef41Sopenharmony_cito.</li> 9681cb0ef41Sopenharmony_ci<li><code>completer</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> An optional function used for Tab autocompletion.</li> 9691cb0ef41Sopenharmony_ci<li><code>terminal</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <code>true</code> if the <code>input</code> and <code>output</code> streams should be 9701cb0ef41Sopenharmony_citreated like a TTY, and have ANSI/VT100 escape codes written to it. 9711cb0ef41Sopenharmony_ci<strong>Default:</strong> checking <code>isTTY</code> on the <code>output</code> stream upon instantiation.</li> 9721cb0ef41Sopenharmony_ci<li><code>history</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string[]></a> Initial list of history lines. This option makes sense 9731cb0ef41Sopenharmony_cionly if <code>terminal</code> is set to <code>true</code> by the user or by an internal <code>output</code> 9741cb0ef41Sopenharmony_cicheck, otherwise the history caching mechanism is not initialized at all. 9751cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>[]</code>.</li> 9761cb0ef41Sopenharmony_ci<li><code>historySize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Maximum number of history lines retained. To disable 9771cb0ef41Sopenharmony_cithe history set this value to <code>0</code>. This option makes sense only if 9781cb0ef41Sopenharmony_ci<code>terminal</code> is set to <code>true</code> by the user or by an internal <code>output</code> check, 9791cb0ef41Sopenharmony_ciotherwise the history caching mechanism is not initialized at all. 9801cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>30</code>.</li> 9811cb0ef41Sopenharmony_ci<li><code>removeHistoryDuplicates</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> If <code>true</code>, when a new input line added 9821cb0ef41Sopenharmony_cito the history list duplicates an older one, this removes the older line 9831cb0ef41Sopenharmony_cifrom the list. <strong>Default:</strong> <code>false</code>.</li> 9841cb0ef41Sopenharmony_ci<li><code>prompt</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The prompt string to use. <strong>Default:</strong> <code>'> '</code>.</li> 9851cb0ef41Sopenharmony_ci<li><code>crlfDelay</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> If the delay between <code>\r</code> and <code>\n</code> exceeds 9861cb0ef41Sopenharmony_ci<code>crlfDelay</code> milliseconds, both <code>\r</code> and <code>\n</code> will be treated as separate 9871cb0ef41Sopenharmony_ciend-of-line input. <code>crlfDelay</code> will be coerced to a number no less than 9881cb0ef41Sopenharmony_ci<code>100</code>. It can be set to <code>Infinity</code>, in which case <code>\r</code> followed by <code>\n</code> 9891cb0ef41Sopenharmony_ciwill always be considered a single newline (which may be reasonable for 9901cb0ef41Sopenharmony_ci<a href="#example-read-file-stream-line-by-line">reading files</a> with <code>\r\n</code> line delimiter). <strong>Default:</strong> <code>100</code>.</li> 9911cb0ef41Sopenharmony_ci<li><code>escapeCodeTimeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The duration <code>readlinePromises</code> will wait for a 9921cb0ef41Sopenharmony_cicharacter (when reading an ambiguous key sequence in milliseconds one that 9931cb0ef41Sopenharmony_cican both form a complete key sequence using the input read so far and can 9941cb0ef41Sopenharmony_citake additional input to complete a longer key sequence). 9951cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>500</code>.</li> 9961cb0ef41Sopenharmony_ci<li><code>tabSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> The number of spaces a tab is equal to (minimum 1). 9971cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>8</code>.</li> 9981cb0ef41Sopenharmony_ci</ul> 9991cb0ef41Sopenharmony_ci</li> 10001cb0ef41Sopenharmony_ci<li>Returns: <a href="readline.html#class-readlinepromisesinterface" class="type"><readlinePromises.Interface></a></li> 10011cb0ef41Sopenharmony_ci</ul> 10021cb0ef41Sopenharmony_ci<p>The <code>readlinePromises.createInterface()</code> method creates a new <code>readlinePromises.Interface</code> 10031cb0ef41Sopenharmony_ciinstance.</p> 10041cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">const</span> readlinePromises = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:readline/promises'</span>); 10051cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> rl = readlinePromises.<span class="hljs-title function_">createInterface</span>({ 10061cb0ef41Sopenharmony_ci <span class="hljs-attr">input</span>: process.<span class="hljs-property">stdin</span>, 10071cb0ef41Sopenharmony_ci <span class="hljs-attr">output</span>: process.<span class="hljs-property">stdout</span>, 10081cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre> 10091cb0ef41Sopenharmony_ci<p>Once the <code>readlinePromises.Interface</code> instance is created, the most common case 10101cb0ef41Sopenharmony_ciis to listen for the <code>'line'</code> event:</p> 10111cb0ef41Sopenharmony_ci<pre><code class="language-js">rl.<span class="hljs-title function_">on</span>(<span class="hljs-string">'line'</span>, <span class="hljs-function">(<span class="hljs-params">line</span>) =></span> { 10121cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`Received: <span class="hljs-subst">${line}</span>`</span>); 10131cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre> 10141cb0ef41Sopenharmony_ci<p>If <code>terminal</code> is <code>true</code> for this instance then the <code>output</code> stream will get 10151cb0ef41Sopenharmony_cithe best compatibility if it defines an <code>output.columns</code> property and emits 10161cb0ef41Sopenharmony_cia <code>'resize'</code> event on the <code>output</code> if or when the columns ever change 10171cb0ef41Sopenharmony_ci(<a href="process.html#processstdout"><code>process.stdout</code></a> does this automatically when it is a TTY).</p> 10181cb0ef41Sopenharmony_ci<h5>Use of the <code>completer</code> function<span><a class="mark" href="#use-of-the-completer-function" id="use-of-the-completer-function">#</a></span><a aria-hidden="true" class="legacy" id="readline_use_of_the_completer_function"></a></h5> 10191cb0ef41Sopenharmony_ci<p>The <code>completer</code> function takes the current line entered by the user 10201cb0ef41Sopenharmony_cias an argument, and returns an <code>Array</code> with 2 entries:</p> 10211cb0ef41Sopenharmony_ci<ul> 10221cb0ef41Sopenharmony_ci<li>An <code>Array</code> with matching entries for the completion.</li> 10231cb0ef41Sopenharmony_ci<li>The substring that was used for the matching.</li> 10241cb0ef41Sopenharmony_ci</ul> 10251cb0ef41Sopenharmony_ci<p>For instance: <code>[[substr1, substr2, ...], originalsubstring]</code>.</p> 10261cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">function</span> <span class="hljs-title function_">completer</span>(<span class="hljs-params">line</span>) { 10271cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> completions = <span class="hljs-string">'.help .error .exit .quit .q'</span>.<span class="hljs-title function_">split</span>(<span class="hljs-string">' '</span>); 10281cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> hits = completions.<span class="hljs-title function_">filter</span>(<span class="hljs-function">(<span class="hljs-params">c</span>) =></span> c.<span class="hljs-title function_">startsWith</span>(line)); 10291cb0ef41Sopenharmony_ci <span class="hljs-comment">// Show all completions if none found</span> 10301cb0ef41Sopenharmony_ci <span class="hljs-keyword">return</span> [hits.<span class="hljs-property">length</span> ? hits : completions, line]; 10311cb0ef41Sopenharmony_ci}</code> <button class="copy-button">copy</button></pre> 10321cb0ef41Sopenharmony_ci<p>The <code>completer</code> function can also return a <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" class="type"><Promise></a>, or be asynchronous:</p> 10331cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-title function_">completer</span>(<span class="hljs-params">linePartial</span>) { 10341cb0ef41Sopenharmony_ci <span class="hljs-keyword">await</span> <span class="hljs-title function_">someAsyncWork</span>(); 10351cb0ef41Sopenharmony_ci <span class="hljs-keyword">return</span> [[<span class="hljs-string">'123'</span>], linePartial]; 10361cb0ef41Sopenharmony_ci}</code> <button class="copy-button">copy</button></pre> 10371cb0ef41Sopenharmony_ci</section><section><h3>Callback API<span><a class="mark" href="#callback-api" id="callback-api">#</a></span><a aria-hidden="true" class="legacy" id="readline_callback_api"></a></h3> 10381cb0ef41Sopenharmony_ci<div class="api_metadata"> 10391cb0ef41Sopenharmony_ci<span>Added in: v0.1.104</span> 10401cb0ef41Sopenharmony_ci</div> 10411cb0ef41Sopenharmony_ci<h4>Class: <code>readline.Interface</code><span><a class="mark" href="#class-readlineinterface" id="class-readlineinterface">#</a></span><a aria-hidden="true" class="legacy" id="readline_class_readline_interface"></a></h4> 10421cb0ef41Sopenharmony_ci<div class="api_metadata"> 10431cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 10441cb0ef41Sopenharmony_ci<table> 10451cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 10461cb0ef41Sopenharmony_ci<tr><td>v17.0.0</td> 10471cb0ef41Sopenharmony_ci<td><p>The class <code>readline.Interface</code> now inherits from <code>Interface</code>.</p></td></tr> 10481cb0ef41Sopenharmony_ci<tr><td>v0.1.104</td> 10491cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.1.104</span></p></td></tr> 10501cb0ef41Sopenharmony_ci</tbody></table> 10511cb0ef41Sopenharmony_ci</details> 10521cb0ef41Sopenharmony_ci</div> 10531cb0ef41Sopenharmony_ci<ul> 10541cb0ef41Sopenharmony_ci<li>Extends: <a href="readline.html#class-interfaceconstructor" class="type"><readline.InterfaceConstructor></a></li> 10551cb0ef41Sopenharmony_ci</ul> 10561cb0ef41Sopenharmony_ci<p>Instances of the <code>readline.Interface</code> class are constructed using the 10571cb0ef41Sopenharmony_ci<code>readline.createInterface()</code> method. Every instance is associated with a 10581cb0ef41Sopenharmony_cisingle <code>input</code> <a href="stream.html#readable-streams">Readable</a> stream and a single <code>output</code> <a href="stream.html#writable-streams">Writable</a> stream. 10591cb0ef41Sopenharmony_ciThe <code>output</code> stream is used to print prompts for user input that arrives on, 10601cb0ef41Sopenharmony_ciand is read from, the <code>input</code> stream.</p> 10611cb0ef41Sopenharmony_ci<h5><code>rl.question(query[, options], callback)</code><span><a class="mark" href="#rlquestionquery-options-callback_1" id="rlquestionquery-options-callback_1">#</a></span><a aria-hidden="true" class="legacy" id="readline_rl_question_query_options_callback_1"></a></h5> 10621cb0ef41Sopenharmony_ci<div class="api_metadata"> 10631cb0ef41Sopenharmony_ci<span>Added in: v0.3.3</span> 10641cb0ef41Sopenharmony_ci</div> 10651cb0ef41Sopenharmony_ci<ul> 10661cb0ef41Sopenharmony_ci<li><code>query</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> A statement or query to write to <code>output</code>, prepended to the 10671cb0ef41Sopenharmony_ciprompt.</li> 10681cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 10691cb0ef41Sopenharmony_ci<ul> 10701cb0ef41Sopenharmony_ci<li><code>signal</code> <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a> Optionally allows the <code>question()</code> to be canceled 10711cb0ef41Sopenharmony_ciusing an <code>AbortController</code>.</li> 10721cb0ef41Sopenharmony_ci</ul> 10731cb0ef41Sopenharmony_ci</li> 10741cb0ef41Sopenharmony_ci<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> A callback function that is invoked with the user's 10751cb0ef41Sopenharmony_ciinput in response to the <code>query</code>.</li> 10761cb0ef41Sopenharmony_ci</ul> 10771cb0ef41Sopenharmony_ci<p>The <code>rl.question()</code> method displays the <code>query</code> by writing it to the <code>output</code>, 10781cb0ef41Sopenharmony_ciwaits for user input to be provided on <code>input</code>, then invokes the <code>callback</code> 10791cb0ef41Sopenharmony_cifunction passing the provided input as the first argument.</p> 10801cb0ef41Sopenharmony_ci<p>When called, <code>rl.question()</code> will resume the <code>input</code> stream if it has been 10811cb0ef41Sopenharmony_cipaused.</p> 10821cb0ef41Sopenharmony_ci<p>If the <code>readline.Interface</code> was created with <code>output</code> set to <code>null</code> or 10831cb0ef41Sopenharmony_ci<code>undefined</code> the <code>query</code> is not written.</p> 10841cb0ef41Sopenharmony_ci<p>The <code>callback</code> function passed to <code>rl.question()</code> does not follow the typical 10851cb0ef41Sopenharmony_cipattern of accepting an <code>Error</code> object or <code>null</code> as the first argument. 10861cb0ef41Sopenharmony_ciThe <code>callback</code> is called with the provided answer as the only argument.</p> 10871cb0ef41Sopenharmony_ci<p>An error will be thrown if calling <code>rl.question()</code> after <code>rl.close()</code>.</p> 10881cb0ef41Sopenharmony_ci<p>Example usage:</p> 10891cb0ef41Sopenharmony_ci<pre><code class="language-js">rl.<span class="hljs-title function_">question</span>(<span class="hljs-string">'What is your favorite food? '</span>, <span class="hljs-function">(<span class="hljs-params">answer</span>) =></span> { 10901cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`Oh, so your favorite food is <span class="hljs-subst">${answer}</span>`</span>); 10911cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre> 10921cb0ef41Sopenharmony_ci<p>Using an <code>AbortController</code> to cancel a question.</p> 10931cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">const</span> ac = <span class="hljs-keyword">new</span> <span class="hljs-title class_">AbortController</span>(); 10941cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> signal = ac.<span class="hljs-property">signal</span>; 10951cb0ef41Sopenharmony_ci 10961cb0ef41Sopenharmony_cirl.<span class="hljs-title function_">question</span>(<span class="hljs-string">'What is your favorite food? '</span>, { signal }, <span class="hljs-function">(<span class="hljs-params">answer</span>) =></span> { 10971cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`Oh, so your favorite food is <span class="hljs-subst">${answer}</span>`</span>); 10981cb0ef41Sopenharmony_ci}); 10991cb0ef41Sopenharmony_ci 11001cb0ef41Sopenharmony_cisignal.<span class="hljs-title function_">addEventListener</span>(<span class="hljs-string">'abort'</span>, <span class="hljs-function">() =></span> { 11011cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'The food question timed out'</span>); 11021cb0ef41Sopenharmony_ci}, { <span class="hljs-attr">once</span>: <span class="hljs-literal">true</span> }); 11031cb0ef41Sopenharmony_ci 11041cb0ef41Sopenharmony_ci<span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =></span> ac.<span class="hljs-title function_">abort</span>(), <span class="hljs-number">10000</span>);</code> <button class="copy-button">copy</button></pre> 11051cb0ef41Sopenharmony_ci<h4><code>readline.clearLine(stream, dir[, callback])</code><span><a class="mark" href="#readlineclearlinestream-dir-callback" id="readlineclearlinestream-dir-callback">#</a></span><a aria-hidden="true" class="legacy" id="readline_readline_clearline_stream_dir_callback"></a></h4> 11061cb0ef41Sopenharmony_ci<div class="api_metadata"> 11071cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 11081cb0ef41Sopenharmony_ci<table> 11091cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 11101cb0ef41Sopenharmony_ci<tr><td>v18.0.0</td> 11111cb0ef41Sopenharmony_ci<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr> 11121cb0ef41Sopenharmony_ci<tr><td>v12.7.0</td> 11131cb0ef41Sopenharmony_ci<td><p>The stream's write() callback and return value are exposed.</p></td></tr> 11141cb0ef41Sopenharmony_ci<tr><td>v0.7.7</td> 11151cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.7.7</span></p></td></tr> 11161cb0ef41Sopenharmony_ci</tbody></table> 11171cb0ef41Sopenharmony_ci</details> 11181cb0ef41Sopenharmony_ci</div> 11191cb0ef41Sopenharmony_ci<ul> 11201cb0ef41Sopenharmony_ci<li><code>stream</code> <a href="stream.html#class-streamwritable" class="type"><stream.Writable></a></li> 11211cb0ef41Sopenharmony_ci<li><code>dir</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> 11221cb0ef41Sopenharmony_ci<ul> 11231cb0ef41Sopenharmony_ci<li><code>-1</code>: to the left from cursor</li> 11241cb0ef41Sopenharmony_ci<li><code>1</code>: to the right from cursor</li> 11251cb0ef41Sopenharmony_ci<li><code>0</code>: the entire line</li> 11261cb0ef41Sopenharmony_ci</ul> 11271cb0ef41Sopenharmony_ci</li> 11281cb0ef41Sopenharmony_ci<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> Invoked once the operation completes.</li> 11291cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <code>false</code> if <code>stream</code> wishes for the calling code to wait for 11301cb0ef41Sopenharmony_cithe <code>'drain'</code> event to be emitted before continuing to write additional data; 11311cb0ef41Sopenharmony_ciotherwise <code>true</code>.</li> 11321cb0ef41Sopenharmony_ci</ul> 11331cb0ef41Sopenharmony_ci<p>The <code>readline.clearLine()</code> method clears current line of given <a href="tty.html">TTY</a> stream 11341cb0ef41Sopenharmony_ciin a specified direction identified by <code>dir</code>.</p> 11351cb0ef41Sopenharmony_ci<h4><code>readline.clearScreenDown(stream[, callback])</code><span><a class="mark" href="#readlineclearscreendownstream-callback" id="readlineclearscreendownstream-callback">#</a></span><a aria-hidden="true" class="legacy" id="readline_readline_clearscreendown_stream_callback"></a></h4> 11361cb0ef41Sopenharmony_ci<div class="api_metadata"> 11371cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 11381cb0ef41Sopenharmony_ci<table> 11391cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 11401cb0ef41Sopenharmony_ci<tr><td>v18.0.0</td> 11411cb0ef41Sopenharmony_ci<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr> 11421cb0ef41Sopenharmony_ci<tr><td>v12.7.0</td> 11431cb0ef41Sopenharmony_ci<td><p>The stream's write() callback and return value are exposed.</p></td></tr> 11441cb0ef41Sopenharmony_ci<tr><td>v0.7.7</td> 11451cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.7.7</span></p></td></tr> 11461cb0ef41Sopenharmony_ci</tbody></table> 11471cb0ef41Sopenharmony_ci</details> 11481cb0ef41Sopenharmony_ci</div> 11491cb0ef41Sopenharmony_ci<ul> 11501cb0ef41Sopenharmony_ci<li><code>stream</code> <a href="stream.html#class-streamwritable" class="type"><stream.Writable></a></li> 11511cb0ef41Sopenharmony_ci<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> Invoked once the operation completes.</li> 11521cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <code>false</code> if <code>stream</code> wishes for the calling code to wait for 11531cb0ef41Sopenharmony_cithe <code>'drain'</code> event to be emitted before continuing to write additional data; 11541cb0ef41Sopenharmony_ciotherwise <code>true</code>.</li> 11551cb0ef41Sopenharmony_ci</ul> 11561cb0ef41Sopenharmony_ci<p>The <code>readline.clearScreenDown()</code> method clears the given <a href="tty.html">TTY</a> stream from 11571cb0ef41Sopenharmony_cithe current position of the cursor down.</p> 11581cb0ef41Sopenharmony_ci<h4><code>readline.createInterface(options)</code><span><a class="mark" href="#readlinecreateinterfaceoptions" id="readlinecreateinterfaceoptions">#</a></span><a aria-hidden="true" class="legacy" id="readline_readline_createinterface_options"></a></h4> 11591cb0ef41Sopenharmony_ci<div class="api_metadata"> 11601cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 11611cb0ef41Sopenharmony_ci<table> 11621cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 11631cb0ef41Sopenharmony_ci<tr><td>v15.14.0, v14.18.0</td> 11641cb0ef41Sopenharmony_ci<td><p>The <code>signal</code> option is supported now.</p></td></tr> 11651cb0ef41Sopenharmony_ci<tr><td>v15.8.0, v14.18.0</td> 11661cb0ef41Sopenharmony_ci<td><p>The <code>history</code> option is supported now.</p></td></tr> 11671cb0ef41Sopenharmony_ci<tr><td>v13.9.0</td> 11681cb0ef41Sopenharmony_ci<td><p>The <code>tabSize</code> option is supported now.</p></td></tr> 11691cb0ef41Sopenharmony_ci<tr><td>v8.3.0, v6.11.4</td> 11701cb0ef41Sopenharmony_ci<td><p>Remove max limit of <code>crlfDelay</code> option.</p></td></tr> 11711cb0ef41Sopenharmony_ci<tr><td>v6.6.0</td> 11721cb0ef41Sopenharmony_ci<td><p>The <code>crlfDelay</code> option is supported now.</p></td></tr> 11731cb0ef41Sopenharmony_ci<tr><td>v6.3.0</td> 11741cb0ef41Sopenharmony_ci<td><p>The <code>prompt</code> option is supported now.</p></td></tr> 11751cb0ef41Sopenharmony_ci<tr><td>v6.0.0</td> 11761cb0ef41Sopenharmony_ci<td><p>The <code>historySize</code> option can be <code>0</code> now.</p></td></tr> 11771cb0ef41Sopenharmony_ci<tr><td>v0.1.98</td> 11781cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.1.98</span></p></td></tr> 11791cb0ef41Sopenharmony_ci</tbody></table> 11801cb0ef41Sopenharmony_ci</details> 11811cb0ef41Sopenharmony_ci</div> 11821cb0ef41Sopenharmony_ci<ul> 11831cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> 11841cb0ef41Sopenharmony_ci<ul> 11851cb0ef41Sopenharmony_ci<li><code>input</code> <a href="stream.html#class-streamreadable" class="type"><stream.Readable></a> The <a href="stream.html#readable-streams">Readable</a> stream to listen to. This option 11861cb0ef41Sopenharmony_ciis <em>required</em>.</li> 11871cb0ef41Sopenharmony_ci<li><code>output</code> <a href="stream.html#class-streamwritable" class="type"><stream.Writable></a> The <a href="stream.html#writable-streams">Writable</a> stream to write readline data 11881cb0ef41Sopenharmony_cito.</li> 11891cb0ef41Sopenharmony_ci<li><code>completer</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> An optional function used for Tab autocompletion.</li> 11901cb0ef41Sopenharmony_ci<li><code>terminal</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <code>true</code> if the <code>input</code> and <code>output</code> streams should be 11911cb0ef41Sopenharmony_citreated like a TTY, and have ANSI/VT100 escape codes written to it. 11921cb0ef41Sopenharmony_ci<strong>Default:</strong> checking <code>isTTY</code> on the <code>output</code> stream upon instantiation.</li> 11931cb0ef41Sopenharmony_ci<li><code>history</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string[]></a> Initial list of history lines. This option makes sense 11941cb0ef41Sopenharmony_cionly if <code>terminal</code> is set to <code>true</code> by the user or by an internal <code>output</code> 11951cb0ef41Sopenharmony_cicheck, otherwise the history caching mechanism is not initialized at all. 11961cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>[]</code>.</li> 11971cb0ef41Sopenharmony_ci<li><code>historySize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Maximum number of history lines retained. To disable 11981cb0ef41Sopenharmony_cithe history set this value to <code>0</code>. This option makes sense only if 11991cb0ef41Sopenharmony_ci<code>terminal</code> is set to <code>true</code> by the user or by an internal <code>output</code> check, 12001cb0ef41Sopenharmony_ciotherwise the history caching mechanism is not initialized at all. 12011cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>30</code>.</li> 12021cb0ef41Sopenharmony_ci<li><code>removeHistoryDuplicates</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> If <code>true</code>, when a new input line added 12031cb0ef41Sopenharmony_cito the history list duplicates an older one, this removes the older line 12041cb0ef41Sopenharmony_cifrom the list. <strong>Default:</strong> <code>false</code>.</li> 12051cb0ef41Sopenharmony_ci<li><code>prompt</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The prompt string to use. <strong>Default:</strong> <code>'> '</code>.</li> 12061cb0ef41Sopenharmony_ci<li><code>crlfDelay</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> If the delay between <code>\r</code> and <code>\n</code> exceeds 12071cb0ef41Sopenharmony_ci<code>crlfDelay</code> milliseconds, both <code>\r</code> and <code>\n</code> will be treated as separate 12081cb0ef41Sopenharmony_ciend-of-line input. <code>crlfDelay</code> will be coerced to a number no less than 12091cb0ef41Sopenharmony_ci<code>100</code>. It can be set to <code>Infinity</code>, in which case <code>\r</code> followed by <code>\n</code> 12101cb0ef41Sopenharmony_ciwill always be considered a single newline (which may be reasonable for 12111cb0ef41Sopenharmony_ci<a href="#example-read-file-stream-line-by-line">reading files</a> with <code>\r\n</code> line delimiter). <strong>Default:</strong> <code>100</code>.</li> 12121cb0ef41Sopenharmony_ci<li><code>escapeCodeTimeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> The duration <code>readline</code> will wait for a 12131cb0ef41Sopenharmony_cicharacter (when reading an ambiguous key sequence in milliseconds one that 12141cb0ef41Sopenharmony_cican both form a complete key sequence using the input read so far and can 12151cb0ef41Sopenharmony_citake additional input to complete a longer key sequence). 12161cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>500</code>.</li> 12171cb0ef41Sopenharmony_ci<li><code>tabSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> The number of spaces a tab is equal to (minimum 1). 12181cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>8</code>.</li> 12191cb0ef41Sopenharmony_ci<li><code>signal</code> <a href="globals.html#class-abortsignal" class="type"><AbortSignal></a> Allows closing the interface using an AbortSignal. 12201cb0ef41Sopenharmony_ciAborting the signal will internally call <code>close</code> on the interface.</li> 12211cb0ef41Sopenharmony_ci</ul> 12221cb0ef41Sopenharmony_ci</li> 12231cb0ef41Sopenharmony_ci<li>Returns: <a href="readline.html#class-readlineinterface" class="type"><readline.Interface></a></li> 12241cb0ef41Sopenharmony_ci</ul> 12251cb0ef41Sopenharmony_ci<p>The <code>readline.createInterface()</code> method creates a new <code>readline.Interface</code> 12261cb0ef41Sopenharmony_ciinstance.</p> 12271cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">const</span> readline = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:readline'</span>); 12281cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> rl = readline.<span class="hljs-title function_">createInterface</span>({ 12291cb0ef41Sopenharmony_ci <span class="hljs-attr">input</span>: process.<span class="hljs-property">stdin</span>, 12301cb0ef41Sopenharmony_ci <span class="hljs-attr">output</span>: process.<span class="hljs-property">stdout</span>, 12311cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre> 12321cb0ef41Sopenharmony_ci<p>Once the <code>readline.Interface</code> instance is created, the most common case is to 12331cb0ef41Sopenharmony_cilisten for the <code>'line'</code> event:</p> 12341cb0ef41Sopenharmony_ci<pre><code class="language-js">rl.<span class="hljs-title function_">on</span>(<span class="hljs-string">'line'</span>, <span class="hljs-function">(<span class="hljs-params">line</span>) =></span> { 12351cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`Received: <span class="hljs-subst">${line}</span>`</span>); 12361cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre> 12371cb0ef41Sopenharmony_ci<p>If <code>terminal</code> is <code>true</code> for this instance then the <code>output</code> stream will get 12381cb0ef41Sopenharmony_cithe best compatibility if it defines an <code>output.columns</code> property and emits 12391cb0ef41Sopenharmony_cia <code>'resize'</code> event on the <code>output</code> if or when the columns ever change 12401cb0ef41Sopenharmony_ci(<a href="process.html#processstdout"><code>process.stdout</code></a> does this automatically when it is a TTY).</p> 12411cb0ef41Sopenharmony_ci<p>When creating a <code>readline.Interface</code> using <code>stdin</code> as input, the program 12421cb0ef41Sopenharmony_ciwill not terminate until it receives an <a href="https://en.wikipedia.org/wiki/End-of-file#EOF_character">EOF character</a>. To exit without 12431cb0ef41Sopenharmony_ciwaiting for user input, call <code>process.stdin.unref()</code>.</p> 12441cb0ef41Sopenharmony_ci<h5>Use of the <code>completer</code> function<span><a class="mark" href="#use-of-the-completer-function_1" id="use-of-the-completer-function_1">#</a></span><a aria-hidden="true" class="legacy" id="readline_use_of_the_completer_function_1"></a></h5> 12451cb0ef41Sopenharmony_ci<p>The <code>completer</code> function takes the current line entered by the user 12461cb0ef41Sopenharmony_cias an argument, and returns an <code>Array</code> with 2 entries:</p> 12471cb0ef41Sopenharmony_ci<ul> 12481cb0ef41Sopenharmony_ci<li>An <code>Array</code> with matching entries for the completion.</li> 12491cb0ef41Sopenharmony_ci<li>The substring that was used for the matching.</li> 12501cb0ef41Sopenharmony_ci</ul> 12511cb0ef41Sopenharmony_ci<p>For instance: <code>[[substr1, substr2, ...], originalsubstring]</code>.</p> 12521cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">function</span> <span class="hljs-title function_">completer</span>(<span class="hljs-params">line</span>) { 12531cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> completions = <span class="hljs-string">'.help .error .exit .quit .q'</span>.<span class="hljs-title function_">split</span>(<span class="hljs-string">' '</span>); 12541cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> hits = completions.<span class="hljs-title function_">filter</span>(<span class="hljs-function">(<span class="hljs-params">c</span>) =></span> c.<span class="hljs-title function_">startsWith</span>(line)); 12551cb0ef41Sopenharmony_ci <span class="hljs-comment">// Show all completions if none found</span> 12561cb0ef41Sopenharmony_ci <span class="hljs-keyword">return</span> [hits.<span class="hljs-property">length</span> ? hits : completions, line]; 12571cb0ef41Sopenharmony_ci}</code> <button class="copy-button">copy</button></pre> 12581cb0ef41Sopenharmony_ci<p>The <code>completer</code> function can be called asynchronously if it accepts two 12591cb0ef41Sopenharmony_ciarguments:</p> 12601cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">function</span> <span class="hljs-title function_">completer</span>(<span class="hljs-params">linePartial, callback</span>) { 12611cb0ef41Sopenharmony_ci <span class="hljs-title function_">callback</span>(<span class="hljs-literal">null</span>, [[<span class="hljs-string">'123'</span>], linePartial]); 12621cb0ef41Sopenharmony_ci}</code> <button class="copy-button">copy</button></pre> 12631cb0ef41Sopenharmony_ci<h4><code>readline.cursorTo(stream, x[, y][, callback])</code><span><a class="mark" href="#readlinecursortostream-x-y-callback" id="readlinecursortostream-x-y-callback">#</a></span><a aria-hidden="true" class="legacy" id="readline_readline_cursorto_stream_x_y_callback"></a></h4> 12641cb0ef41Sopenharmony_ci<div class="api_metadata"> 12651cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 12661cb0ef41Sopenharmony_ci<table> 12671cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 12681cb0ef41Sopenharmony_ci<tr><td>v18.0.0</td> 12691cb0ef41Sopenharmony_ci<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr> 12701cb0ef41Sopenharmony_ci<tr><td>v12.7.0</td> 12711cb0ef41Sopenharmony_ci<td><p>The stream's write() callback and return value are exposed.</p></td></tr> 12721cb0ef41Sopenharmony_ci<tr><td>v0.7.7</td> 12731cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.7.7</span></p></td></tr> 12741cb0ef41Sopenharmony_ci</tbody></table> 12751cb0ef41Sopenharmony_ci</details> 12761cb0ef41Sopenharmony_ci</div> 12771cb0ef41Sopenharmony_ci<ul> 12781cb0ef41Sopenharmony_ci<li><code>stream</code> <a href="stream.html#class-streamwritable" class="type"><stream.Writable></a></li> 12791cb0ef41Sopenharmony_ci<li><code>x</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 12801cb0ef41Sopenharmony_ci<li><code>y</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 12811cb0ef41Sopenharmony_ci<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> Invoked once the operation completes.</li> 12821cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <code>false</code> if <code>stream</code> wishes for the calling code to wait for 12831cb0ef41Sopenharmony_cithe <code>'drain'</code> event to be emitted before continuing to write additional data; 12841cb0ef41Sopenharmony_ciotherwise <code>true</code>.</li> 12851cb0ef41Sopenharmony_ci</ul> 12861cb0ef41Sopenharmony_ci<p>The <code>readline.cursorTo()</code> method moves cursor to the specified position in a 12871cb0ef41Sopenharmony_cigiven <a href="tty.html">TTY</a> <code>stream</code>.</p> 12881cb0ef41Sopenharmony_ci<h4><code>readline.moveCursor(stream, dx, dy[, callback])</code><span><a class="mark" href="#readlinemovecursorstream-dx-dy-callback" id="readlinemovecursorstream-dx-dy-callback">#</a></span><a aria-hidden="true" class="legacy" id="readline_readline_movecursor_stream_dx_dy_callback"></a></h4> 12891cb0ef41Sopenharmony_ci<div class="api_metadata"> 12901cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary> 12911cb0ef41Sopenharmony_ci<table> 12921cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr> 12931cb0ef41Sopenharmony_ci<tr><td>v18.0.0</td> 12941cb0ef41Sopenharmony_ci<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr> 12951cb0ef41Sopenharmony_ci<tr><td>v12.7.0</td> 12961cb0ef41Sopenharmony_ci<td><p>The stream's write() callback and return value are exposed.</p></td></tr> 12971cb0ef41Sopenharmony_ci<tr><td>v0.7.7</td> 12981cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.7.7</span></p></td></tr> 12991cb0ef41Sopenharmony_ci</tbody></table> 13001cb0ef41Sopenharmony_ci</details> 13011cb0ef41Sopenharmony_ci</div> 13021cb0ef41Sopenharmony_ci<ul> 13031cb0ef41Sopenharmony_ci<li><code>stream</code> <a href="stream.html#class-streamwritable" class="type"><stream.Writable></a></li> 13041cb0ef41Sopenharmony_ci<li><code>dx</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 13051cb0ef41Sopenharmony_ci<li><code>dy</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li> 13061cb0ef41Sopenharmony_ci<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> Invoked once the operation completes.</li> 13071cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> <code>false</code> if <code>stream</code> wishes for the calling code to wait for 13081cb0ef41Sopenharmony_cithe <code>'drain'</code> event to be emitted before continuing to write additional data; 13091cb0ef41Sopenharmony_ciotherwise <code>true</code>.</li> 13101cb0ef41Sopenharmony_ci</ul> 13111cb0ef41Sopenharmony_ci<p>The <code>readline.moveCursor()</code> method moves the cursor <em>relative</em> to its current 13121cb0ef41Sopenharmony_ciposition in a given <a href="tty.html">TTY</a> <code>stream</code>.</p> 13131cb0ef41Sopenharmony_ci</section><section><h3><code>readline.emitKeypressEvents(stream[, interface])</code><span><a class="mark" href="#readlineemitkeypresseventsstream-interface" id="readlineemitkeypresseventsstream-interface">#</a></span><a aria-hidden="true" class="legacy" id="readline_readline_emitkeypressevents_stream_interface"></a></h3> 13141cb0ef41Sopenharmony_ci<div class="api_metadata"> 13151cb0ef41Sopenharmony_ci<span>Added in: v0.7.7</span> 13161cb0ef41Sopenharmony_ci</div> 13171cb0ef41Sopenharmony_ci<ul> 13181cb0ef41Sopenharmony_ci<li><code>stream</code> <a href="stream.html#class-streamreadable" class="type"><stream.Readable></a></li> 13191cb0ef41Sopenharmony_ci<li><code>interface</code> <a href="readline.html#class-interfaceconstructor" class="type"><readline.InterfaceConstructor></a></li> 13201cb0ef41Sopenharmony_ci</ul> 13211cb0ef41Sopenharmony_ci<p>The <code>readline.emitKeypressEvents()</code> method causes the given <a href="stream.html#readable-streams">Readable</a> 13221cb0ef41Sopenharmony_cistream to begin emitting <code>'keypress'</code> events corresponding to received input.</p> 13231cb0ef41Sopenharmony_ci<p>Optionally, <code>interface</code> specifies a <code>readline.Interface</code> instance for which 13241cb0ef41Sopenharmony_ciautocompletion is disabled when copy-pasted input is detected.</p> 13251cb0ef41Sopenharmony_ci<p>If the <code>stream</code> is a <a href="tty.html">TTY</a>, then it must be in raw mode.</p> 13261cb0ef41Sopenharmony_ci<p>This is automatically called by any readline instance on its <code>input</code> if the 13271cb0ef41Sopenharmony_ci<code>input</code> is a terminal. Closing the <code>readline</code> instance does not stop 13281cb0ef41Sopenharmony_cithe <code>input</code> from emitting <code>'keypress'</code> events.</p> 13291cb0ef41Sopenharmony_ci<pre><code class="language-js">readline.<span class="hljs-title function_">emitKeypressEvents</span>(process.<span class="hljs-property">stdin</span>); 13301cb0ef41Sopenharmony_ci<span class="hljs-keyword">if</span> (process.<span class="hljs-property">stdin</span>.<span class="hljs-property">isTTY</span>) 13311cb0ef41Sopenharmony_ci process.<span class="hljs-property">stdin</span>.<span class="hljs-title function_">setRawMode</span>(<span class="hljs-literal">true</span>);</code> <button class="copy-button">copy</button></pre> 13321cb0ef41Sopenharmony_ci</section><section><h3>Example: Tiny CLI<span><a class="mark" href="#example-tiny-cli" id="example-tiny-cli">#</a></span><a aria-hidden="true" class="legacy" id="readline_example_tiny_cli"></a></h3> 13331cb0ef41Sopenharmony_ci<p>The following example illustrates the use of <code>readline.Interface</code> class to 13341cb0ef41Sopenharmony_ciimplement a small command-line interface:</p> 13351cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">const</span> readline = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:readline'</span>); 13361cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> rl = readline.<span class="hljs-title function_">createInterface</span>({ 13371cb0ef41Sopenharmony_ci <span class="hljs-attr">input</span>: process.<span class="hljs-property">stdin</span>, 13381cb0ef41Sopenharmony_ci <span class="hljs-attr">output</span>: process.<span class="hljs-property">stdout</span>, 13391cb0ef41Sopenharmony_ci <span class="hljs-attr">prompt</span>: <span class="hljs-string">'OHAI> '</span>, 13401cb0ef41Sopenharmony_ci}); 13411cb0ef41Sopenharmony_ci 13421cb0ef41Sopenharmony_cirl.<span class="hljs-title function_">prompt</span>(); 13431cb0ef41Sopenharmony_ci 13441cb0ef41Sopenharmony_cirl.<span class="hljs-title function_">on</span>(<span class="hljs-string">'line'</span>, <span class="hljs-function">(<span class="hljs-params">line</span>) =></span> { 13451cb0ef41Sopenharmony_ci <span class="hljs-keyword">switch</span> (line.<span class="hljs-title function_">trim</span>()) { 13461cb0ef41Sopenharmony_ci <span class="hljs-keyword">case</span> <span class="hljs-string">'hello'</span>: 13471cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'world!'</span>); 13481cb0ef41Sopenharmony_ci <span class="hljs-keyword">break</span>; 13491cb0ef41Sopenharmony_ci <span class="hljs-attr">default</span>: 13501cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`Say what? I might have heard '<span class="hljs-subst">${line.trim()}</span>'`</span>); 13511cb0ef41Sopenharmony_ci <span class="hljs-keyword">break</span>; 13521cb0ef41Sopenharmony_ci } 13531cb0ef41Sopenharmony_ci rl.<span class="hljs-title function_">prompt</span>(); 13541cb0ef41Sopenharmony_ci}).<span class="hljs-title function_">on</span>(<span class="hljs-string">'close'</span>, <span class="hljs-function">() =></span> { 13551cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'Have a great day!'</span>); 13561cb0ef41Sopenharmony_ci process.<span class="hljs-title function_">exit</span>(<span class="hljs-number">0</span>); 13571cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre> 13581cb0ef41Sopenharmony_ci</section><section><h3>Example: Read file stream line-by-Line<span><a class="mark" href="#example-read-file-stream-line-by-line" id="example-read-file-stream-line-by-line">#</a></span><a aria-hidden="true" class="legacy" id="readline_example_read_file_stream_line_by_line"></a></h3> 13591cb0ef41Sopenharmony_ci<p>A common use case for <code>readline</code> is to consume an input file one line at a 13601cb0ef41Sopenharmony_citime. The easiest way to do so is leveraging the <a href="fs.html#class-fsreadstream"><code>fs.ReadStream</code></a> API as 13611cb0ef41Sopenharmony_ciwell as a <code>for await...of</code> loop:</p> 13621cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:fs'</span>); 13631cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> readline = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:readline'</span>); 13641cb0ef41Sopenharmony_ci 13651cb0ef41Sopenharmony_ci<span class="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-title function_">processLineByLine</span>(<span class="hljs-params"></span>) { 13661cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> fileStream = fs.<span class="hljs-title function_">createReadStream</span>(<span class="hljs-string">'input.txt'</span>); 13671cb0ef41Sopenharmony_ci 13681cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> rl = readline.<span class="hljs-title function_">createInterface</span>({ 13691cb0ef41Sopenharmony_ci <span class="hljs-attr">input</span>: fileStream, 13701cb0ef41Sopenharmony_ci <span class="hljs-attr">crlfDelay</span>: <span class="hljs-title class_">Infinity</span>, 13711cb0ef41Sopenharmony_ci }); 13721cb0ef41Sopenharmony_ci <span class="hljs-comment">// Note: we use the crlfDelay option to recognize all instances of CR LF</span> 13731cb0ef41Sopenharmony_ci <span class="hljs-comment">// ('\r\n') in input.txt as a single line break.</span> 13741cb0ef41Sopenharmony_ci 13751cb0ef41Sopenharmony_ci <span class="hljs-keyword">for</span> <span class="hljs-keyword">await</span> (<span class="hljs-keyword">const</span> line <span class="hljs-keyword">of</span> rl) { 13761cb0ef41Sopenharmony_ci <span class="hljs-comment">// Each line in input.txt will be successively available here as `line`.</span> 13771cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`Line from file: <span class="hljs-subst">${line}</span>`</span>); 13781cb0ef41Sopenharmony_ci } 13791cb0ef41Sopenharmony_ci} 13801cb0ef41Sopenharmony_ci 13811cb0ef41Sopenharmony_ci<span class="hljs-title function_">processLineByLine</span>();</code> <button class="copy-button">copy</button></pre> 13821cb0ef41Sopenharmony_ci<p>Alternatively, one could use the <a href="#event-line"><code>'line'</code></a> event:</p> 13831cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:fs'</span>); 13841cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> readline = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:readline'</span>); 13851cb0ef41Sopenharmony_ci 13861cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> rl = readline.<span class="hljs-title function_">createInterface</span>({ 13871cb0ef41Sopenharmony_ci <span class="hljs-attr">input</span>: fs.<span class="hljs-title function_">createReadStream</span>(<span class="hljs-string">'sample.txt'</span>), 13881cb0ef41Sopenharmony_ci <span class="hljs-attr">crlfDelay</span>: <span class="hljs-title class_">Infinity</span>, 13891cb0ef41Sopenharmony_ci}); 13901cb0ef41Sopenharmony_ci 13911cb0ef41Sopenharmony_cirl.<span class="hljs-title function_">on</span>(<span class="hljs-string">'line'</span>, <span class="hljs-function">(<span class="hljs-params">line</span>) =></span> { 13921cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`Line from file: <span class="hljs-subst">${line}</span>`</span>); 13931cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre> 13941cb0ef41Sopenharmony_ci<p>Currently, <code>for await...of</code> loop can be a bit slower. If <code>async</code> / <code>await</code> 13951cb0ef41Sopenharmony_ciflow and speed are both essential, a mixed approach can be applied:</p> 13961cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">const</span> { once } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:events'</span>); 13971cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> { createReadStream } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:fs'</span>); 13981cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> { createInterface } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:readline'</span>); 13991cb0ef41Sopenharmony_ci 14001cb0ef41Sopenharmony_ci(<span class="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-title function_">processLineByLine</span>(<span class="hljs-params"></span>) { 14011cb0ef41Sopenharmony_ci <span class="hljs-keyword">try</span> { 14021cb0ef41Sopenharmony_ci <span class="hljs-keyword">const</span> rl = <span class="hljs-title function_">createInterface</span>({ 14031cb0ef41Sopenharmony_ci <span class="hljs-attr">input</span>: <span class="hljs-title function_">createReadStream</span>(<span class="hljs-string">'big-file.txt'</span>), 14041cb0ef41Sopenharmony_ci <span class="hljs-attr">crlfDelay</span>: <span class="hljs-title class_">Infinity</span>, 14051cb0ef41Sopenharmony_ci }); 14061cb0ef41Sopenharmony_ci 14071cb0ef41Sopenharmony_ci rl.<span class="hljs-title function_">on</span>(<span class="hljs-string">'line'</span>, <span class="hljs-function">(<span class="hljs-params">line</span>) =></span> { 14081cb0ef41Sopenharmony_ci <span class="hljs-comment">// Process the line.</span> 14091cb0ef41Sopenharmony_ci }); 14101cb0ef41Sopenharmony_ci 14111cb0ef41Sopenharmony_ci <span class="hljs-keyword">await</span> <span class="hljs-title function_">once</span>(rl, <span class="hljs-string">'close'</span>); 14121cb0ef41Sopenharmony_ci 14131cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'File processed.'</span>); 14141cb0ef41Sopenharmony_ci } <span class="hljs-keyword">catch</span> (err) { 14151cb0ef41Sopenharmony_ci <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(err); 14161cb0ef41Sopenharmony_ci } 14171cb0ef41Sopenharmony_ci})();</code> <button class="copy-button">copy</button></pre> 14181cb0ef41Sopenharmony_ci</section><section><h3>TTY keybindings<span><a class="mark" href="#tty-keybindings" id="tty-keybindings">#</a></span><a aria-hidden="true" class="legacy" id="readline_tty_keybindings"></a></h3> 14191cb0ef41Sopenharmony_ci<table> 14201cb0ef41Sopenharmony_ci <tbody><tr> 14211cb0ef41Sopenharmony_ci <th>Keybindings</th> 14221cb0ef41Sopenharmony_ci <th>Description</th> 14231cb0ef41Sopenharmony_ci <th>Notes</th> 14241cb0ef41Sopenharmony_ci </tr> 14251cb0ef41Sopenharmony_ci <tr> 14261cb0ef41Sopenharmony_ci <td><kbd>Ctrl</kbd>+<kbd>Shift</kbd>+<kbd>Backspace</kbd></td> 14271cb0ef41Sopenharmony_ci <td>Delete line left</td> 14281cb0ef41Sopenharmony_ci <td>Doesn't work on Linux, Mac and Windows</td> 14291cb0ef41Sopenharmony_ci </tr> 14301cb0ef41Sopenharmony_ci <tr> 14311cb0ef41Sopenharmony_ci <td><kbd>Ctrl</kbd>+<kbd>Shift</kbd>+<kbd>Delete</kbd></td> 14321cb0ef41Sopenharmony_ci <td>Delete line right</td> 14331cb0ef41Sopenharmony_ci <td>Doesn't work on Mac</td> 14341cb0ef41Sopenharmony_ci </tr> 14351cb0ef41Sopenharmony_ci <tr> 14361cb0ef41Sopenharmony_ci <td><kbd>Ctrl</kbd>+<kbd>C</kbd></td> 14371cb0ef41Sopenharmony_ci <td>Emit <code>SIGINT</code> or close the readline instance</td> 14381cb0ef41Sopenharmony_ci <td></td> 14391cb0ef41Sopenharmony_ci </tr> 14401cb0ef41Sopenharmony_ci <tr> 14411cb0ef41Sopenharmony_ci <td><kbd>Ctrl</kbd>+<kbd>H</kbd></td> 14421cb0ef41Sopenharmony_ci <td>Delete left</td> 14431cb0ef41Sopenharmony_ci <td></td> 14441cb0ef41Sopenharmony_ci </tr> 14451cb0ef41Sopenharmony_ci <tr> 14461cb0ef41Sopenharmony_ci <td><kbd>Ctrl</kbd>+<kbd>D</kbd></td> 14471cb0ef41Sopenharmony_ci <td>Delete right or close the readline instance in case the current line is empty / EOF</td> 14481cb0ef41Sopenharmony_ci <td>Doesn't work on Windows</td> 14491cb0ef41Sopenharmony_ci </tr> 14501cb0ef41Sopenharmony_ci <tr> 14511cb0ef41Sopenharmony_ci <td><kbd>Ctrl</kbd>+<kbd>U</kbd></td> 14521cb0ef41Sopenharmony_ci <td>Delete from the current position to the line start</td> 14531cb0ef41Sopenharmony_ci <td></td> 14541cb0ef41Sopenharmony_ci </tr> 14551cb0ef41Sopenharmony_ci <tr> 14561cb0ef41Sopenharmony_ci <td><kbd>Ctrl</kbd>+<kbd>K</kbd></td> 14571cb0ef41Sopenharmony_ci <td>Delete from the current position to the end of line</td> 14581cb0ef41Sopenharmony_ci <td></td> 14591cb0ef41Sopenharmony_ci </tr> 14601cb0ef41Sopenharmony_ci <tr> 14611cb0ef41Sopenharmony_ci <td><kbd>Ctrl</kbd>+<kbd>Y</kbd></td> 14621cb0ef41Sopenharmony_ci <td>Yank (Recall) the previously deleted text</td> 14631cb0ef41Sopenharmony_ci <td>Only works with text deleted by <kbd>Ctrl</kbd>+<kbd>U</kbd> or <kbd>Ctrl</kbd>+<kbd>K</kbd></td> 14641cb0ef41Sopenharmony_ci </tr> 14651cb0ef41Sopenharmony_ci <tr> 14661cb0ef41Sopenharmony_ci <td><kbd>Meta</kbd>+<kbd>Y</kbd></td> 14671cb0ef41Sopenharmony_ci <td>Cycle among previously deleted texts</td> 14681cb0ef41Sopenharmony_ci <td>Only available when the last keystroke is <kbd>Ctrl</kbd>+<kbd>Y</kbd> or <kbd>Meta</kbd>+<kbd>Y</kbd></td> 14691cb0ef41Sopenharmony_ci </tr> 14701cb0ef41Sopenharmony_ci <tr> 14711cb0ef41Sopenharmony_ci <td><kbd>Ctrl</kbd>+<kbd>A</kbd></td> 14721cb0ef41Sopenharmony_ci <td>Go to start of line</td> 14731cb0ef41Sopenharmony_ci <td></td> 14741cb0ef41Sopenharmony_ci </tr> 14751cb0ef41Sopenharmony_ci <tr> 14761cb0ef41Sopenharmony_ci <td><kbd>Ctrl</kbd>+<kbd>E</kbd></td> 14771cb0ef41Sopenharmony_ci <td>Go to end of line</td> 14781cb0ef41Sopenharmony_ci <td></td> 14791cb0ef41Sopenharmony_ci </tr> 14801cb0ef41Sopenharmony_ci <tr> 14811cb0ef41Sopenharmony_ci <td><kbd>Ctrl</kbd>+<kbd>B</kbd></td> 14821cb0ef41Sopenharmony_ci <td>Back one character</td> 14831cb0ef41Sopenharmony_ci <td></td> 14841cb0ef41Sopenharmony_ci </tr> 14851cb0ef41Sopenharmony_ci <tr> 14861cb0ef41Sopenharmony_ci <td><kbd>Ctrl</kbd>+<kbd>F</kbd></td> 14871cb0ef41Sopenharmony_ci <td>Forward one character</td> 14881cb0ef41Sopenharmony_ci <td></td> 14891cb0ef41Sopenharmony_ci </tr> 14901cb0ef41Sopenharmony_ci <tr> 14911cb0ef41Sopenharmony_ci <td><kbd>Ctrl</kbd>+<kbd>L</kbd></td> 14921cb0ef41Sopenharmony_ci <td>Clear screen</td> 14931cb0ef41Sopenharmony_ci <td></td> 14941cb0ef41Sopenharmony_ci </tr> 14951cb0ef41Sopenharmony_ci <tr> 14961cb0ef41Sopenharmony_ci <td><kbd>Ctrl</kbd>+<kbd>N</kbd></td> 14971cb0ef41Sopenharmony_ci <td>Next history item</td> 14981cb0ef41Sopenharmony_ci <td></td> 14991cb0ef41Sopenharmony_ci </tr> 15001cb0ef41Sopenharmony_ci <tr> 15011cb0ef41Sopenharmony_ci <td><kbd>Ctrl</kbd>+<kbd>P</kbd></td> 15021cb0ef41Sopenharmony_ci <td>Previous history item</td> 15031cb0ef41Sopenharmony_ci <td></td> 15041cb0ef41Sopenharmony_ci </tr> 15051cb0ef41Sopenharmony_ci <tr> 15061cb0ef41Sopenharmony_ci <td><kbd>Ctrl</kbd>+<kbd>-</kbd></td> 15071cb0ef41Sopenharmony_ci <td>Undo previous change</td> 15081cb0ef41Sopenharmony_ci <td>Any keystroke that emits key code <code>0x1F</code> will do this action. 15091cb0ef41Sopenharmony_ci In many terminals, for example <code>xterm</code>, 15101cb0ef41Sopenharmony_ci this is bound to <kbd>Ctrl</kbd>+<kbd>-</kbd>.</td> 15111cb0ef41Sopenharmony_ci </tr> 15121cb0ef41Sopenharmony_ci <tr> 15131cb0ef41Sopenharmony_ci <td><kbd>Ctrl</kbd>+<kbd>6</kbd></td> 15141cb0ef41Sopenharmony_ci <td>Redo previous change</td> 15151cb0ef41Sopenharmony_ci <td>Many terminals don't have a default redo keystroke. 15161cb0ef41Sopenharmony_ci We choose key code <code>0x1E</code> to perform redo. 15171cb0ef41Sopenharmony_ci In <code>xterm</code>, it is bound to <kbd>Ctrl</kbd>+<kbd>6</kbd> 15181cb0ef41Sopenharmony_ci by default.</td> 15191cb0ef41Sopenharmony_ci </tr> 15201cb0ef41Sopenharmony_ci <tr> 15211cb0ef41Sopenharmony_ci <td><kbd>Ctrl</kbd>+<kbd>Z</kbd></td> 15221cb0ef41Sopenharmony_ci <td>Moves running process into background. Type 15231cb0ef41Sopenharmony_ci <code>fg</code> and press <kbd>Enter</kbd> 15241cb0ef41Sopenharmony_ci to return.</td> 15251cb0ef41Sopenharmony_ci <td>Doesn't work on Windows</td> 15261cb0ef41Sopenharmony_ci </tr> 15271cb0ef41Sopenharmony_ci <tr> 15281cb0ef41Sopenharmony_ci <td><kbd>Ctrl</kbd>+<kbd>W</kbd> or <kbd>Ctrl</kbd> 15291cb0ef41Sopenharmony_ci +<kbd>Backspace</kbd></td> 15301cb0ef41Sopenharmony_ci <td>Delete backward to a word boundary</td> 15311cb0ef41Sopenharmony_ci <td><kbd>Ctrl</kbd>+<kbd>Backspace</kbd> Doesn't 15321cb0ef41Sopenharmony_ci work on Linux, Mac and Windows</td> 15331cb0ef41Sopenharmony_ci </tr> 15341cb0ef41Sopenharmony_ci <tr> 15351cb0ef41Sopenharmony_ci <td><kbd>Ctrl</kbd>+<kbd>Delete</kbd></td> 15361cb0ef41Sopenharmony_ci <td>Delete forward to a word boundary</td> 15371cb0ef41Sopenharmony_ci <td>Doesn't work on Mac</td> 15381cb0ef41Sopenharmony_ci </tr> 15391cb0ef41Sopenharmony_ci <tr> 15401cb0ef41Sopenharmony_ci <td><kbd>Ctrl</kbd>+<kbd>Left arrow</kbd> or 15411cb0ef41Sopenharmony_ci <kbd>Meta</kbd>+<kbd>B</kbd></td> 15421cb0ef41Sopenharmony_ci <td>Word left</td> 15431cb0ef41Sopenharmony_ci <td><kbd>Ctrl</kbd>+<kbd>Left arrow</kbd> Doesn't work 15441cb0ef41Sopenharmony_ci on Mac</td> 15451cb0ef41Sopenharmony_ci </tr> 15461cb0ef41Sopenharmony_ci <tr> 15471cb0ef41Sopenharmony_ci <td><kbd>Ctrl</kbd>+<kbd>Right arrow</kbd> or 15481cb0ef41Sopenharmony_ci <kbd>Meta</kbd>+<kbd>F</kbd></td> 15491cb0ef41Sopenharmony_ci <td>Word right</td> 15501cb0ef41Sopenharmony_ci <td><kbd>Ctrl</kbd>+<kbd>Right arrow</kbd> Doesn't work 15511cb0ef41Sopenharmony_ci on Mac</td> 15521cb0ef41Sopenharmony_ci </tr> 15531cb0ef41Sopenharmony_ci <tr> 15541cb0ef41Sopenharmony_ci <td><kbd>Meta</kbd>+<kbd>D</kbd> or <kbd>Meta</kbd> 15551cb0ef41Sopenharmony_ci +<kbd>Delete</kbd></td> 15561cb0ef41Sopenharmony_ci <td>Delete word right</td> 15571cb0ef41Sopenharmony_ci <td><kbd>Meta</kbd>+<kbd>Delete</kbd> Doesn't work 15581cb0ef41Sopenharmony_ci on windows</td> 15591cb0ef41Sopenharmony_ci </tr> 15601cb0ef41Sopenharmony_ci <tr> 15611cb0ef41Sopenharmony_ci <td><kbd>Meta</kbd>+<kbd>Backspace</kbd></td> 15621cb0ef41Sopenharmony_ci <td>Delete word left</td> 15631cb0ef41Sopenharmony_ci <td>Doesn't work on Mac</td> 15641cb0ef41Sopenharmony_ci </tr> 15651cb0ef41Sopenharmony_ci</tbody></table></section> 15661cb0ef41Sopenharmony_ci <!-- API END --> 15671cb0ef41Sopenharmony_ci </div> 15681cb0ef41Sopenharmony_ci </div> 15691cb0ef41Sopenharmony_ci </div> 15701cb0ef41Sopenharmony_ci</body> 15711cb0ef41Sopenharmony_ci</html> 1572