Harsh Patel
Harsh Patel

Reputation: 141

Why does the terminal show “^[[A” “^[[B” “^[[C” “^[[D” when pressing the arrow keys in macos apple silicon m1 for c++?

I have refered previous questions but none of those helped. Issue is similar as mentioned here: Why does the terminal show "^[[A" "^[[B" "^[[C" "^[[D" when pressing the arrow keys in Ubuntu?

Executing g++(gcc11) and running it. Cannot move cursor using arrows.

Interestingly, while taking input in python does not have this issue.

Able to move cursor in Python3 input()

I have tried various things including using "bash" shell (3.2), using iTerm2 instead, running macos 11.4 in safe mode. Terminal app's key bindings are as default settings. None of the mentioned helped.

Edit: C++ Code:

#include <iostream>
using namespace std;

int main() {
    string s;
    cin>>s;
    return 0;
}

Upvotes: 0

Views: 2025

Answers (1)

n. m. could be an AI
n. m. could be an AI

Reputation: 120229

Python and bash use line editing components. Your C++ program does not. If you want to add line editing capability to your program, you need to add it. Don't expect it will appear out of thin air just because some other programs have it.

Fortunately it is not hard. There are several libraries that implement just that. One is GNU readline. There are several others, variously called "editline" or "libedit". You need to choose one of those and use it in your project.

Another possibility is to use a separate program called rlwrap which wraps your program, adding readline capabilities.

Upvotes: 1

Related Questions