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
3
  • 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. Commented Mar 24, 2016 at 3:28
  • 1
    Question: do you expect the semi-entered command to disappear if the user repeatedly press backspace? Commented Mar 24, 2016 at 3: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 Commented Mar 24, 2016 at 4:06

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.