Mimic zsh"s right prompt in bash

Mimic zsh"s right prompt in bash

am 31.05.2007 19:25:17 von Thorsten Kampe

Hi,

How can I mimic a typical zsh prompt with the current directory at the
right in bash?

My zsh prompt is:
user@host% /path/to/pwd

How can I mimic the right hand side in bash with PS1?


Thorsten

Re: Mimic zsh"s right prompt in bash

am 31.05.2007 20:02:46 von Thorsten Kampe

* Thorsten Kampe (Thu, 31 May 2007 18:25:17 +0100)
> How can I mimic a typical zsh prompt with the current directory at the
> right in bash?
>
> My zsh prompt is:
> user@host% /path/to/pwd
>
> How can I mimic the right hand side in bash with PS1?

Oops, that's already been asked in May. I'll read through the
thread...

Re: Mimic zsh"s right prompt in bash

am 31.05.2007 20:17:08 von Thorsten Kampe

* Thorsten Kampe (Thu, 31 May 2007 19:02:46 +0100)
> * Thorsten Kampe (Thu, 31 May 2007 18:25:17 +0100)
> > How can I mimic a typical zsh prompt with the current directory at the
> > right in bash?
> >
> > My zsh prompt is:
> > user@host% /path/to/pwd
> >
> > How can I mimic the right hand side in bash with PS1?
>
> Oops, that's already been asked in May. I'll read through the
> thread...

Hm, interesting thread but no answer given. I /guess/ it should be
possible to mimic the RPS1 in bash by extending the PS1 prompt to the
right.

Just to give a hint how this could be achieved in a shell that doesn't
have its own right hand side prompt:

In 4NT (Windows Cmd enhancement and replacement) you could do:

set prompt=`%_winuser@%_host%$g$s$e[s$e[%@inc[%_row];%@eval[%_co lumns
- %@len[%_cwd]]H$p$e[u`


Thorsten

Re: Mimic zsh"s right prompt in bash

am 31.05.2007 21:06:50 von Stephane CHAZELAS

2007-05-31, 19:17(+01), Thorsten Kampe:
> * Thorsten Kampe (Thu, 31 May 2007 19:02:46 +0100)
>> * Thorsten Kampe (Thu, 31 May 2007 18:25:17 +0100)
>> > How can I mimic a typical zsh prompt with the current directory at the
>> > right in bash?
>> >
>> > My zsh prompt is:
>> > user@host% /path/to/pwd
>> >
>> > How can I mimic the right hand side in bash with PS1?
>>
>> Oops, that's already been asked in May. I'll read through the
>> thread...
>
> Hm, interesting thread but no answer given. I /guess/ it should be
> possible to mimic the RPS1 in bash by extending the PS1 prompt to the
> right.
[...]

Why don't you use zsh in the first place?

You want to try things like:

PROMPT_COMMAND='printf "%*s\r" "$COLUMNS" "$PWD"'

In anycase, this is bound not to always work, as you need the
shell to be aware that there are characters at the right of the
cursor.

--
Stéphane

Re: Mimic zsh"s right prompt in bash

am 31.05.2007 21:49:59 von Thorsten Kampe

* Stephane CHAZELAS (Thu, 31 May 2007 19:06:50 GMT)
> 2007-05-31, 19:17(+01), Thorsten Kampe:
> > * Thorsten Kampe (Thu, 31 May 2007 19:02:46 +0100)
> >> * Thorsten Kampe (Thu, 31 May 2007 18:25:17 +0100)
> >> > How can I mimic a typical zsh prompt with the current directory at the
> >> > right in bash?
> >> >
> >> > My zsh prompt is:
> >> > user@host% /path/to/pwd
> >> >
> >> > How can I mimic the right hand side in bash with PS1?
> >>
> >> Oops, that's already been asked in May. I'll read through the
> >> thread...
> >
> > Hm, interesting thread but no answer given. I /guess/ it should be
> > possible to mimic the RPS1 in bash by extending the PS1 prompt to the
> > right.
> [...]
>
> Why don't you use zsh in the first place?

I do to 95%, I swear. But in some environments people haven't seen the
light yet and I have to use what's available there.

