Add newline to prompt if it's too longbash prompt - long command circle back to same line after adding colorTerminal prompt not wrapping correctlyProblem with command promp cycling when using colors and trying to put git branches on the command promptHow can avoid these spurious characters in my bash prompt?How to wrap bash commands after adding colorHow can I undo an accidental newline in bash?How to automatically insert a string after the promptBash: conditional newline in PS1 breaks typeaheadmake zsh run a command when no command is entered
What should come first--characters or plot?
Why is 1. d4 Nf6 2. c4 e6 3. Bg5 almost never played?
"Sorry to bother you" in an email?
Localization at a multiplicative set is a localization at a prime ideal if local
Prevent use of CNAME Record for Untrusted Domain
How do I prevent other wifi networks from showing up on my computer?
Did the British navy fail to take into account the ballistics correction due to Coriolis force during WW1 Falkland Islands battle?
Disambiguation of "nobis vobis" and "nobis nobis"
Why do banks “park” their money at the European Central Bank?
Handling Disruptive Student on the Autistic Spectrum
Round towards zero
How to prevent clipped screen edges on my TV, HDMI-connected?
Transposing from C to Cm?
Why are non-collision-resistant hash functions considered insecure for signing self-generated information
Asymmetric table
How do I, an introvert, communicate to my friend and only colleague, an extrovert, that I want to spend my scheduled breaks without them?
Uri tokenizer as a simple state machine
Showing that the limit of non-eigenvector goes to infinity
Could George I (of Great Britain) speak English?
Can I get temporary health insurance while moving to the US?
How to gently end involvement with an online community?
Algorithms vs LP or MIP
Are the A380 engines interchangeable (given they are not all equipped with reverse)?
Are the players on the same team as the DM?
Add newline to prompt if it's too long
bash prompt - long command circle back to same line after adding colorTerminal prompt not wrapping correctlyProblem with command promp cycling when using colors and trying to put git branches on the command promptHow can avoid these spurious characters in my bash prompt?How to wrap bash commands after adding colorHow can I undo an accidental newline in bash?How to automatically insert a string after the promptBash: conditional newline in PS1 breaks typeaheadmake zsh run a command when no command is entered
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I show my full working directory plus other information (git, etc) in my bash prompt and sometimes it gets very long.
I want to add a newline to the end of my prompt so I can type the command on the next line, but only if the prompt is long e.g. more than 50 characters.
| ~ $ Typing a command here is nice |
| ~/foo/longDirectoryName/longsubirectory/src/package (master +*) $ Typing here s|
| ucks. I want to just start on a new line |
Obviously, If I wanted to always type my command on the next line, I could just add a newline to PS1
(as in this post). But I haven't found a way to do that conditionally because PS1
is just a format string.
P.S. I'm actually using ZSH trying to customize the Agnoster theme but I imagine any solution for bash in general would help.
bash terminal zsh prompt
New contributor
add a comment |
I show my full working directory plus other information (git, etc) in my bash prompt and sometimes it gets very long.
I want to add a newline to the end of my prompt so I can type the command on the next line, but only if the prompt is long e.g. more than 50 characters.
| ~ $ Typing a command here is nice |
| ~/foo/longDirectoryName/longsubirectory/src/package (master +*) $ Typing here s|
| ucks. I want to just start on a new line |
Obviously, If I wanted to always type my command on the next line, I could just add a newline to PS1
(as in this post). But I haven't found a way to do that conditionally because PS1
is just a format string.
P.S. I'm actually using ZSH trying to customize the Agnoster theme but I imagine any solution for bash in general would help.
bash terminal zsh prompt
New contributor
add a comment |
I show my full working directory plus other information (git, etc) in my bash prompt and sometimes it gets very long.
I want to add a newline to the end of my prompt so I can type the command on the next line, but only if the prompt is long e.g. more than 50 characters.
| ~ $ Typing a command here is nice |
| ~/foo/longDirectoryName/longsubirectory/src/package (master +*) $ Typing here s|
| ucks. I want to just start on a new line |
Obviously, If I wanted to always type my command on the next line, I could just add a newline to PS1
(as in this post). But I haven't found a way to do that conditionally because PS1
is just a format string.
P.S. I'm actually using ZSH trying to customize the Agnoster theme but I imagine any solution for bash in general would help.
bash terminal zsh prompt
New contributor
I show my full working directory plus other information (git, etc) in my bash prompt and sometimes it gets very long.
I want to add a newline to the end of my prompt so I can type the command on the next line, but only if the prompt is long e.g. more than 50 characters.
| ~ $ Typing a command here is nice |
| ~/foo/longDirectoryName/longsubirectory/src/package (master +*) $ Typing here s|
| ucks. I want to just start on a new line |
Obviously, If I wanted to always type my command on the next line, I could just add a newline to PS1
(as in this post). But I haven't found a way to do that conditionally because PS1
is just a format string.
P.S. I'm actually using ZSH trying to customize the Agnoster theme but I imagine any solution for bash in general would help.
bash terminal zsh prompt
bash terminal zsh prompt
New contributor
New contributor
edited 4 hours ago
mosvy
16.1k2 gold badges20 silver badges51 bronze badges
16.1k2 gold badges20 silver badges51 bronze badges
New contributor
asked 8 hours ago
B1CL0PSB1CL0PS
1083 bronze badges
1083 bronze badges
New contributor
New contributor
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
In zsh
, that's what the %<number>(l:<yes>:<no>)
prompt expansion is for. When the number is negative, like -30
, if there are at least 30 characters left until the right edge of the screen, then the yes
text is output, otherwise no
, so:
PS1=$'%~%-30(l::n)$ '
Would insert a newline if fewer than 28 characters (30 minus the "$ "
) are left for you to use on the line.
You can do your 50 or more with:
PS1=$'%~%50(l:n:)$ '
But IMO, it's more useful to guarantee a minimum available space, than a maximum unusable space.
See the manual for details. You'll find other directives to truncate long prompts and replace with ellipsis for instance which you may also find useful.
Note that zsh prompt expansion is completely different from that of bash
. It's actually closer to that of tcsh
, so solutions for bash
are unlikely to be of much use for zsh
, though it's generally more true the other way round.
This solved my problem perfectly. I ended up adding:bash newline_if_too_long() SEGMENT_SEPARATOR=$'ue0b0' local blue_arrow="%%Fblue%$SEGMENT_SEPARATOR%%f%" local blue_space="%%Fblack%%%Kblue% ↳ %%k%%%f%" echo -n "%-30(l::n$blue_space$blue_arrow)"
as a segment to my prompt builder
– B1CL0PS
4 hours ago
add a comment |
In newer versions of bash
, you can combine PROMPT_COMMAND
with the $var@P
form of variable substitution ("expand as if it were a prompt").
Here is a way to do that. You simply set RPS1
instead of PS1
to the prompt you want. The \-
escape inside RPS1
will act like a soft hyphen: it will expand to a newline when the prompt would be longer than COLUMNS / 2
; otherwise it will be removed:
generate_ps1()
local ps1=$RPS1@P
if [ "$#ps1" -gt "$((COLUMNS / 2))" ]; then PS1=$RPS1//\-/$'n'
else PS1=$RPS1//\-/
fi
RPS1=$PS1
PROMPT_COMMAND=generate_ps1
# after this, set RPS1 instead of PS1 to the prompt you want
$ RPS1='$long_var-$ '
$ long_var=$(printf 'foobar %.0s' 0..21)
foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foo
bar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar
$ _
$ long_var=$(printf 'foobar %.0s' 0..2)
foobar foobar foobar $ _
This could be improved to act like a real soft-hyphen, take into account the automatic margins of the terminal and already existing newlines inside the prompt, etc; but that will make more complex than it's worth.
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
B1CL0PS is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f537230%2fadd-newline-to-prompt-if-its-too-long%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
In zsh
, that's what the %<number>(l:<yes>:<no>)
prompt expansion is for. When the number is negative, like -30
, if there are at least 30 characters left until the right edge of the screen, then the yes
text is output, otherwise no
, so:
PS1=$'%~%-30(l::n)$ '
Would insert a newline if fewer than 28 characters (30 minus the "$ "
) are left for you to use on the line.
You can do your 50 or more with:
PS1=$'%~%50(l:n:)$ '
But IMO, it's more useful to guarantee a minimum available space, than a maximum unusable space.
See the manual for details. You'll find other directives to truncate long prompts and replace with ellipsis for instance which you may also find useful.
Note that zsh prompt expansion is completely different from that of bash
. It's actually closer to that of tcsh
, so solutions for bash
are unlikely to be of much use for zsh
, though it's generally more true the other way round.
This solved my problem perfectly. I ended up adding:bash newline_if_too_long() SEGMENT_SEPARATOR=$'ue0b0' local blue_arrow="%%Fblue%$SEGMENT_SEPARATOR%%f%" local blue_space="%%Fblack%%%Kblue% ↳ %%k%%%f%" echo -n "%-30(l::n$blue_space$blue_arrow)"
as a segment to my prompt builder
– B1CL0PS
4 hours ago
add a comment |
In zsh
, that's what the %<number>(l:<yes>:<no>)
prompt expansion is for. When the number is negative, like -30
, if there are at least 30 characters left until the right edge of the screen, then the yes
text is output, otherwise no
, so:
PS1=$'%~%-30(l::n)$ '
Would insert a newline if fewer than 28 characters (30 minus the "$ "
) are left for you to use on the line.
You can do your 50 or more with:
PS1=$'%~%50(l:n:)$ '
But IMO, it's more useful to guarantee a minimum available space, than a maximum unusable space.
See the manual for details. You'll find other directives to truncate long prompts and replace with ellipsis for instance which you may also find useful.
Note that zsh prompt expansion is completely different from that of bash
. It's actually closer to that of tcsh
, so solutions for bash
are unlikely to be of much use for zsh
, though it's generally more true the other way round.
This solved my problem perfectly. I ended up adding:bash newline_if_too_long() SEGMENT_SEPARATOR=$'ue0b0' local blue_arrow="%%Fblue%$SEGMENT_SEPARATOR%%f%" local blue_space="%%Fblack%%%Kblue% ↳ %%k%%%f%" echo -n "%-30(l::n$blue_space$blue_arrow)"
as a segment to my prompt builder
– B1CL0PS
4 hours ago
add a comment |
In zsh
, that's what the %<number>(l:<yes>:<no>)
prompt expansion is for. When the number is negative, like -30
, if there are at least 30 characters left until the right edge of the screen, then the yes
text is output, otherwise no
, so:
PS1=$'%~%-30(l::n)$ '
Would insert a newline if fewer than 28 characters (30 minus the "$ "
) are left for you to use on the line.
You can do your 50 or more with:
PS1=$'%~%50(l:n:)$ '
But IMO, it's more useful to guarantee a minimum available space, than a maximum unusable space.
See the manual for details. You'll find other directives to truncate long prompts and replace with ellipsis for instance which you may also find useful.
Note that zsh prompt expansion is completely different from that of bash
. It's actually closer to that of tcsh
, so solutions for bash
are unlikely to be of much use for zsh
, though it's generally more true the other way round.
In zsh
, that's what the %<number>(l:<yes>:<no>)
prompt expansion is for. When the number is negative, like -30
, if there are at least 30 characters left until the right edge of the screen, then the yes
text is output, otherwise no
, so:
PS1=$'%~%-30(l::n)$ '
Would insert a newline if fewer than 28 characters (30 minus the "$ "
) are left for you to use on the line.
You can do your 50 or more with:
PS1=$'%~%50(l:n:)$ '
But IMO, it's more useful to guarantee a minimum available space, than a maximum unusable space.
See the manual for details. You'll find other directives to truncate long prompts and replace with ellipsis for instance which you may also find useful.
Note that zsh prompt expansion is completely different from that of bash
. It's actually closer to that of tcsh
, so solutions for bash
are unlikely to be of much use for zsh
, though it's generally more true the other way round.
edited 4 hours ago
answered 5 hours ago
Stéphane ChazelasStéphane Chazelas
331k58 gold badges646 silver badges1015 bronze badges
331k58 gold badges646 silver badges1015 bronze badges
This solved my problem perfectly. I ended up adding:bash newline_if_too_long() SEGMENT_SEPARATOR=$'ue0b0' local blue_arrow="%%Fblue%$SEGMENT_SEPARATOR%%f%" local blue_space="%%Fblack%%%Kblue% ↳ %%k%%%f%" echo -n "%-30(l::n$blue_space$blue_arrow)"
as a segment to my prompt builder
– B1CL0PS
4 hours ago
add a comment |
This solved my problem perfectly. I ended up adding:bash newline_if_too_long() SEGMENT_SEPARATOR=$'ue0b0' local blue_arrow="%%Fblue%$SEGMENT_SEPARATOR%%f%" local blue_space="%%Fblack%%%Kblue% ↳ %%k%%%f%" echo -n "%-30(l::n$blue_space$blue_arrow)"
as a segment to my prompt builder
– B1CL0PS
4 hours ago
This solved my problem perfectly. I ended up adding:
bash newline_if_too_long() SEGMENT_SEPARATOR=$'ue0b0' local blue_arrow="%%Fblue%$SEGMENT_SEPARATOR%%f%" local blue_space="%%Fblack%%%Kblue% ↳ %%k%%%f%" echo -n "%-30(l::n$blue_space$blue_arrow)"
as a segment to my prompt builder– B1CL0PS
4 hours ago
This solved my problem perfectly. I ended up adding:
bash newline_if_too_long() SEGMENT_SEPARATOR=$'ue0b0' local blue_arrow="%%Fblue%$SEGMENT_SEPARATOR%%f%" local blue_space="%%Fblack%%%Kblue% ↳ %%k%%%f%" echo -n "%-30(l::n$blue_space$blue_arrow)"
as a segment to my prompt builder– B1CL0PS
4 hours ago
add a comment |
In newer versions of bash
, you can combine PROMPT_COMMAND
with the $var@P
form of variable substitution ("expand as if it were a prompt").
Here is a way to do that. You simply set RPS1
instead of PS1
to the prompt you want. The \-
escape inside RPS1
will act like a soft hyphen: it will expand to a newline when the prompt would be longer than COLUMNS / 2
; otherwise it will be removed:
generate_ps1()
local ps1=$RPS1@P
if [ "$#ps1" -gt "$((COLUMNS / 2))" ]; then PS1=$RPS1//\-/$'n'
else PS1=$RPS1//\-/
fi
RPS1=$PS1
PROMPT_COMMAND=generate_ps1
# after this, set RPS1 instead of PS1 to the prompt you want
$ RPS1='$long_var-$ '
$ long_var=$(printf 'foobar %.0s' 0..21)
foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foo
bar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar
$ _
$ long_var=$(printf 'foobar %.0s' 0..2)
foobar foobar foobar $ _
This could be improved to act like a real soft-hyphen, take into account the automatic margins of the terminal and already existing newlines inside the prompt, etc; but that will make more complex than it's worth.
add a comment |
In newer versions of bash
, you can combine PROMPT_COMMAND
with the $var@P
form of variable substitution ("expand as if it were a prompt").
Here is a way to do that. You simply set RPS1
instead of PS1
to the prompt you want. The \-
escape inside RPS1
will act like a soft hyphen: it will expand to a newline when the prompt would be longer than COLUMNS / 2
; otherwise it will be removed:
generate_ps1()
local ps1=$RPS1@P
if [ "$#ps1" -gt "$((COLUMNS / 2))" ]; then PS1=$RPS1//\-/$'n'
else PS1=$RPS1//\-/
fi
RPS1=$PS1
PROMPT_COMMAND=generate_ps1
# after this, set RPS1 instead of PS1 to the prompt you want
$ RPS1='$long_var-$ '
$ long_var=$(printf 'foobar %.0s' 0..21)
foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foo
bar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar
$ _
$ long_var=$(printf 'foobar %.0s' 0..2)
foobar foobar foobar $ _
This could be improved to act like a real soft-hyphen, take into account the automatic margins of the terminal and already existing newlines inside the prompt, etc; but that will make more complex than it's worth.
add a comment |
In newer versions of bash
, you can combine PROMPT_COMMAND
with the $var@P
form of variable substitution ("expand as if it were a prompt").
Here is a way to do that. You simply set RPS1
instead of PS1
to the prompt you want. The \-
escape inside RPS1
will act like a soft hyphen: it will expand to a newline when the prompt would be longer than COLUMNS / 2
; otherwise it will be removed:
generate_ps1()
local ps1=$RPS1@P
if [ "$#ps1" -gt "$((COLUMNS / 2))" ]; then PS1=$RPS1//\-/$'n'
else PS1=$RPS1//\-/
fi
RPS1=$PS1
PROMPT_COMMAND=generate_ps1
# after this, set RPS1 instead of PS1 to the prompt you want
$ RPS1='$long_var-$ '
$ long_var=$(printf 'foobar %.0s' 0..21)
foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foo
bar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar
$ _
$ long_var=$(printf 'foobar %.0s' 0..2)
foobar foobar foobar $ _
This could be improved to act like a real soft-hyphen, take into account the automatic margins of the terminal and already existing newlines inside the prompt, etc; but that will make more complex than it's worth.
In newer versions of bash
, you can combine PROMPT_COMMAND
with the $var@P
form of variable substitution ("expand as if it were a prompt").
Here is a way to do that. You simply set RPS1
instead of PS1
to the prompt you want. The \-
escape inside RPS1
will act like a soft hyphen: it will expand to a newline when the prompt would be longer than COLUMNS / 2
; otherwise it will be removed:
generate_ps1()
local ps1=$RPS1@P
if [ "$#ps1" -gt "$((COLUMNS / 2))" ]; then PS1=$RPS1//\-/$'n'
else PS1=$RPS1//\-/
fi
RPS1=$PS1
PROMPT_COMMAND=generate_ps1
# after this, set RPS1 instead of PS1 to the prompt you want
$ RPS1='$long_var-$ '
$ long_var=$(printf 'foobar %.0s' 0..21)
foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar foo
bar foobar foobar foobar foobar foobar foobar foobar foobar foobar foobar
$ _
$ long_var=$(printf 'foobar %.0s' 0..2)
foobar foobar foobar $ _
This could be improved to act like a real soft-hyphen, take into account the automatic margins of the terminal and already existing newlines inside the prompt, etc; but that will make more complex than it's worth.
edited 1 hour ago
answered 6 hours ago
mosvymosvy
16.1k2 gold badges20 silver badges51 bronze badges
16.1k2 gold badges20 silver badges51 bronze badges
add a comment |
add a comment |
B1CL0PS is a new contributor. Be nice, and check out our Code of Conduct.
B1CL0PS is a new contributor. Be nice, and check out our Code of Conduct.
B1CL0PS is a new contributor. Be nice, and check out our Code of Conduct.
B1CL0PS is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f537230%2fadd-newline-to-prompt-if-its-too-long%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown