We all know this annoying issue:
$ printf "abc" > some-file
$ cat some-file
abc$
which tends to mess up the prompt if it is any more complex than $
, throws off the current cursor position and looks just plain ugly. There are some solutions (e.g. here), but they have another drawback: Logging into a slow machine over ssh, you may start to type before the prompt appears. Normally, your input is then buffered and displayed nicely as soon as possible. However, with the linked solution, the input is discarded.
How do I
- always start the prompt on a new line, esp. if the output of the last command did not end with a new line and
- keep unconsumed input already entered during the execution of the previous command in the command line buffer?
Best Answer
This solution slightly combines the solution from the question with a small perl snippet:
The following code in
.bashrc
defines two new functions,inject
andclear_newline
. The latter is then called every time a prompt has to be printed. The former is called from within the latter (see inline commands for the details):The second
stty -echo/stty echo
pair is necessary to hide the input injected; it will be printed by bash as soon as the prompt finishes.