zsh theme for full path + display git changes
Asked Answered
S

4

72

I'm looking for theme to display a full path + git (branch name + uncommitted changes + added files). Didn't find any. something like this:

/full/path/to/repo (master *+)

would love a recommendation of one / a tip of how to edit an existing one (I am currently using Godzilla).

Scanty answered 11/1, 2015 at 7:49 Comment(1)
Zsh itself does not have themes. Perhaps you meant oh-my-zsh? Correctly tagging the question would help bringing the right people to look at your question.Jetta
R
73

Run man zshmisc and search for the SIMPLE PROMPT ESCAPES section. This documents escapes which can be used to theme your prompt.

To get the full path path of your current working directory use %d.

I'm assuming you're using oh-my-zsh. In order to accomplish what you want, you can create a modified version of the Godzilla theme and replace the %c (which just shows the current folder) with %d in the PROMPT.

See here: https://github.com/robbyrussell/oh-my-zsh/blob/c78277fd8bda5fec87504469afdf121355876006/themes/gozilla.zsh-theme#L1

Runge answered 15/1, 2015 at 12:52 Comment(2)
Is there a way to make "/Users/myuser/Desktop" become "~/Desktop" when using %d instead of %c? Thankss!Leaven
You can use %~ (instead of %d) to show ~/Desktop/path/ instead of /Users/myuser/Desktop/path/. zsh.sourceforge.net/Doc/Release/Prompt-Expansion.htmlAfterthought
S
159

Creating a copy of original theme file is the recommended way of tweaking original theme files. Oh-my-zsh docs

Check your existing theme:

$ echo $ZSH_THEME
robbyrussell

Create a copy of original theme file in your $ZSH_CUSTOM/themes directory:

cp $ZSH/themes/robbyrussell.zsh-theme $ZSH_CUSTOM/themes/ 

Edit your custom copy of theme file

vim $ZSH_CUSTOM/themes/robbyrussell.zsh-theme

Which looks like this:

PROMPT+=' %{$fg[cyan]%}%c%{$reset_color%} $(git_prompt_info)'
#                       ^ replace c with ~

Then source theme again:

exec zsh

It will now show the path relative to your home directory (~). For example:

# BEFORE
➜  sqlboiler git:(master)
# AFTER
➜  ~/open-source/sqlboiler git:(master)
Saleable answered 20/10, 2020 at 8:54 Comment(6)
Instead of changing the default theme file, I would recommend copying the PROMT= lines to the end of ~/.zshrc before customizing themVolney
good point, I was satisfied with cp ~/.oh-my-zsh/themes/robbyrussell.zsh-theme ~/.oh-my-zsh/themes/robbyrussell.zsh-theme.origPhineas
This didn't work for me at all. i.imgur.com/3otsfsa.pngLueck
If your theme is not robbyrussell you will need to edit different theme file. You can find your current active theme in ~/.zshrc file under ZSH_THEME variableGrime
copy command, copy the file from ~/.oh-my-zsh/themes/robbyrussel.zsh-themes to ~/.oh-my-zsh/custom/themes/robbyrussel.zsh-theme and we edit this file then how does exec command read from /custom/themse location but not from /themes location ?Madson
Simplifying this answer, you can add this PROMPT=$(echo $PROMPT | sed 's/%c%/%~%/') to your .zshrc file!Snowdrop
R
73

Run man zshmisc and search for the SIMPLE PROMPT ESCAPES section. This documents escapes which can be used to theme your prompt.

To get the full path path of your current working directory use %d.

I'm assuming you're using oh-my-zsh. In order to accomplish what you want, you can create a modified version of the Godzilla theme and replace the %c (which just shows the current folder) with %d in the PROMPT.

See here: https://github.com/robbyrussell/oh-my-zsh/blob/c78277fd8bda5fec87504469afdf121355876006/themes/gozilla.zsh-theme#L1

Runge answered 15/1, 2015 at 12:52 Comment(2)
Is there a way to make "/Users/myuser/Desktop" become "~/Desktop" when using %d instead of %c? Thankss!Leaven
You can use %~ (instead of %d) to show ~/Desktop/path/ instead of /Users/myuser/Desktop/path/. zsh.sourceforge.net/Doc/Release/Prompt-Expansion.htmlAfterthought
K
18

Besides other answers,

If you also want to add the username and/or hostname, add the three lines below in the end of ~/.zshrc to overwrite the PROMPT:

PROMPT="%{$fg_bold[white]%}%n %{$fg[blue]%}@ %{$fg_bold[yellow]%}%m"

PROMPT+=" %(?:%{$fg_bold[green]%}➜ :%{$fg_bold[red]%}➜ )"

PROMPT+=' %{$fg[cyan]%}%d%{$reset_color%} $(git_prompt_info)'

Explanation:

%n is the username

%m is the hostname

%d is the directory (you can replace it with %~)

Note: Space between username and hostname is added in the example above is for clarity, you can remove them if you want.

Kallman answered 12/10, 2021 at 9:3 Comment(0)
G
9

Locate your current theme in ~/.zshrc file defined under variable named ZSH_THEME. You are supposed to edit the file according to the theme(current zsh theme) defined here. Default one is robbyrussell.

For default theme edit this file: ~/.oh-my-zsh/themes/robbyrussell.zsh-theme Find PROMPT variable definition in file(Note: You can find multiple variable declaration with concatenation, find the one that matches structure mentioned below). It should have something like below:

PROMPT+='%{$fg[blue]%}%~%{$reset_color%}

Modify the middle part of PROMPT variable as your preference from following to change prompt path. Example how path would look like for each setting is shown below:

  1. %~% -> shows: ~/example_dir/temp/hello_world
  2. %d% -> shows: /User/ydave/Desktop/example_dir/temp/hello_world
  3. %2d -> shows: temp/hello_world
  4. %3d -> shows: example_dir/temp/hello_world
  5. %c% -> shows: hello_world
Grime answered 5/1, 2023 at 18:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.