0

Is it possible to add text, as if the user typed something, to the current bash user input prompt (not the information display prompt, input after the $ or #).

For example, (stupid example)

Say I want a command that adds 'sudo' to the next prompt after pressing enter

user@computer ~$ runAsSudo # ENTER PRESSED
user@computer ~$ sudo █    # █ is the cursor

My real application for this is having a command that undoes something, for example a copy, but instead of running it or asking for y/n, it simply adds it to the input prompt where the user can do whatever.

user@computer ~$ cp myFile /some/long/path/      # ENTER PRESSED
user@computer ~$ undo                            # ENTER PRESSED
user@computer ~$ cp /some/long/path/ myfile █    # █ is the cursor
francium
  • 2,384
  • 3
  • 20
  • 29
  • I don't believe you can do this. You could push a history entry with the reversed command which would let them use `!!` to run it but that's all I can think of that isn't a prompt or just running it. – Etan Reisner Mar 24 '16 at 03:28
  • 1
    Question: do you expect the semi-entered command to disappear if the user repeatedly press backspace? – mauro Mar 24 '16 at 03:32
  • @mauro No really a requirement, doesnt really matter. Edit. Rereading your question, my goal is to have the semi command be regular text, as if it was pasted or typed in by user, so yes i was hoping it woud behave as regular text – francium Mar 24 '16 at 04:06

1 Answers1

2

The Readline library doesn't have this functionality (although for reference, you might want to check out zsh, whose built-in line editor does have it). In this case, though, you can use history expansion in place of your undo command. For instance, typing !!, then Meta-^ (usually Alt-^, but possibly Esc-^, depending on your setup) will expand the history expansion without actually executing the command, letting the user edit the command.

chepner
  • 497,756
  • 71
  • 530
  • 681