> You want to try things like:
>
> PROMPT_COMMAND='printf "%*s\r" "$COLUMNS" "$PWD"'
>
> In anycase, this is bound not to always work, as you need the
> shell to be aware that there are characters at the right of the
> cursor.

I'll try this.

Thorsten

Re: Mimic zsh"s right prompt in bash

am 31.05.2007 22:04:28 von Thorsten Kampe

* Stephane CHAZELAS (Thu, 31 May 2007 19:06:50 GMT)
> 2007-05-31, 19:17(+01), Thorsten Kampe:
> > * Thorsten Kampe (Thu, 31 May 2007 19:02:46 +0100)
> >> * Thorsten Kampe (Thu, 31 May 2007 18:25:17 +0100)
> >> > How can I mimic a typical zsh prompt with the current directory at the
> >> > right in bash?
> >> >
> >> > My zsh prompt is:
> >> > user@host% /path/to/pwd
> >> >
> >> > How can I mimic the right hand side in bash with PS1?
> >>
> >> Oops, that's already been asked in May. I'll read through the
> >> thread...
> >
> > Hm, interesting thread but no answer given. I /guess/ it should be
> > possible to mimic the RPS1 in bash by extending the PS1 prompt to the
> > right.
>
> You want to try things like:
>
> PROMPT_COMMAND='printf "%*s\r" "$COLUMNS" "$PWD"'

That's pretty clever but unfortunately the PROMPT_COMMAND uses an
additional line - it doesn't display on the same line as PS1. This is
the same effect as having a two line prompt.

To explain: I started with a two line prompt like many people:

/path/to/pwd
user@host$

....but then I noticed that half of my screen is always only used for
the pwd prompt. Then I switched to

user@host[$basename]$

That's better but now I have only the last component of the path in
the prompt and the prompt length still changes with the directory
(okay, of course not as much as if I would put the whole path in the
brackets).

Zsh's solution to this command dilemma is brilliant: put the path to
the space that is hardly ever used: the space on the right.

Thorsten

Re: Mimic zsh"s right prompt in bash

am 01.06.2007 10:49:35 von Stephane CHAZELAS

2007-05-31, 21:04(+01), Thorsten Kampe:
[...]
>> PROMPT_COMMAND='printf "%*s\r" "$COLUMNS" "$PWD"'
>
> That's pretty clever but unfortunately the PROMPT_COMMAND uses an
> additional line - it doesn't display on the same line as PS1. This is
> the same effect as having a two line prompt.

Works for me.

>
> To explain: I started with a two line prompt like many people:
>
> /path/to/pwd
> user@host$

Personaly, I have

PS1='%(?..%B(%?%)%b)${jobstates:+%S$#jobstates%s}%60<...<${STY[1,0]-"%{$fg_bold[magenta]%}%m%{$reset_color%}:"}%~%(#.#.$) '
RPS1='%30<...<${ZFTP_PWD:-%T}%S${ZFTP_HOST:+×}%s'

That is:

if running within screen I have:

~/bin$
or
~/bin# if I'm root (that is alsmost never as I tend to use sudo)

so no need to have the user in the prompt.

(1)~/bin$

with (1) in bold if the last command failed (1 being the exit
status).

~/bin4$

4 in reverse video, being the number of jobs in background.

If I don't run in screen (which never happens and thus means one
of my screen windows runs a rlogin or ssh to some remote host),
then the prompt also includes the host name in bold magenta (as
a reminder that I'm rloggedin).

I have the screen status line in the xterm window. That includes
the hostname and username and date and time, and the currently
run command (and window number), so all those informations don't
need to be in the prompt.

in ~/.screenrc:

hardstatus string '%H - %D %d %M - %c - %h (%n)'
termcapinfo xterm* 'hs:ts=\E]2;:fs=\007:ds=\E]0;xterm\007'

and in ~/.zshrc:
if (( $+STY )); then
preexec() {
local -a words
words=("${(@Q)${(z)1}}")
printf '\033]0;%s\a' " ${${${(qqqq)words[1,2]}#??}%?}${words[3]+...}";
}
precmd() { printf '\033]0;zsh\a'; }
fi

--
Stéphane