francium
francium

Reputation: 2480

Bash User Prompt Default Input Text

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

Upvotes: 0

Views: 236

Answers (1)

chepner
chepner

Reputation: 531165

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.

Upvotes: 2

Related Questions