• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<!DOCTYPE html>
2<html lang="en">
3<head>
4  <meta charset="utf-8">
5  <meta name="viewport" content="width=device-width">
6  <meta name="nodejs.org:node-version" content="v14.21.2">
7  <title>Readline | Node.js v14.21.2 Documentation</title>
8  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:400,700,400italic&display=fallback">
9  <link rel="stylesheet" href="assets/style.css">
10  <link rel="stylesheet" href="assets/hljs.css">
11  <link rel="canonical" href="https://nodejs.org/api/readline.html">
12</head>
13<body class="alt apidoc" id="api-section-readline">
14  <div id="content" class="clearfix">
15    <div id="column2" class="interior">
16      <div id="intro" class="interior">
17        <a href="/" title="Go back to the home page">
18          Node.js
19        </a>
20      </div>
21      <ul>
22<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li>
23<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li>
24</ul>
25<hr class="line">
26<ul>
27<li><a href="assert.html" class="nav-assert">Assertion testing</a></li>
28<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li>
29<li><a href="buffer.html" class="nav-buffer">Buffer</a></li>
30<li><a href="addons.html" class="nav-addons">C++ addons</a></li>
31<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li>
32<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li>
33<li><a href="child_process.html" class="nav-child_process">Child processes</a></li>
34<li><a href="cluster.html" class="nav-cluster">Cluster</a></li>
35<li><a href="cli.html" class="nav-cli">Command-line options</a></li>
36<li><a href="console.html" class="nav-console">Console</a></li>
37<li><a href="corepack.html" class="nav-corepack">Corepack</a></li>
38<li><a href="crypto.html" class="nav-crypto">Crypto</a></li>
39<li><a href="debugger.html" class="nav-debugger">Debugger</a></li>
40<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li>
41<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li>
42<li><a href="dns.html" class="nav-dns">DNS</a></li>
43<li><a href="domain.html" class="nav-domain">Domain</a></li>
44<li><a href="errors.html" class="nav-errors">Errors</a></li>
45<li><a href="events.html" class="nav-events">Events</a></li>
46<li><a href="fs.html" class="nav-fs">File system</a></li>
47<li><a href="globals.html" class="nav-globals">Globals</a></li>
48<li><a href="http.html" class="nav-http">HTTP</a></li>
49<li><a href="http2.html" class="nav-http2">HTTP/2</a></li>
50<li><a href="https.html" class="nav-https">HTTPS</a></li>
51<li><a href="inspector.html" class="nav-inspector">Inspector</a></li>
52<li><a href="intl.html" class="nav-intl">Internationalization</a></li>
53<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li>
54<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li>
55<li><a href="module.html" class="nav-module">Modules: <code>module</code> API</a></li>
56<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li>
57<li><a href="net.html" class="nav-net">Net</a></li>
58<li><a href="os.html" class="nav-os">OS</a></li>
59<li><a href="path.html" class="nav-path">Path</a></li>
60<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li>
61<li><a href="policy.html" class="nav-policy">Policies</a></li>
62<li><a href="process.html" class="nav-process">Process</a></li>
63<li><a href="punycode.html" class="nav-punycode">Punycode</a></li>
64<li><a href="querystring.html" class="nav-querystring">Query strings</a></li>
65<li><a href="readline.html" class="nav-readline active">Readline</a></li>
66<li><a href="repl.html" class="nav-repl">REPL</a></li>
67<li><a href="report.html" class="nav-report">Report</a></li>
68<li><a href="stream.html" class="nav-stream">Stream</a></li>
69<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li>
70<li><a href="timers.html" class="nav-timers">Timers</a></li>
71<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li>
72<li><a href="tracing.html" class="nav-tracing">Trace events</a></li>
73<li><a href="tty.html" class="nav-tty">TTY</a></li>
74<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li>
75<li><a href="url.html" class="nav-url">URL</a></li>
76<li><a href="util.html" class="nav-util">Utilities</a></li>
77<li><a href="v8.html" class="nav-v8">V8</a></li>
78<li><a href="vm.html" class="nav-vm">VM</a></li>
79<li><a href="wasi.html" class="nav-wasi">WASI</a></li>
80<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li>
81<li><a href="zlib.html" class="nav-zlib">Zlib</a></li>
82</ul>
83<hr class="line">
84<ul>
85<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li>
86</ul>
87    </div>
88
89    <div id="column1" data-id="readline" class="interior">
90      <header>
91        <div class="header-container">
92          <h1>Node.js v14.21.2 documentation</h1>
93          <button class="theme-toggle-btn" id="theme-toggle-btn" title="Toggle dark mode/light mode" aria-label="Toggle dark mode/light mode" hidden>
94            <svg xmlns="http://www.w3.org/2000/svg" class="icon dark-icon" height="24" width="24">
95              <path fill="none" d="M0 0h24v24H0z" />
96              <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"/>
97              <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"/>
98            </svg>
99            <svg xmlns="http://www.w3.org/2000/svg" class="icon light-icon" height="24" width="24">
100              <path d="M0 0h24v24H0z" fill="none" />
101              <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"/>
102            </svg>
103          </button>
104        </div>
105        <div id="gtoc">
106          <ul>
107            <li>
108              <a href="index.html">Index</a>
109            </li>
110            <li>
111              <a href="all.html">View on single page</a>
112            </li>
113            <li>
114              <a href="readline.json">View as JSON</a>
115            </li>
116
117    <li class="version-picker">
118      <a href="#">View another version <span>&#x25bc;</span></a>
119      <ol class="version-picker"><li><a href="https://nodejs.org/docs/latest-v19.x/api/readline.html">19.x</a></li>
120<li><a href="https://nodejs.org/docs/latest-v18.x/api/readline.html">18.x <b>LTS</b></a></li>
121<li><a href="https://nodejs.org/docs/latest-v17.x/api/readline.html">17.x</a></li>
122<li><a href="https://nodejs.org/docs/latest-v16.x/api/readline.html">16.x <b>LTS</b></a></li>
123<li><a href="https://nodejs.org/docs/latest-v15.x/api/readline.html">15.x</a></li>
124<li><a href="https://nodejs.org/docs/latest-v14.x/api/readline.html">14.x <b>LTS</b></a></li>
125<li><a href="https://nodejs.org/docs/latest-v13.x/api/readline.html">13.x</a></li>
126<li><a href="https://nodejs.org/docs/latest-v12.x/api/readline.html">12.x</a></li>
127<li><a href="https://nodejs.org/docs/latest-v11.x/api/readline.html">11.x</a></li>
128<li><a href="https://nodejs.org/docs/latest-v10.x/api/readline.html">10.x</a></li>
129<li><a href="https://nodejs.org/docs/latest-v9.x/api/readline.html">9.x</a></li>
130<li><a href="https://nodejs.org/docs/latest-v8.x/api/readline.html">8.x</a></li>
131<li><a href="https://nodejs.org/docs/latest-v7.x/api/readline.html">7.x</a></li>
132<li><a href="https://nodejs.org/docs/latest-v6.x/api/readline.html">6.x</a></li>
133<li><a href="https://nodejs.org/docs/latest-v5.x/api/readline.html">5.x</a></li>
134<li><a href="https://nodejs.org/docs/latest-v4.x/api/readline.html">4.x</a></li>
135<li><a href="https://nodejs.org/docs/latest-v0.12.x/api/readline.html">0.12.x</a></li>
136<li><a href="https://nodejs.org/docs/latest-v0.10.x/api/readline.html">0.10.x</a></li></ol>
137    </li>
138
139            <li class="edit_on_github"><a href="https://github.com/nodejs/node/edit/master/doc/api/readline.md">Edit on GitHub</a></li>
140          </ul>
141        </div>
142        <hr>
143      </header>
144
145      <details id="toc" open><summary>Table of contents</summary><ul>
146<li><span class="stability_2"><a href="#readline_readline">Readline</a></span>
147<ul>
148<li><a href="#readline_class_interface">Class: <code>Interface</code></a>
149<ul>
150<li><a href="#readline_event_close">Event: <code>'close'</code></a></li>
151<li><a href="#readline_event_line">Event: <code>'line'</code></a></li>
152<li><a href="#readline_event_history">Event: <code>'history'</code></a></li>
153<li><a href="#readline_event_pause">Event: <code>'pause'</code></a></li>
154<li><a href="#readline_event_resume">Event: <code>'resume'</code></a></li>
155<li><a href="#readline_event_sigcont">Event: <code>'SIGCONT'</code></a></li>
156<li><a href="#readline_event_sigint">Event: <code>'SIGINT'</code></a></li>
157<li><a href="#readline_event_sigtstp">Event: <code>'SIGTSTP'</code></a></li>
158<li><a href="#readline_rl_close"><code>rl.close()</code></a></li>
159<li><a href="#readline_rl_pause"><code>rl.pause()</code></a></li>
160<li><a href="#readline_rl_prompt_preservecursor"><code>rl.prompt([preserveCursor])</code></a></li>
161<li><a href="#readline_rl_question_query_options_callback"><code>rl.question(query[, options], callback)</code></a></li>
162<li><a href="#readline_rl_resume"><code>rl.resume()</code></a></li>
163<li><a href="#readline_rl_setprompt_prompt"><code>rl.setPrompt(prompt)</code></a></li>
164<li><a href="#readline_rl_getprompt"><code>rl.getPrompt()</code></a></li>
165<li><a href="#readline_rl_write_data_key"><code>rl.write(data[, key])</code></a></li>
166<li><a href="#readline_rl_symbol_asynciterator"><code>rl[Symbol.asyncIterator]()</code></a></li>
167<li><a href="#readline_rl_line"><code>rl.line</code></a></li>
168<li><a href="#readline_rl_cursor"><code>rl.cursor</code></a></li>
169<li><a href="#readline_rl_getcursorpos"><code>rl.getCursorPos()</code></a></li>
170</ul>
171</li>
172<li><a href="#readline_readline_clearline_stream_dir_callback"><code>readline.clearLine(stream, dir[, callback])</code></a></li>
173<li><a href="#readline_readline_clearscreendown_stream_callback"><code>readline.clearScreenDown(stream[, callback])</code></a></li>
174<li><a href="#readline_readline_createinterface_options"><code>readline.createInterface(options)</code></a>
175<ul>
176<li><a href="#readline_use_of_the_completer_function">Use of the <code>completer</code> function</a></li>
177</ul>
178</li>
179<li><a href="#readline_readline_cursorto_stream_x_y_callback"><code>readline.cursorTo(stream, x[, y][, callback])</code></a></li>
180<li><a href="#readline_readline_emitkeypressevents_stream_interface"><code>readline.emitKeypressEvents(stream[, interface])</code></a></li>
181<li><a href="#readline_readline_movecursor_stream_dx_dy_callback"><code>readline.moveCursor(stream, dx, dy[, callback])</code></a></li>
182<li><a href="#readline_example_tiny_cli">Example: Tiny CLI</a></li>
183<li><a href="#readline_example_read_file_stream_line_by_line">Example: Read file stream line-by-Line</a></li>
184<li><a href="#readline_tty_keybindings">TTY keybindings</a></li>
185</ul>
186</li>
187</ul></details>
188
189      <div id="apicontent">
190        <h2>Readline<span><a class="mark" href="#readline_readline" id="readline_readline">#</a></span></h2>
191
192<p></p><div class="api_stability api_stability_2"><a href="documentation.html#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
193<p><strong>Source Code:</strong> <a href="https://github.com/nodejs/node/blob/v14.21.2/lib/readline.js">lib/readline.js</a></p>
194<p>The <code>readline</code> module provides an interface for reading data from a <a href="stream.html#stream_readable_streams">Readable</a>
195stream (such as <a href="process.html#process_process_stdin"><code>process.stdin</code></a>) one line at a time. It can be accessed
196using:</p>
197<pre><code class="language-js"><span class="hljs-keyword">const</span> readline = <span class="hljs-built_in">require</span>(<span class="hljs-string">'readline'</span>);</code></pre>
198<p>The following simple example illustrates the basic use of the <code>readline</code> module.</p>
199<pre><code class="language-js"><span class="hljs-keyword">const</span> readline = <span class="hljs-built_in">require</span>(<span class="hljs-string">'readline'</span>);
200
201<span class="hljs-keyword">const</span> rl = readline.<span class="hljs-title function_">createInterface</span>({
202  <span class="hljs-attr">input</span>: process.<span class="hljs-property">stdin</span>,
203  <span class="hljs-attr">output</span>: process.<span class="hljs-property">stdout</span>
204});
205
206rl.<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> {
207  <span class="hljs-comment">// <span class="hljs-doctag">TODO:</span> Log the answer in a database</span>
208  <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>);
209
210  rl.<span class="hljs-title function_">close</span>();
211});</code></pre>
212<p>Once this code is invoked, the Node.js application will not terminate until the
213<code>readline.Interface</code> is closed because the interface waits for data to be
214received on the <code>input</code> stream.</p>
215<section><h3>Class: <code>Interface</code><span><a class="mark" href="#readline_class_interface" id="readline_class_interface">#</a></span></h3>
216<div class="api_metadata">
217<span>Added in: v0.1.104</span>
218</div>
219<ul>
220<li>Extends: <a href="events.html#events_class_eventemitter" class="type">&#x3C;EventEmitter></a></li>
221</ul>
222<p>Instances of the <code>readline.Interface</code> class are constructed using the
223<code>readline.createInterface()</code> method. Every instance is associated with a
224single <code>input</code> <a href="stream.html#stream_readable_streams">Readable</a> stream and a single <code>output</code> <a href="stream.html#stream_writable_streams">Writable</a> stream.
225The <code>output</code> stream is used to print prompts for user input that arrives on,
226and is read from, the <code>input</code> stream.</p>
227<h4>Event: <code>'close'</code><span><a class="mark" href="#readline_event_close" id="readline_event_close">#</a></span></h4>
228<div class="api_metadata">
229<span>Added in: v0.1.98</span>
230</div>
231<p>The <code>'close'</code> event is emitted when one of the following occur:</p>
232<ul>
233<li>The <code>rl.close()</code> method is called and the <code>readline.Interface</code> instance has
234relinquished control over the <code>input</code> and <code>output</code> streams;</li>
235<li>The <code>input</code> stream receives its <code>'end'</code> event;</li>
236<li>The <code>input</code> stream receives <kbd>Ctrl</kbd>+<kbd>D</kbd> to signal
237end-of-transmission (EOT);</li>
238<li>The <code>input</code> stream receives <kbd>Ctrl</kbd>+<kbd>C</kbd> to signal <code>SIGINT</code>
239and there is no <code>'SIGINT'</code> event listener registered on the
240<code>readline.Interface</code> instance.</li>
241</ul>
242<p>The listener function is called without passing any arguments.</p>
243<p>The <code>readline.Interface</code> instance is finished once the <code>'close'</code> event is
244emitted.</p>
245<h4>Event: <code>'line'</code><span><a class="mark" href="#readline_event_line" id="readline_event_line">#</a></span></h4>
246<div class="api_metadata">
247<span>Added in: v0.1.98</span>
248</div>
249<p>The <code>'line'</code> event is emitted whenever the <code>input</code> stream receives an
250end-of-line input (<code>\n</code>, <code>\r</code>, or <code>\r\n</code>). This usually occurs when the user
251presses <kbd>Enter</kbd> or <kbd>Return</kbd>.</p>
252<p>The listener function is called with a string containing the single line of
253received input.</p>
254<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> {
255  <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>);
256});</code></pre>
257<h4>Event: <code>'history'</code><span><a class="mark" href="#readline_event_history" id="readline_event_history">#</a></span></h4>
258<div class="api_metadata">
259<span>Added in: v14.18.0</span>
260</div>
261<p>The <code>'history'</code> event is emitted whenever the history array has changed.</p>
262<p>The listener function is called with an array containing the history array.
263It will reflect all changes, added lines and removed lines due to
264<code>historySize</code> and <code>removeHistoryDuplicates</code>.</p>
265<p>The primary purpose is to allow a listener to persist the history.
266It is also possible for the listener to change the history object. This
267could be useful to prevent certain lines to be added to the history, like
268a password.</p>
269<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> {
270  <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>);
271});</code></pre>
272<h4>Event: <code>'pause'</code><span><a class="mark" href="#readline_event_pause" id="readline_event_pause">#</a></span></h4>
273<div class="api_metadata">
274<span>Added in: v0.7.5</span>
275</div>
276<p>The <code>'pause'</code> event is emitted when one of the following occur:</p>
277<ul>
278<li>The <code>input</code> stream is paused.</li>
279<li>The <code>input</code> stream is not paused and receives the <code>'SIGCONT'</code> event. (See
280events <a href="#readline_event_sigtstp"><code>'SIGTSTP'</code></a> and <a href="#readline_event_sigcont"><code>'SIGCONT'</code></a>.)</li>
281</ul>
282<p>The listener function is called without passing any arguments.</p>
283<pre><code class="language-js">rl.<span class="hljs-title function_">on</span>(<span class="hljs-string">'pause'</span>, <span class="hljs-function">() =></span> {
284  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'Readline paused.'</span>);
285});</code></pre>
286<h4>Event: <code>'resume'</code><span><a class="mark" href="#readline_event_resume" id="readline_event_resume">#</a></span></h4>
287<div class="api_metadata">
288<span>Added in: v0.7.5</span>
289</div>
290<p>The <code>'resume'</code> event is emitted whenever the <code>input</code> stream is resumed.</p>
291<p>The listener function is called without passing any arguments.</p>
292<pre><code class="language-js">rl.<span class="hljs-title function_">on</span>(<span class="hljs-string">'resume'</span>, <span class="hljs-function">() =></span> {
293  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'Readline resumed.'</span>);
294});</code></pre>
295<h4>Event: <code>'SIGCONT'</code><span><a class="mark" href="#readline_event_sigcont" id="readline_event_sigcont">#</a></span></h4>
296<div class="api_metadata">
297<span>Added in: v0.7.5</span>
298</div>
299<p>The <code>'SIGCONT'</code> event is emitted when a Node.js process previously moved into
300the background using <kbd>Ctrl</kbd>+<kbd>Z</kbd> (i.e. <code>SIGTSTP</code>) is then
301brought back to the foreground using <a href="http://man7.org/linux/man-pages/man1/fg.1p.html"><code>fg(1p)</code></a>.</p>
302<p>If the <code>input</code> stream was paused <em>before</em> the <code>SIGTSTP</code> request, this event will
303not be emitted.</p>
304<p>The listener function is invoked without passing any arguments.</p>
305<pre><code class="language-js">rl.<span class="hljs-title function_">on</span>(<span class="hljs-string">'SIGCONT'</span>, <span class="hljs-function">() =></span> {
306  <span class="hljs-comment">// `prompt` will automatically resume the stream</span>
307  rl.<span class="hljs-title function_">prompt</span>();
308});</code></pre>
309<p>The <code>'SIGCONT'</code> event is <em>not</em> supported on Windows.</p>
310<h4>Event: <code>'SIGINT'</code><span><a class="mark" href="#readline_event_sigint" id="readline_event_sigint">#</a></span></h4>
311<div class="api_metadata">
312<span>Added in: v0.3.0</span>
313</div>
314<p>The <code>'SIGINT'</code> event is emitted whenever the <code>input</code> stream receives a
315<kbd>Ctrl+C</kbd> input, known typically as <code>SIGINT</code>. If there are no <code>'SIGINT'</code>
316event listeners registered when the <code>input</code> stream receives a <code>SIGINT</code>, the
317<code>'pause'</code> event will be emitted.</p>
318<p>The listener function is invoked without passing any arguments.</p>
319<pre><code class="language-js">rl.<span class="hljs-title function_">on</span>(<span class="hljs-string">'SIGINT'</span>, <span class="hljs-function">() =></span> {
320  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> {
321    <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>();
322  });
323});</code></pre>
324<h4>Event: <code>'SIGTSTP'</code><span><a class="mark" href="#readline_event_sigtstp" id="readline_event_sigtstp">#</a></span></h4>
325<div class="api_metadata">
326<span>Added in: v0.7.5</span>
327</div>
328<p>The <code>'SIGTSTP'</code> event is emitted when the <code>input</code> stream receives a
329<kbd>Ctrl</kbd>+<kbd>Z</kbd> input, typically known as <code>SIGTSTP</code>. If there are
330no <code>'SIGTSTP'</code> event listeners registered when the <code>input</code> stream receives a
331<code>SIGTSTP</code>, the Node.js process will be sent to the background.</p>
332<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
333will be emitted. These can be used to resume the <code>input</code> stream.</p>
334<p>The <code>'pause'</code> and <code>'SIGCONT'</code> events will not be emitted if the <code>input</code> was
335paused before the process was sent to the background.</p>
336<p>The listener function is invoked without passing any arguments.</p>
337<pre><code class="language-js">rl.<span class="hljs-title function_">on</span>(<span class="hljs-string">'SIGTSTP'</span>, <span class="hljs-function">() =></span> {
338  <span class="hljs-comment">// This will override SIGTSTP and prevent the program from going to the</span>
339  <span class="hljs-comment">// background.</span>
340  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'Caught SIGTSTP.'</span>);
341});</code></pre>
342<p>The <code>'SIGTSTP'</code> event is <em>not</em> supported on Windows.</p>
343<h4><code>rl.close()</code><span><a class="mark" href="#readline_rl_close" id="readline_rl_close">#</a></span></h4>
344<div class="api_metadata">
345<span>Added in: v0.1.98</span>
346</div>
347<p>The <code>rl.close()</code> method closes the <code>readline.Interface</code> instance and
348relinquishes control over the <code>input</code> and <code>output</code> streams. When called,
349the <code>'close'</code> event will be emitted.</p>
350<p>Calling <code>rl.close()</code> does not immediately stop other events (including <code>'line'</code>)
351from being emitted by the <code>readline.Interface</code> instance.</p>
352<h4><code>rl.pause()</code><span><a class="mark" href="#readline_rl_pause" id="readline_rl_pause">#</a></span></h4>
353<div class="api_metadata">
354<span>Added in: v0.3.4</span>
355</div>
356<p>The <code>rl.pause()</code> method pauses the <code>input</code> stream, allowing it to be resumed
357later if necessary.</p>
358<p>Calling <code>rl.pause()</code> does not immediately pause other events (including
359<code>'line'</code>) from being emitted by the <code>readline.Interface</code> instance.</p>
360<h4><code>rl.prompt([preserveCursor])</code><span><a class="mark" href="#readline_rl_prompt_preservecursor" id="readline_rl_prompt_preservecursor">#</a></span></h4>
361<div class="api_metadata">
362<span>Added in: v0.1.98</span>
363</div>
364<ul>
365<li><code>preserveCursor</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> If <code>true</code>, prevents the cursor placement from
366being reset to <code>0</code>.</li>
367</ul>
368<p>The <code>rl.prompt()</code> method writes the <code>readline.Interface</code> instances configured
369<code>prompt</code> to a new line in <code>output</code> in order to provide a user with a new
370location at which to provide input.</p>
371<p>When called, <code>rl.prompt()</code> will resume the <code>input</code> stream if it has been
372paused.</p>
373<p>If the <code>readline.Interface</code> was created with <code>output</code> set to <code>null</code> or
374<code>undefined</code> the prompt is not written.</p>
375<h4><code>rl.question(query[, options], callback)</code><span><a class="mark" href="#readline_rl_question_query_options_callback" id="readline_rl_question_query_options_callback">#</a></span></h4>
376<div class="api_metadata">
377<span>Added in: v0.3.3</span>
378</div>
379<ul>
380<li><code>query</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> A statement or query to write to <code>output</code>, prepended to the
381prompt.</li>
382<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
383<ul>
384<li><code>signal</code> <a href="globals.html#globals_class_abortsignal" class="type">&#x3C;AbortSignal></a> Optionally allows the <code>question()</code> to be canceled
385using an <code>AbortController</code>.</li>
386</ul>
387</li>
388<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> A callback function that is invoked with the user's
389input in response to the <code>query</code>.</li>
390</ul>
391<p>The <code>rl.question()</code> method displays the <code>query</code> by writing it to the <code>output</code>,
392waits for user input to be provided on <code>input</code>, then invokes the <code>callback</code>
393function passing the provided input as the first argument.</p>
394<p>When called, <code>rl.question()</code> will resume the <code>input</code> stream if it has been
395paused.</p>
396<p>If the <code>readline.Interface</code> was created with <code>output</code> set to <code>null</code> or
397<code>undefined</code> the <code>query</code> is not written.</p>
398<p>The <code>callback</code> function passed to <code>rl.question()</code> does not follow the typical
399pattern of accepting an <code>Error</code> object or <code>null</code> as the first argument.
400The <code>callback</code> is called with the provided answer as the only argument.</p>
401<p>Example usage:</p>
402<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> {
403  <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>);
404});</code></pre>
405<p>Using an <code>AbortController</code> to cancel a question.</p>
406<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>();
407<span class="hljs-keyword">const</span> signal = ac.<span class="hljs-property">signal</span>;
408
409rl.<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> {
410  <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>);
411});
412
413signal.<span class="hljs-title function_">addEventListener</span>(<span class="hljs-string">'abort'</span>, <span class="hljs-function">() =></span> {
414  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'The food question timed out'</span>);
415}, { <span class="hljs-attr">once</span>: <span class="hljs-literal">true</span> });
416
417<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></pre>
418<p>If this method is invoked as it's util.promisify()ed version, it returns a
419Promise that fulfills with the answer. If the question is canceled using
420an <code>AbortController</code> it will reject with an <code>AbortError</code>.</p>
421<pre><code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in">require</span>(<span class="hljs-string">'util'</span>);
422<span class="hljs-keyword">const</span> question = util.<span class="hljs-title function_">promisify</span>(rl.<span class="hljs-property">question</span>).<span class="hljs-title function_">bind</span>(rl);
423
424<span class="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-title function_">questionExample</span>(<span class="hljs-params"></span>) {
425  <span class="hljs-keyword">try</span> {
426    <span class="hljs-keyword">const</span> answer = <span class="hljs-keyword">await</span> <span class="hljs-title function_">question</span>(<span class="hljs-string">'What is you favorite food? '</span>);
427    <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>);
428  } <span class="hljs-keyword">catch</span> (err) {
429    <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">'Question rejected'</span>, err);
430  }
431}
432<span class="hljs-title function_">questionExample</span>();</code></pre>
433<h4><code>rl.resume()</code><span><a class="mark" href="#readline_rl_resume" id="readline_rl_resume">#</a></span></h4>
434<div class="api_metadata">
435<span>Added in: v0.3.4</span>
436</div>
437<p>The <code>rl.resume()</code> method resumes the <code>input</code> stream if it has been paused.</p>
438<h4><code>rl.setPrompt(prompt)</code><span><a class="mark" href="#readline_rl_setprompt_prompt" id="readline_rl_setprompt_prompt">#</a></span></h4>
439<div class="api_metadata">
440<span>Added in: v0.1.98</span>
441</div>
442<ul>
443<li><code>prompt</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
444</ul>
445<p>The <code>rl.setPrompt()</code> method sets the prompt that will be written to <code>output</code>
446whenever <code>rl.prompt()</code> is called.</p>
447<h4><code>rl.getPrompt()</code><span><a class="mark" href="#readline_rl_getprompt" id="readline_rl_getprompt">#</a></span></h4>
448<div class="api_metadata">
449<span>Added in: v14.17.0</span>
450</div>
451<ul>
452<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> the current prompt string</li>
453</ul>
454<p>The <code>rl.getPrompt()</code> method returns the current prompt used by <code>rl.prompt()</code>.</p>
455<h4><code>rl.write(data[, key])</code><span><a class="mark" href="#readline_rl_write_data_key" id="readline_rl_write_data_key">#</a></span></h4>
456<div class="api_metadata">
457<span>Added in: v0.1.98</span>
458</div>
459<ul>
460<li><code>data</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
461<li><code>key</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
462<ul>
463<li><code>ctrl</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> <code>true</code> to indicate the <kbd>Ctrl</kbd> key.</li>
464<li><code>meta</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> <code>true</code> to indicate the <kbd>Meta</kbd> key.</li>
465<li><code>shift</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> <code>true</code> to indicate the <kbd>Shift</kbd> key.</li>
466<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The name of the a key.</li>
467</ul>
468</li>
469</ul>
470<p>The <code>rl.write()</code> method will write either <code>data</code> or a key sequence identified
471by <code>key</code> to the <code>output</code>. The <code>key</code> argument is supported only if <code>output</code> is
472a <a href="tty.html">TTY</a> text terminal. See <a href="#readline_tty_keybindings">TTY keybindings</a> for a list of key
473combinations.</p>
474<p>If <code>key</code> is specified, <code>data</code> is ignored.</p>
475<p>When called, <code>rl.write()</code> will resume the <code>input</code> stream if it has been
476paused.</p>
477<p>If the <code>readline.Interface</code> was created with <code>output</code> set to <code>null</code> or
478<code>undefined</code> the <code>data</code> and <code>key</code> are not written.</p>
479<pre><code class="language-js">rl.<span class="hljs-title function_">write</span>(<span class="hljs-string">'Delete this!'</span>);
480<span class="hljs-comment">// Simulate Ctrl+U to delete the line written previously</span>
481rl.<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></pre>
482<p>The <code>rl.write()</code> method will write the data to the <code>readline</code> <code>Interface</code>'s
483<code>input</code> <em>as if it were provided by the user</em>.</p>
484<h4><code>rl[Symbol.asyncIterator]()</code><span><a class="mark" href="#readline_rl_symbol_asynciterator" id="readline_rl_symbol_asynciterator">#</a></span></h4>
485<div class="api_metadata">
486<details class="changelog"><summary>History</summary>
487<table>
488<tbody><tr><th>Version</th><th>Changes</th></tr>
489<tr><td>v11.4.0, v10.16.0</td>
490<td><p><span>Added in: v11.4.0, v10.16.0</span></p></td></tr>
491<tr><td>v11.14.0, v10.17.0</td>
492<td><p>Symbol.asyncIterator support is no longer experimental.</p></td></tr>
493</tbody></table>
494</details>
495</div>
496<ul>
497<li>Returns: <a href="https://tc39.github.io/ecma262/#sec-asynciterator-interface" class="type">&#x3C;AsyncIterator></a></li>
498</ul>
499<p>Create an <code>AsyncIterator</code> object that iterates through each line in the input
500stream as a string. This method allows asynchronous iteration of
501<code>readline.Interface</code> objects through <code>for await...of</code> loops.</p>
502<p>Errors in the input stream are not forwarded.</p>
503<p>If the loop is terminated with <code>break</code>, <code>throw</code>, or <code>return</code>,
504<a href="#readline_rl_close"><code>rl.close()</code></a> will be called. In other words, iterating over a
505<code>readline.Interface</code> will always consume the input stream fully.</p>
506<p>Performance is not on par with the traditional <code>'line'</code> event API. Use <code>'line'</code>
507instead for performance-sensitive applications.</p>
508<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>) {
509  <span class="hljs-keyword">const</span> rl = readline.<span class="hljs-title function_">createInterface</span>({
510    <span class="hljs-comment">// ...</span>
511  });
512
513  <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) {
514    <span class="hljs-comment">// Each line in the readline input will be successively available here as</span>
515    <span class="hljs-comment">// `line`.</span>
516  }
517}</code></pre>
518<p><code>readline.createInterface()</code> will start to consume the input stream once
519invoked. Having asynchronous operations between interface creation and
520asynchronous iteration may result in missed lines.</p>
521<h4><code>rl.line</code><span><a class="mark" href="#readline_rl_line" id="readline_rl_line">#</a></span></h4>
522<div class="api_metadata">
523<details class="changelog"><summary>History</summary>
524<table>
525<tbody><tr><th>Version</th><th>Changes</th></tr>
526<tr><td>v14.18.0</td>
527<td><p>Value will always be a string, never undefined.</p></td></tr>
528<tr><td>v0.1.98</td>
529<td><p><span>Added in: v0.1.98</span></p></td></tr>
530</tbody></table>
531</details>
532</div>
533<ul>
534<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
535</ul>
536<p>The current input data being processed by node.</p>
537<p>This can be used when collecting input from a TTY stream to retrieve the
538current value that has been processed thus far, prior to the <code>line</code> event
539being emitted. Once the <code>line</code> event has been emitted, this property will
540be an empty string.</p>
541<p>Be aware that modifying the value during the instance runtime may have
542unintended consequences if <code>rl.cursor</code> is not also controlled.</p>
543<p><strong>If not using a TTY stream for input, use the <a href="#readline_event_line"><code>'line'</code></a> event.</strong></p>
544<p>One possible use case would be as follows:</p>
545<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>];
546<span class="hljs-keyword">const</span> rl = readline.<span class="hljs-title function_">createInterface</span>(process.<span class="hljs-property">stdin</span>);
547<span class="hljs-keyword">const</span> showResults = <span class="hljs-title function_">debounce</span>(<span class="hljs-function">() =></span> {
548  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(
549    <span class="hljs-string">'\n'</span>,
550    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>)
551  );
552}, <span class="hljs-number">300</span>);
553process.<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> {
554  <span class="hljs-title function_">showResults</span>();
555});</code></pre>
556<h4><code>rl.cursor</code><span><a class="mark" href="#readline_rl_cursor" id="readline_rl_cursor">#</a></span></h4>
557<div class="api_metadata">
558<span>Added in: v0.1.98</span>
559</div>
560<ul>
561<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Undefined_type" class="type">&#x3C;undefined></a></li>
562</ul>
563<p>The cursor position relative to <code>rl.line</code>.</p>
564<p>This will track where the current cursor lands in the input string, when
565reading input from a TTY stream. The position of cursor determines the
566portion of the input string that will be modified as input is processed,
567as well as the column where the terminal caret will be rendered.</p>
568<h4><code>rl.getCursorPos()</code><span><a class="mark" href="#readline_rl_getcursorpos" id="readline_rl_getcursorpos">#</a></span></h4>
569<div class="api_metadata">
570<span>Added in: v13.5.0, v12.16.0</span>
571</div>
572<ul>
573<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
574<ul>
575<li><code>rows</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> the row of the prompt the cursor currently lands on</li>
576<li><code>cols</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> the screen column the cursor currently lands on</li>
577</ul>
578</li>
579</ul>
580<p>Returns the real position of the cursor in relation to the input
581prompt + string. Long input (wrapping) strings, as well as multiple
582line prompts are included in the calculations.</p>
583</section><section><h3><code>readline.clearLine(stream, dir[, callback])</code><span><a class="mark" href="#readline_readline_clearline_stream_dir_callback" id="readline_readline_clearline_stream_dir_callback">#</a></span></h3>
584<div class="api_metadata">
585<details class="changelog"><summary>History</summary>
586<table>
587<tbody><tr><th>Version</th><th>Changes</th></tr>
588<tr><td>v12.7.0</td>
589<td><p>The stream's write() callback and return value are exposed.</p></td></tr>
590<tr><td>v0.7.7</td>
591<td><p><span>Added in: v0.7.7</span></p></td></tr>
592</tbody></table>
593</details>
594</div>
595<ul>
596<li><code>stream</code> <a href="stream.html#stream_class_stream_writable" class="type">&#x3C;stream.Writable></a></li>
597<li><code>dir</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a>
598<ul>
599<li><code>-1</code>: to the left from cursor</li>
600<li><code>1</code>: to the right from cursor</li>
601<li><code>0</code>: the entire line</li>
602</ul>
603</li>
604<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> Invoked once the operation completes.</li>
605<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> <code>false</code> if <code>stream</code> wishes for the calling code to wait for
606the <code>'drain'</code> event to be emitted before continuing to write additional data;
607otherwise <code>true</code>.</li>
608</ul>
609<p>The <code>readline.clearLine()</code> method clears current line of given <a href="tty.html">TTY</a> stream
610in a specified direction identified by <code>dir</code>.</p>
611</section><section><h3><code>readline.clearScreenDown(stream[, callback])</code><span><a class="mark" href="#readline_readline_clearscreendown_stream_callback" id="readline_readline_clearscreendown_stream_callback">#</a></span></h3>
612<div class="api_metadata">
613<details class="changelog"><summary>History</summary>
614<table>
615<tbody><tr><th>Version</th><th>Changes</th></tr>
616<tr><td>v12.7.0</td>
617<td><p>The stream's write() callback and return value are exposed.</p></td></tr>
618<tr><td>v0.7.7</td>
619<td><p><span>Added in: v0.7.7</span></p></td></tr>
620</tbody></table>
621</details>
622</div>
623<ul>
624<li><code>stream</code> <a href="stream.html#stream_class_stream_writable" class="type">&#x3C;stream.Writable></a></li>
625<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> Invoked once the operation completes.</li>
626<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> <code>false</code> if <code>stream</code> wishes for the calling code to wait for
627the <code>'drain'</code> event to be emitted before continuing to write additional data;
628otherwise <code>true</code>.</li>
629</ul>
630<p>The <code>readline.clearScreenDown()</code> method clears the given <a href="tty.html">TTY</a> stream from
631the current position of the cursor down.</p>
632</section><section><h3><code>readline.createInterface(options)</code><span><a class="mark" href="#readline_readline_createinterface_options" id="readline_readline_createinterface_options">#</a></span></h3>
633<div class="api_metadata">
634<details class="changelog"><summary>History</summary>
635<table>
636<tbody><tr><th>Version</th><th>Changes</th></tr>
637<tr><td>v14.18.0</td>
638<td><p>The <code>signal</code> option is supported now.</p></td></tr>
639<tr><td>v14.18.0</td>
640<td><p>The <code>history</code> option is supported now.</p></td></tr>
641<tr><td>v13.9.0</td>
642<td><p>The <code>tabSize</code> option is supported now.</p></td></tr>
643<tr><td>v8.3.0, v6.11.4</td>
644<td><p>Remove max limit of <code>crlfDelay</code> option.</p></td></tr>
645<tr><td>v6.6.0</td>
646<td><p>The <code>crlfDelay</code> option is supported now.</p></td></tr>
647<tr><td>v6.3.0</td>
648<td><p>The <code>prompt</code> option is supported now.</p></td></tr>
649<tr><td>v6.0.0</td>
650<td><p>The <code>historySize</code> option can be <code>0</code> now.</p></td></tr>
651<tr><td>v0.1.98</td>
652<td><p><span>Added in: v0.1.98</span></p></td></tr>
653</tbody></table>
654</details>
655</div>
656<ul>
657<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a>
658<ul>
659<li><code>input</code> <a href="stream.html#stream_class_stream_readable" class="type">&#x3C;stream.Readable></a> The <a href="stream.html#stream_readable_streams">Readable</a> stream to listen to. This option
660is <em>required</em>.</li>
661<li><code>output</code> <a href="stream.html#stream_class_stream_writable" class="type">&#x3C;stream.Writable></a> The <a href="stream.html#stream_writable_streams">Writable</a> stream to write readline data
662to.</li>
663<li><code>completer</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> An optional function used for Tab autocompletion.</li>
664<li><code>terminal</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> <code>true</code> if the <code>input</code> and <code>output</code> streams should be
665treated like a TTY, and have ANSI/VT100 escape codes written to it.
666<strong>Default:</strong> checking <code>isTTY</code> on the <code>output</code> stream upon instantiation.</li>
667<li><code>history</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string[]></a> Initial list of history lines. This option makes sense
668only if <code>terminal</code> is set to <code>true</code> by the user or by an internal <code>output</code>
669check, otherwise the history caching mechanism is not initialized at all.
670<strong>Default:</strong> <code>[]</code>.</li>
671<li><code>historySize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Maximum number of history lines retained. To disable
672the history set this value to <code>0</code>. This option makes sense only if
673<code>terminal</code> is set to <code>true</code> by the user or by an internal <code>output</code> check,
674otherwise the history caching mechanism is not initialized at all.
675<strong>Default:</strong> <code>30</code>.</li>
676<li><code>removeHistoryDuplicates</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> If <code>true</code>, when a new input line added
677to the history list duplicates an older one, this removes the older line
678from the list. <strong>Default:</strong> <code>false</code>.</li>
679<li><code>prompt</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The prompt string to use. <strong>Default:</strong> <code>'> '</code>.</li>
680<li><code>crlfDelay</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> If the delay between <code>\r</code> and <code>\n</code> exceeds
681<code>crlfDelay</code> milliseconds, both <code>\r</code> and <code>\n</code> will be treated as separate
682end-of-line input. <code>crlfDelay</code> will be coerced to a number no less than
683<code>100</code>. It can be set to <code>Infinity</code>, in which case <code>\r</code> followed by <code>\n</code>
684will always be considered a single newline (which may be reasonable for
685<a href="#readline_example_read_file_stream_line_by_line">reading files</a> with <code>\r\n</code> line delimiter). <strong>Default:</strong> <code>100</code>.</li>
686<li><code>escapeCodeTimeout</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> The duration <code>readline</code> will wait for a
687character (when reading an ambiguous key sequence in milliseconds one that
688can both form a complete key sequence using the input read so far and can
689take additional input to complete a longer key sequence).
690<strong>Default:</strong> <code>500</code>.</li>
691<li><code>tabSize</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;integer></a> The number of spaces a tab is equal to (minimum 1).
692<strong>Default:</strong> <code>8</code>.</li>
693<li><code>signal</code> <a href="globals.html#globals_class_abortsignal" class="type">&#x3C;AbortSignal></a> Allows closing the interface using an AbortSignal.
694Aborting the signal will internally call <code>close</code> on the interface.</li>
695</ul>
696</li>
697<li>Returns: <a href="readline.html#readline_class_interface" class="type">&#x3C;readline.Interface></a></li>
698</ul>
699<p>The <code>readline.createInterface()</code> method creates a new <code>readline.Interface</code>
700instance.</p>
701<pre><code class="language-js"><span class="hljs-keyword">const</span> readline = <span class="hljs-built_in">require</span>(<span class="hljs-string">'readline'</span>);
702<span class="hljs-keyword">const</span> rl = readline.<span class="hljs-title function_">createInterface</span>({
703  <span class="hljs-attr">input</span>: process.<span class="hljs-property">stdin</span>,
704  <span class="hljs-attr">output</span>: process.<span class="hljs-property">stdout</span>
705});</code></pre>
706<p>Once the <code>readline.Interface</code> instance is created, the most common case is to
707listen for the <code>'line'</code> event:</p>
708<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> {
709  <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>);
710});</code></pre>
711<p>If <code>terminal</code> is <code>true</code> for this instance then the <code>output</code> stream will get
712the best compatibility if it defines an <code>output.columns</code> property and emits
713a <code>'resize'</code> event on the <code>output</code> if or when the columns ever change
714(<a href="process.html#process_process_stdout"><code>process.stdout</code></a> does this automatically when it is a TTY).</p>
715<p>When creating a <code>readline.Interface</code> using <code>stdin</code> as input, the program
716will not terminate until it receives <code>EOF</code> (<kbd>Ctrl</kbd>+<kbd>D</kbd> on
717Linux/macOS, <kbd>Ctrl</kbd>+<kbd>Z</kbd> followed by <kbd>Return</kbd> on
718Windows).
719If you want your application to exit without waiting for user input, you can
720<a href="net.html#net_socket_unref"><code>unref()</code></a> the standard input stream:</p>
721<pre><code class="language-js">process.<span class="hljs-property">stdin</span>.<span class="hljs-title function_">unref</span>();</code></pre>
722<h4>Use of the <code>completer</code> function<span><a class="mark" href="#readline_use_of_the_completer_function" id="readline_use_of_the_completer_function">#</a></span></h4>
723<p>The <code>completer</code> function takes the current line entered by the user
724as an argument, and returns an <code>Array</code> with 2 entries:</p>
725<ul>
726<li>An <code>Array</code> with matching entries for the completion.</li>
727<li>The substring that was used for the matching.</li>
728</ul>
729<p>For instance: <code>[[substr1, substr2, ...], originalsubstring]</code>.</p>
730<pre><code class="language-js"><span class="hljs-keyword">function</span> <span class="hljs-title function_">completer</span>(<span class="hljs-params">line</span>) {
731  <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>);
732  <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));
733  <span class="hljs-comment">// Show all completions if none found</span>
734  <span class="hljs-keyword">return</span> [hits.<span class="hljs-property">length</span> ? hits : completions, line];
735}</code></pre>
736<p>The <code>completer</code> function can be called asynchronously if it accepts two
737arguments:</p>
738<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>) {
739  <span class="hljs-title function_">callback</span>(<span class="hljs-literal">null</span>, [[<span class="hljs-string">'123'</span>], linePartial]);
740}</code></pre>
741</section><section><h3><code>readline.cursorTo(stream, x[, y][, callback])</code><span><a class="mark" href="#readline_readline_cursorto_stream_x_y_callback" id="readline_readline_cursorto_stream_x_y_callback">#</a></span></h3>
742<div class="api_metadata">
743<details class="changelog"><summary>History</summary>
744<table>
745<tbody><tr><th>Version</th><th>Changes</th></tr>
746<tr><td>v12.7.0</td>
747<td><p>The stream's write() callback and return value are exposed.</p></td></tr>
748<tr><td>v0.7.7</td>
749<td><p><span>Added in: v0.7.7</span></p></td></tr>
750</tbody></table>
751</details>
752</div>
753<ul>
754<li><code>stream</code> <a href="stream.html#stream_class_stream_writable" class="type">&#x3C;stream.Writable></a></li>
755<li><code>x</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
756<li><code>y</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
757<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> Invoked once the operation completes.</li>
758<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> <code>false</code> if <code>stream</code> wishes for the calling code to wait for
759the <code>'drain'</code> event to be emitted before continuing to write additional data;
760otherwise <code>true</code>.</li>
761</ul>
762<p>The <code>readline.cursorTo()</code> method moves cursor to the specified position in a
763given <a href="tty.html">TTY</a> <code>stream</code>.</p>
764</section><section><h3><code>readline.emitKeypressEvents(stream[, interface])</code><span><a class="mark" href="#readline_readline_emitkeypressevents_stream_interface" id="readline_readline_emitkeypressevents_stream_interface">#</a></span></h3>
765<div class="api_metadata">
766<span>Added in: v0.7.7</span>
767</div>
768<ul>
769<li><code>stream</code> <a href="stream.html#stream_class_stream_readable" class="type">&#x3C;stream.Readable></a></li>
770<li><code>interface</code> <a href="readline.html#readline_class_interface" class="type">&#x3C;readline.Interface></a></li>
771</ul>
772<p>The <code>readline.emitKeypressEvents()</code> method causes the given <a href="stream.html#stream_readable_streams">Readable</a>
773stream to begin emitting <code>'keypress'</code> events corresponding to received input.</p>
774<p>Optionally, <code>interface</code> specifies a <code>readline.Interface</code> instance for which
775autocompletion is disabled when copy-pasted input is detected.</p>
776<p>If the <code>stream</code> is a <a href="tty.html">TTY</a>, then it must be in raw mode.</p>
777<p>This is automatically called by any readline instance on its <code>input</code> if the
778<code>input</code> is a terminal. Closing the <code>readline</code> instance does not stop
779the <code>input</code> from emitting <code>'keypress'</code> events.</p>
780<pre><code class="language-js">readline.<span class="hljs-title function_">emitKeypressEvents</span>(process.<span class="hljs-property">stdin</span>);
781<span class="hljs-keyword">if</span> (process.<span class="hljs-property">stdin</span>.<span class="hljs-property">isTTY</span>)
782  process.<span class="hljs-property">stdin</span>.<span class="hljs-title function_">setRawMode</span>(<span class="hljs-literal">true</span>);</code></pre>
783</section><section><h3><code>readline.moveCursor(stream, dx, dy[, callback])</code><span><a class="mark" href="#readline_readline_movecursor_stream_dx_dy_callback" id="readline_readline_movecursor_stream_dx_dy_callback">#</a></span></h3>
784<div class="api_metadata">
785<details class="changelog"><summary>History</summary>
786<table>
787<tbody><tr><th>Version</th><th>Changes</th></tr>
788<tr><td>v12.7.0</td>
789<td><p>The stream's write() callback and return value are exposed.</p></td></tr>
790<tr><td>v0.7.7</td>
791<td><p><span>Added in: v0.7.7</span></p></td></tr>
792</tbody></table>
793</details>
794</div>
795<ul>
796<li><code>stream</code> <a href="stream.html#stream_class_stream_writable" class="type">&#x3C;stream.Writable></a></li>
797<li><code>dx</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
798<li><code>dy</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a></li>
799<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> Invoked once the operation completes.</li>
800<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> <code>false</code> if <code>stream</code> wishes for the calling code to wait for
801the <code>'drain'</code> event to be emitted before continuing to write additional data;
802otherwise <code>true</code>.</li>
803</ul>
804<p>The <code>readline.moveCursor()</code> method moves the cursor <em>relative</em> to its current
805position in a given <a href="tty.html">TTY</a> <code>stream</code>.</p>
806</section><section><h3>Example: Tiny CLI<span><a class="mark" href="#readline_example_tiny_cli" id="readline_example_tiny_cli">#</a></span></h3>
807<p>The following example illustrates the use of <code>readline.Interface</code> class to
808implement a small command-line interface:</p>
809<pre><code class="language-js"><span class="hljs-keyword">const</span> readline = <span class="hljs-built_in">require</span>(<span class="hljs-string">'readline'</span>);
810<span class="hljs-keyword">const</span> rl = readline.<span class="hljs-title function_">createInterface</span>({
811  <span class="hljs-attr">input</span>: process.<span class="hljs-property">stdin</span>,
812  <span class="hljs-attr">output</span>: process.<span class="hljs-property">stdout</span>,
813  <span class="hljs-attr">prompt</span>: <span class="hljs-string">'OHAI> '</span>
814});
815
816rl.<span class="hljs-title function_">prompt</span>();
817
818rl.<span class="hljs-title function_">on</span>(<span class="hljs-string">'line'</span>, <span class="hljs-function">(<span class="hljs-params">line</span>) =></span> {
819  <span class="hljs-keyword">switch</span> (line.<span class="hljs-title function_">trim</span>()) {
820    <span class="hljs-keyword">case</span> <span class="hljs-string">'hello'</span>:
821      <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'world!'</span>);
822      <span class="hljs-keyword">break</span>;
823    <span class="hljs-attr">default</span>:
824      <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>);
825      <span class="hljs-keyword">break</span>;
826  }
827  rl.<span class="hljs-title function_">prompt</span>();
828}).<span class="hljs-title function_">on</span>(<span class="hljs-string">'close'</span>, <span class="hljs-function">() =></span> {
829  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'Have a great day!'</span>);
830  process.<span class="hljs-title function_">exit</span>(<span class="hljs-number">0</span>);
831});</code></pre>
832</section><section><h3>Example: Read file stream line-by-Line<span><a class="mark" href="#readline_example_read_file_stream_line_by_line" id="readline_example_read_file_stream_line_by_line">#</a></span></h3>
833<p>A common use case for <code>readline</code> is to consume an input file one line at a
834time. The easiest way to do so is leveraging the <a href="fs.html#fs_class_fs_readstream"><code>fs.ReadStream</code></a> API as
835well as a <code>for await...of</code> loop:</p>
836<pre><code class="language-js"><span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">'fs'</span>);
837<span class="hljs-keyword">const</span> readline = <span class="hljs-built_in">require</span>(<span class="hljs-string">'readline'</span>);
838
839<span class="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-title function_">processLineByLine</span>(<span class="hljs-params"></span>) {
840  <span class="hljs-keyword">const</span> fileStream = fs.<span class="hljs-title function_">createReadStream</span>(<span class="hljs-string">'input.txt'</span>);
841
842  <span class="hljs-keyword">const</span> rl = readline.<span class="hljs-title function_">createInterface</span>({
843    <span class="hljs-attr">input</span>: fileStream,
844    <span class="hljs-attr">crlfDelay</span>: <span class="hljs-title class_">Infinity</span>
845  });
846  <span class="hljs-comment">// Note: we use the crlfDelay option to recognize all instances of CR LF</span>
847  <span class="hljs-comment">// ('\r\n') in input.txt as a single line break.</span>
848
849  <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) {
850    <span class="hljs-comment">// Each line in input.txt will be successively available here as `line`.</span>
851    <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>);
852  }
853}
854
855<span class="hljs-title function_">processLineByLine</span>();</code></pre>
856<p>Alternatively, one could use the <a href="#readline_event_line"><code>'line'</code></a> event:</p>
857<pre><code class="language-js"><span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">'fs'</span>);
858<span class="hljs-keyword">const</span> readline = <span class="hljs-built_in">require</span>(<span class="hljs-string">'readline'</span>);
859
860<span class="hljs-keyword">const</span> rl = readline.<span class="hljs-title function_">createInterface</span>({
861  <span class="hljs-attr">input</span>: fs.<span class="hljs-title function_">createReadStream</span>(<span class="hljs-string">'sample.txt'</span>),
862  <span class="hljs-attr">crlfDelay</span>: <span class="hljs-title class_">Infinity</span>
863});
864
865rl.<span class="hljs-title function_">on</span>(<span class="hljs-string">'line'</span>, <span class="hljs-function">(<span class="hljs-params">line</span>) =></span> {
866  <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>);
867});</code></pre>
868<p>Currently, <code>for await...of</code> loop can be a bit slower. If <code>async</code> / <code>await</code>
869flow and speed are both essential, a mixed approach can be applied:</p>
870<pre><code class="language-js"><span class="hljs-keyword">const</span> { once } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'events'</span>);
871<span class="hljs-keyword">const</span> { createReadStream } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'fs'</span>);
872<span class="hljs-keyword">const</span> { createInterface } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'readline'</span>);
873
874(<span class="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-title function_">processLineByLine</span>(<span class="hljs-params"></span>) {
875  <span class="hljs-keyword">try</span> {
876    <span class="hljs-keyword">const</span> rl = <span class="hljs-title function_">createInterface</span>({
877      <span class="hljs-attr">input</span>: <span class="hljs-title function_">createReadStream</span>(<span class="hljs-string">'big-file.txt'</span>),
878      <span class="hljs-attr">crlfDelay</span>: <span class="hljs-title class_">Infinity</span>
879    });
880
881    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> {
882      <span class="hljs-comment">// Process the line.</span>
883    });
884
885    <span class="hljs-keyword">await</span> <span class="hljs-title function_">once</span>(rl, <span class="hljs-string">'close'</span>);
886
887    <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'File processed.'</span>);
888  } <span class="hljs-keyword">catch</span> (err) {
889    <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(err);
890  }
891})();</code></pre>
892</section><section><h3>TTY keybindings<span><a class="mark" href="#readline_tty_keybindings" id="readline_tty_keybindings">#</a></span></h3>
893<table>
894  <tbody><tr>
895    <th>Keybindings</th>
896    <th>Description</th>
897    <th>Notes</th>
898  </tr>
899  <tr>
900    <td><kbd>Ctrl</kbd>+<kbd>Shift</kbd>+<kbd>Backspace</kbd></td>
901    <td>Delete line left</td>
902    <td>Doesn't work on Linux, Mac and Windows</td>
903  </tr>
904  <tr>
905    <td><kbd>Ctrl</kbd>+<kbd>Shift</kbd>+<kbd>Delete</kbd></td>
906    <td>Delete line right</td>
907    <td>Doesn't work on Mac</td>
908  </tr>
909  <tr>
910    <td><kbd>Ctrl</kbd>+<kbd>C</kbd></td>
911    <td>Emit <code>SIGINT</code> or close the readline instance</td>
912    <td></td>
913  </tr>
914  <tr>
915    <td><kbd>Ctrl</kbd>+<kbd>H</kbd></td>
916    <td>Delete left</td>
917    <td></td>
918  </tr>
919  <tr>
920    <td><kbd>Ctrl</kbd>+<kbd>D</kbd></td>
921    <td>Delete right or close the readline instance in case the current line is empty / EOF</td>
922    <td>Doesn't work on Windows</td>
923  </tr>
924  <tr>
925    <td><kbd>Ctrl</kbd>+<kbd>U</kbd></td>
926    <td>Delete from the current position to the line start</td>
927    <td></td>
928  </tr>
929  <tr>
930    <td><kbd>Ctrl</kbd>+<kbd>K</kbd></td>
931    <td>Delete from the current position to the end of line</td>
932    <td></td>
933  </tr>
934  <tr>
935    <td><kbd>Ctrl</kbd>+<kbd>A</kbd></td>
936    <td>Go to start of line</td>
937    <td></td>
938  </tr>
939  <tr>
940    <td><kbd>Ctrl</kbd>+<kbd>E</kbd></td>
941    <td>Go to to end of line</td>
942    <td></td>
943  </tr>
944  <tr>
945    <td><kbd>Ctrl</kbd>+<kbd>B</kbd></td>
946    <td>Back one character</td>
947    <td></td>
948  </tr>
949  <tr>
950    <td><kbd>Ctrl</kbd>+<kbd>F</kbd></td>
951    <td>Forward one character</td>
952    <td></td>
953  </tr>
954  <tr>
955    <td><kbd>Ctrl</kbd>+<kbd>L</kbd></td>
956    <td>Clear screen</td>
957    <td></td>
958  </tr>
959  <tr>
960    <td><kbd>Ctrl</kbd>+<kbd>N</kbd></td>
961    <td>Next history item</td>
962    <td></td>
963  </tr>
964  <tr>
965    <td><kbd>Ctrl</kbd>+<kbd>P</kbd></td>
966    <td>Previous history item</td>
967    <td></td>
968  </tr>
969  <tr>
970    <td><kbd>Ctrl</kbd>+<kbd>Z</kbd></td>
971    <td>Moves running process into background. Type
972    <code>fg</code> and press <kbd>Enter</kbd>
973    to return.</td>
974    <td>Doesn't work on Windows</td>
975  </tr>
976  <tr>
977    <td><kbd>Ctrl</kbd>+<kbd>W</kbd> or <kbd>Ctrl</kbd>
978   +<kbd>Backspace</kbd></td>
979    <td>Delete backward to a word boundary</td>
980    <td><kbd>Ctrl</kbd>+<kbd>Backspace</kbd> Doesn't
981    work on Linux, Mac and Windows</td>
982  </tr>
983  <tr>
984    <td><kbd>Ctrl</kbd>+<kbd>Delete</kbd></td>
985    <td>Delete forward to a word boundary</td>
986    <td>Doesn't work on Mac</td>
987  </tr>
988  <tr>
989    <td><kbd>Ctrl</kbd>+<kbd>Left arrow</kbd> or
990    <kbd>Meta</kbd>+<kbd>B</kbd></td>
991    <td>Word left</td>
992    <td><kbd>Ctrl</kbd>+<kbd>Left arrow</kbd> Doesn't work
993    on Mac</td>
994  </tr>
995  <tr>
996    <td><kbd>Ctrl</kbd>+<kbd>Right arrow</kbd> or
997    <kbd>Meta</kbd>+<kbd>F</kbd></td>
998    <td>Word right</td>
999    <td><kbd>Ctrl</kbd>+<kbd>Right arrow</kbd> Doesn't work
1000    on Mac</td>
1001  </tr>
1002  <tr>
1003    <td><kbd>Meta</kbd>+<kbd>D</kbd> or <kbd>Meta</kbd>
1004   +<kbd>Delete</kbd></td>
1005    <td>Delete word right</td>
1006    <td><kbd>Meta</kbd>+<kbd>Delete</kbd> Doesn't work
1007    on windows</td>
1008  </tr>
1009  <tr>
1010    <td><kbd>Meta</kbd>+<kbd>Backspace</kbd></td>
1011    <td>Delete word left</td>
1012    <td>Doesn't work on Mac</td>
1013  </tr>
1014</tbody></table></section>
1015        <!-- API END -->
1016      </div>
1017    </div>
1018  </div>
1019  <script>
1020    'use strict';
1021    {
1022      const kCustomPreference = 'customDarkTheme';
1023      const userSettings = sessionStorage.getItem(kCustomPreference);
1024      const themeToggleButton = document.getElementById('theme-toggle-btn');
1025      if (userSettings === null && window.matchMedia) {
1026        const mq = window.matchMedia('(prefers-color-scheme: dark)');
1027        if ('onchange' in mq) {
1028          function mqChangeListener(e) {
1029            document.body.classList.toggle('dark-mode', e.matches);
1030          }
1031          mq.addEventListener('change', mqChangeListener);
1032          if (themeToggleButton) {
1033            themeToggleButton.addEventListener('click', function() {
1034              mq.removeEventListener('change', mqChangeListener);
1035            }, { once: true });
1036          }
1037        }
1038        if (mq.matches) {
1039          document.body.classList.add('dark-mode');
1040        }
1041      } else if (userSettings === 'true') {
1042        document.body.classList.add('dark-mode');
1043      }
1044      if (themeToggleButton) {
1045        themeToggleButton.hidden = false;
1046        themeToggleButton.addEventListener('click', function() {
1047          sessionStorage.setItem(
1048            kCustomPreference,
1049            document.body.classList.toggle('dark-mode')
1050          );
1051        });
1052      }
1053    }
1054  </script>
1055</body>
1056</html>
1057