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