Skip to content

Instantly share code, notes, and snippets.

@fdevibe
Created May 1, 2025 15:08
Show Gist options
  • Save fdevibe/1a19a3a47b0a58d46aca0bb7db18968d to your computer and use it in GitHub Desktop.
Save fdevibe/1a19a3a47b0a58d46aca0bb7db18968d to your computer and use it in GitHub Desktop.
Add newline to bash prompt if the last output didn't end with one
# This won't work in PS1, as inside PS1, newlines
# in command substitutions are handled specially.
# Hence, using PROMPT_COMMAND instead.
__conditional_newline() {
IFS='[;' read -rsd R -p $'\e[6n' _ _ col
if [ -n "$col" ] && [ "$col" -gt 1 ]; then
echo -e "\033[01;31m\\missing newline\033[00m"
fi
}
__add_to_prompt_command() {
local new_cmd="$1"
if [[ "$PROMPT_COMMAND" != *"$new_cmd"* ]]; then
if [ -n "$PROMPT_COMMAND" ]; then
PROMPT_COMMAND="$new_cmd; $PROMPT_COMMAND"
else
PROMPT_COMMAND="$new_cmd"
fi
fi
}
__add_to_prompt_command __conditional_newline
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment