Reputation: 59
I'm creating a Node.js application in Linux that spawns a child process, bash, when someone connects to the server via WebSocket. It sends all data sent through the WebSocket to stdin and sends all stdout output to the WebSocket client. I'm making the frontend using xtermjs. essentially, I'm creating a remote terminal using HTTP.
How can I send a backspace key to stdin? neither \b
nor \x7F
work for me. more importantly, how does bash (or any linux application that allows editing inputs) even recieve these kinds of inputs?
When I try to edit something I've already input into bash using the backspace key (sent over WebSocket as \b
or \x7F
I simply get an error regarding invalid input, telling me that ab\b\x7F
command doesnt exist.
There's also a chance I simply do not understand how the terminal or shell works, so excuse me.
Upvotes: 1
Views: 352
Reputation: 44711
\x7F
corresponds to DEL
(alternatively rubout
for those having experience in more legacy environments). It doesn’t do what you think it does. From the corresponding Wikipedia page:
It is supposed to do nothing and was designed to erase incorrect characters on paper tape.
Similarly, \b
, which corresponds to backspace
, also does not do what you seem to think it does. From the accepted answer on this related Stack Overflow question:
[
\b
] moves the Terminal cursor backward one space, but does not do anything to the character that's been backspaced over.
For those not accustomed to working in more “retrocomputing” environments, this would be more akin to the functionality of ←.
You can co-opt \b
into doing what you seem to expect it to do by chaining it with a blank space, followed by another \b
to overwrite the previous character with a blank, then move the cursor back one space pending the next write to stdin
: \b \b
Since you haven’t provided any code, this pseudocode should suffice to illustrate putting this idea into action:
child_process.stdin.write('\b \b');
Upvotes: 2