3

I wrote a c++ code with the readline library:

#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <readline/readline.h>
#include <readline/history.h>

int main(int argc, char** argv) {

  char* buf;
  while ((buf = readline(">> ")) != nullptr) {
    if (strlen(buf) > 0) add_history(buf);

    printf("[%s]\n", buf);
    free(buf);
  }

  return 0;
}

The code runs OK, but the problem is with the backspace key. When I make a mistake with the input and trying to remove a character with the key, instead of removing the last character, it gives a space. Originally

>> Hellow

After hitting backspace, it gives me a space instead of removing w:

>> Hellow(there_is_a_space_here!) 

I'm working in bash using tmux and this strange behavior only appears when running this code with readline. Could anyone tell me how to fix this strange behavior of backspace?

UPDATE

The same behavior also appears when running the code on tcsh.

rnels12
  • 639
  • 6
  • 12

0 Answers0