You are not logged in.

#1 2012-09-14 08:01:09

hiveNzin0
Member
Registered: 2011-10-02
Posts: 69

[SOLVED][zsh] modify PROMPT, problem with a variable

Good morning,

I wanted to try zsh for a while and I finally installed it. I used bash for a long time.

In my .bashrc, I modified to variable PS1 to display something like this :

user@host current_directory size_of_the_directory $

Unfortunately with zsh, I cannot do it anymore.

Here is the command I use to get the size of my directory :

ls | grep -m 1 total | sed 's/total //'

This returns something like 256K, 40G, ...

With zsh, in the PROMPT variable, I cannot find how to execute that, it just doesn't work, I tried with $(), `the command`, ...

I looked in the documentation and I found that 2 functions hooks are executed when a prompt is created or a directory changes.
These functions are precmd() and chgpwd(). precmd is executed everytime a prompt is displayed and chgpwd is executed everytime I change my current directory.

I did something like that then :

precmd
{
var=`ls | grep -m 1 total | sed 's/total //'`
}

chgpwd
{
var=`ls | grep -m 1 total | sed 's/total //'`
}

PROMPT="some blabla [${var}]"

If I "echo" my variable var in the functions, it works fine, the variable always has the correct value which is the folder size.

Unfortunately, the variable in the PROMPT never changes. To change it, I have to source again my zshrc.

Does somebody know what I am doing wrong ?

Thank you very much.

Last edited by hiveNzin0 (2012-09-18 06:21:29)

Offline

#2 2012-09-14 10:54:43

Trilby
Forum Moderator
From: Massachusetts, USA
Registered: 2011-11-29
Posts: 14,001
Website

Re: [SOLVED][zsh] modify PROMPT, problem with a variable

I don't use ZSH, but what you describe is what I would expect.  The [${var}] in PROMPT is expanded when the zshrc is read.  You need to reset PROMPT each time it changes.

Can you set the PROMPT variable in precmd or chgpwd instead of using the var middleman?

EDIT: if you want, you can also get rid of grep:

ls -l | sed '1 s/total //;q'

Last edited by Trilby (2012-09-14 11:08:18)


InterrobangSlider
• How's my coding? See this page.
• How's my moderating? Feel free to email any concerns, complaints, or objections.

Offline

#3 2012-09-14 10:57:00

hiveNzin0
Member
Registered: 2011-10-02
Posts: 69

Re: [SOLVED][zsh] modify PROMPT, problem with a variable

Trilby wrote:

Can you set the PROMPT variable in precmd or chgpwd instead of using the var middleman?

Yeah I suppose I can, I will try that instead, it should work.

I will confirm when I can test that. smile

Thanks.

Offline

#4 2012-09-14 17:00:22

Trent
Member
From: Baltimore, MD (US)
Registered: 2009-04-16
Posts: 987

Re: [SOLVED][zsh] modify PROMPT, problem with a variable

Trilby wrote:

I don't use ZSH, but what you describe is what I would expect.  The [${var}] in PROMPT is expanded when the zshrc is read.

This would be the same for bash, no? Curious how OP did it to begin with...

Offline

#5 2012-09-17 14:56:26

hiveNzin0
Member
Registered: 2011-10-02
Posts: 69

Re: [SOLVED][zsh] modify PROMPT, problem with a variable

Trent wrote:
Trilby wrote:

I don't use ZSH, but what you describe is what I would expect.  The [${var}] in PROMPT is expanded when the zshrc is read.

This would be the same for bash, no? Curious how OP did it to begin with...

Hi,

here is my PS1 in bash :

export PS1="[\[\033[1;34m\]\u@\h\[\e[0m\] \[\033[1;31m\]\w\[\e[0m\] \[\033[32m\]\$(/bin/ls -lah | /bin/grep -m 1 total | /bin/sed 's/total //')\[\e[0m\]]\\ $ "

I didn't have time to try on zsh. I will do that tonight and tell the results.

Offline

#6 2012-09-17 19:11:45

hiveNzin0
Member
Registered: 2011-10-02
Posts: 69

Re: [SOLVED][zsh] modify PROMPT, problem with a variable

Okay, it works.

Here is my "final" modifications unless something should be changed. Thank you again.

# prompt stuff
function getFolderSize
{
   folderSize="`ls | grep -m 1 total | sed 's/total//'`"
}

function updatePrompt
{
   getFolderSize
   PROMPT="[%*] %{$fg[cyan]%}%n%{$reset_color%}@%{$fg[green]%}%m%{$reset_color%} %~${folderSize} $ "
}

function precmd
{
   updatePrompt   
}

function chpwd
{
   updatePrompt   
}

Offline

#7 2012-09-17 20:47:27

Trent
Member
From: Baltimore, MD (US)
Registered: 2009-04-16
Posts: 987

Re: [SOLVED][zsh] modify PROMPT, problem with a variable

I got curious and poked through the manpages for a while. Turns out you can `setopt PROMPT_SUBST` to perform parameter expansion and command substitution on the prompt a la bash.

Offline

Board footer

Powered by FluxBB