With all fancy complexity directory naming configuration
POWERLEVEL9K_SHORTEN_DIR_LENGTH
POWERLEVEL9K_SHORTEN_DELIMITER
POWERLEVEL9K_SHORTEN_STRATEGY
etc, I couldn't find any combination to show only the last dir name offered by the simple %1~
Eg.
~/Documents/I_symply_want_to_show_the_last_name/movies/last_name
would become
last_name
Well. More or less:
POWERLEVEL9K_SHORTEN_DIR_LENGTH=1
POWERLEVEL9K_SHORTEN_STRATEGY=truncate_folders
And, if you want to get rid of the abbreviation:
POWERLEVEL9K_SHORTEN_DELIMITER=""
To pick up your example:
~/Documents/I_symply_want_to_show_the_last_name/movies/last_name would become …/last_name, and if you set the delimiter to blank, you'll get /last_name.
Hmm, nice. If I also add POWERLEVEL9K_DIR_OMIT_FIRST_CHARACTER=true I get almost what I want. The problem is, if the direcotry contains only one char, as in the case when I'm at home dir, the omit char option will disappear with the ~
As in you want to also remove the leading / from the filename?
Hm, don't think we actually have an option to do that, honestly. It would be fairly easy to modify the code for this, if you were so inclined, but I don't think we have a way to only show the parent dirname.
Am I correct on that, @dritter? If so, this option would be a fairly easy feature to add since there isn't much logic to it.
I want an exactly behaviour as in %1~. I had indded modified powerlevel9k.zsh-theme line 623 from local current_path="$(print -P "%~")" to
local current_path="$(print -P "%1~")"
since it was the only way I found to achieve this. Maybe you guys could add an option to this :)
As said, there is no easy way to achieve exactly the same as %1~..
But I found a hacky way, without changing the code in the theme..

The Idea is to override the prompt_dir function, and set POWERLEVEL9K_DIR_OMIT_FIRST_CHARACTER dynamically.
# Backup original prompt_dir function to _prompt_dir
eval "`declare -f prompt_dir | sed '1s/.*/_&/'`"
prompt_dir() {
# Never omit first character
POWERLEVEL9K_DIR_OMIT_FIRST_CHARACTER=false
# Except if current directory is longer than one character
local currentDirectory=$(print -P "%~")
[[ ${#currentDirectory} -gt 1 ]] && POWERLEVEL9K_DIR_OMIT_FIRST_CHARACTER=true
# Call original prompt_dir function
_prompt_dir "${1}" "${2}"
}
That code must be after you source P9K. I put it in my ~/.zshrc at the very end.
@bhilburn I agree, we should put such an option into the theme. Maybe we could give POWERLEVEL9K_DIR_OMIT_FIRST_CHARACTER more logic. ;)
@dritter - Nice hack! Very clever :)
@FreddieOliveira - Does @dritter's hack work for you?
I'm going to leave this issue open as a tracking item for the enhancement. I agree with @dritter - this would be a good thing to add to the OMIT_FIRST_CHARACTER logic.
Thanks for the attention guys. Now I've got exactly what I wanted :+1: The hack works perfectly and is an ugly but functional replacement for the %1~
: P
@dritter With the hack above, what would be the best way to change the color of the foreground?
After using the workaround, the foreground went into the default black text despite having the code below in my .zshrc
POWERLEVEL9K_DIR_HOME_FOREGROUND="white"
POWERLEVEL9K_DIR_HOME_SUBFOLDER_FOREGROUND="white"
POWERLEVEL9K_DIR_DEFAULT_FOREGROUND="white"
@faewin Sorry, just stumbled upon your comment.
The answer lies in how we generate that color overwrite variable names. In our very basic *_prompt_segment functions we take the name of the actual segment and substitute prompt_ (this is how we name the segments internally prompt_dir, prompt_vcs, etc) from the name and check if a back or foreground variable exists. So, now that we redeclared our prompt_dir function to _prompt_dir (note the underscore in the beginning), this mechanism does not work any more. The same applies to other variable names as well (like *_VISUAL_IDENTIFIER).
Disclaimer for both solutions: The prompt_dir function checks internally for some variables. Here the name is hardcoded. So, if you use these variables, they do not get renamed.
Short, but ugly: Rename your variables as if the segment name was never stripped. Rename your variables to POWERLEVEL9K__PROMPT_DIR_HOME_FOREGROUND="white" (double underscore here).
Btw. if you want to set all segments to white foreground color, you might try setting POWERLEVEL9K_COLOR_SCHEME='light'. That might not set foreground always to white, but probably in a lot of cases.
Change the redeclared name to something else:
# Backup original prompt_dir function to prompt_my_dear
eval "`declare -f prompt_dir | sed '1s/.*/prompt_my_dear() {/'`"
prompt_dir() {
# Never omit first character
POWERLEVEL9K_DIR_OMIT_FIRST_CHARACTER=false
# Except if current directory is longer than one character
local currentDirectory=$(print -P "%~")
[[ ${#currentDirectory} -gt 1 ]] && POWERLEVEL9K_DIR_OMIT_FIRST_CHARACTER=true
# Call original prompt_dir function
# no pun intended
prompt_my_dear "${1}" "${2}"
}
The tricky part is how the declare statement works. declare -f prompt_dir outputs the source of prompt_dir, and we pipe that output through sed to replace the function name with something else. This whole expression gets evaluated and is by that a real function in your shell.
With this approach your color variables should be renamed to POWERLEVEL9K_MY_DEAR_FOREGROUND="white"..
Going to close this one out due to staleness, and the fact that this should be a bit easier in our new codebase, so this discussion isn't totally relevant anymore.
A year late to the party, but:
this should be a bit easier in our new codebase
Is there now an easier, more straightforward solution to the issue above? This is pretty much my only niggling issue with powerlevel9k; overall, it's an awesome little thing, keep up the good work. :)
@nathanielevan You are in luck. There is indeed a straightforward solution: POWERLEVEL9K_SHORTEN_STRATEGY=truncate_to_last.
It works, thank you!
You are very welcome.
Most helpful comment
Well. More or less:
And, if you want to get rid of the abbreviation:
To pick up your example:
~/Documents/I_symply_want_to_show_the_last_name/movies/last_namewould become…/last_name, and if you set the delimiter to blank, you'll get/last_name.