0

I am having trouble finding a succinct answer to this. I am using Oh My Zsh, and right now using the default theme, robbyrussell.

I would like my prompt to have 2 components:

  1. The current directory, plus one level up
  2. If I am using a virtual environment (like anaconda), have the name of the active environment in parentheses.

1 Answer 1

2
+50

Below is the way to do it, you can customize it as per your needs

# Helper method to add background and foreground colors prompt_segment () { local bg fg [[ -n $1 ]] && bg="%K{$1}" || bg="%k" [[ -n $2 ]] && fg="%F{$2}" || fg="%f" if [[ $CURRENT_BG != 'NONE' && $1 != $CURRENT_BG ]] then echo -n " %{$bg%F{$CURRENT_BG}%}$SEGMENT_SEPARATOR%{$fg%} " else echo -n "%{$bg%}%{$fg%} " fi CURRENT_BG=$1 [[ -n $3 ]] && echo -n $3 } prompt_virtualenv () { # Check if we are in a virtual environment # if we are then VIRTUAL_ENV variable will be set local virtualenv_path="$VIRTUAL_ENV" if [[ -n $virtualenv_path && -n $VIRTUAL_ENV_DISABLE_PROMPT ]] then # We are in virtual env so show just the project name prompt_segment blue black "(`basename $virtualenv_path`)" fi } prompt_directory() { # Show the current directory prompt_segment red blue $PWD } build_my_zsh_prompt() { # Call all the prompt functions to build the actual prompt prompt_virtualenv prompt_directory prompt_segment black white "" } # Assign the PROMPT variable with the function, so bash call it everytime # Single quotes are important here, else you will get a fixed PROMPT # Without single quotes, the function will be called once and evaluated value # will be assigned PROMPT='$(build_my_zsh_prompt)' 

PROMPT variable is used by zsh shell to determine what needs to be displayed as the prompt. When we set PROMPT=$(build_my_zsh_prompt), we are asking shell to call our function build_my_zsh_prompt.

This function in turn (ideally) should call different function which create individual parts of prompt. Now let's look at prompt_directory

prompt_segment black red $PWD" 

The prompt_segment is a helper function to echo some text with background and the foreground color

First parameter black is the background and second parameter red is the foreground color in this case. Next we show what text needs to be given for this prompt.

All of this needs to be added to your ~/.zshrc file at the very end

Sign up to request clarification or add additional context in comments.

8 Comments

Could you please add comments so I understand what's going on here? Also, what file do I put this in?
@Adam_G, updated the answer with more details. Please check now
Thanks! I'll give it a try in the morning!
This raises the following error: prompt_parent_directory:1: command not found: prompt_segment prompt_directory:1: command not found: prompt_segment build_my_zsh_prompt:4: command not found: prompt_segment which makes sense. What should prompt_segment() say?
Ok, so this raises another issue. I can't get a prompt in my terminal, just the error above. How can I edit the file?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.