How to show cmd at each ZSH shell line?

9/11/2017

I have this function in my zsh configuration :

local kube_prompt() 
{
   kubectl_current_context=$(kubectl config current-context)
   kubectl_cluster=$(echo $kubectl_current_context | cut -d '_' -f 4)
   kubectl_prompt="⎈ ($kubectl_cluster)"
   echo $kubectl_prompt
}

And I call it by this way :

%{$fg[white]%}$(kube_prompt) \
${git_info} \

It work perfectly (I have my output on my shell prompt) but my problem is,

I want this command to be launch at each shell line (when I press ENTER for example) and not only when I open a new shell.

Any idea how to achieve that ?

Thanks !!!

-- Valentin Ouvrard
kubernetes
shell
zsh

1 Answer

9/11/2017

You can add a delay to the evaluation of the function by adding single quotes around it.

i.e. your PROMPT should be like PROMPT='%{$fg[white]%}$(kube_prompt) ${git_info}'

This will delay updating of the variable long enough so that effect of the command that you run is captured.

This is how I have configured it in my .zshrc:

kube_prompt()
{
   kubectl_current_context=$(kubectl config current-context)
   kubectl_prompt="( \u2388 $kubectl_current_context )"
   echo $kubectl_prompt
}

RPROMPT='%F{81}$(kube_prompt)'

And this is how it looks like:

$ kubectl config use-context chip24                                                                                      ( ⎈ minikube )
 Switched to context "chip24".
$ kubectl config use-context hasura                                                                                        ( ⎈ chip24 )
 Switched to context "hasura".
$                                                                                                                          ( ⎈ hasura )

The unicode character ⎈ adds a nice k8s logo effect to it. :)

-- Shahidh
Source: StackOverflow