Closed sig-kill closed 4 years ago
Let's first explore if you can achieve what you want with the existing tools.
Put this in ~/.zshrc
:
hash -d cpp=~/documents/dev/cpp_projects
hash -d citc=/google/cloud/src/$USER/src
Then restart zsh: exec zsh
.
Does prompt look better? Is it satisfactory?
hash -d
introduces a two-way association similar to ~
<=> /home/$USER
(or more precisely, ~
<=> $HOME
). You can do cd ~cpp
or even cd ~cpp/<TAB>
.
Ha, guess I made it a little obvious.
I did try out the hash -d function, but it doesn't seem to work for the path replacing part. I'm able to "cd ~[thing]" normally, but the dir prompt element still shows the full (truncated) path.
I'd like to map /google/cloud/src/$USER/src/(.*)/google3/ to '/' so I can match convention ("//whatever/folder"), not sure if that's possible with hash -d.
Ha, guess I made it a little obvious.
😉 I'm epitaph/romanp
I did try out the hash -d function, but it doesn't seem to work for the path replacing part. I'm able to "cd ~[thing]" normally, but the dir prompt element still shows the full (truncated) path.
Can you clarify this bit? cd ~[thing]
suggests that you are using dynamic named directories but above I mentioned only static. So, what's your current directory, what do you see in prompt, what do you want to see in prompt, and which named directories do you have?
(You can get what you want with dynamic named directories, by the way. Check them out. Since you only care about one way conversion, it should be fairly straightforward.)
I'd like to map /google/cloud/src/$USER/src/(.*)/google3/ to '/' so I can match convention ("//whatever/folder"), not sure if that's possible with hash -d.
You can try these:
POWERLEVEL9K_SHORTEN_FOLDER_MARKER=google3
POWERLEVEL9K_DIR_TRUNCATE_BEFORE_MARKER=true
Or, if there is some unique file or directory in google3
:
POWERLEVEL9K_SHORTEN_FOLDER_MARKER=unique_file_or_directory
POWERLEVEL9K_DIR_TRUNCATE_BEFORE_MARKER=true
When these kick in, you'll have an indication in your prompt: the current directory won't start neither with /
nor with ~
, so you'll know it's truncated.
FWIW, I'm not a fan of these options because they make it impossible to figure out your current directory from prompt. hash -d c=/google/cloud/src/$USER/src
should work and it'll be almost as concise as what you ask (plus the CL number, which isn't a terrible thing). If this doesn't work, let me know.
Do any of these work satisfactorily?
Ah, I only meant cd ~name_of_thing
, not implying any dynamic behavior. All of my directories-to-alias don't really change much so there's no need for anything too fancy.
What I mean by that statement was that hash -d doesn't affect my prompt, but I still see that the underlying association was made if I do ls ~name_of_thing
or something.
My prompt looks kind of like this:
username@host /google/cloud/src/$USER/client_name/google3/foo/bar $ ... [client_name]
where [client_name] is a custom right prompt element that just does some simple regex on the pwd. I want it to look like this:
username@host //foo/bar $ ... [client_name]
I didn't think of using google3 as an anchor, I guess that works too.
username@host client_name/google3/foo/bar $ ... [client_name]
though I'd like to still reduce the redundancy present.
Thanks for the help, I'll go check out the options you've presented and see if there's a way to make that dream second prompt. I'll close this and reopen it if I need.
I've extended POWERLEVEL9K_DIR_TRUNCATE_BEFORE_MARKER
to support optional offset:
# Optionally, "first" and "last" can be followed by ":<offset>" where <offset> is an integer.
# This moves the truncation point to the right (positive offset) or to the left (negative offset)
# relative to the marker. Plain "first" and "last" are equivalent to "first:0" and "last:0"
# respectively.
Try updating powerlevel10k and setting these options in ~/.p10k.zsh
:
typeset -g POWERLEVEL9K_DIR_TRUNCATE_BEFORE_MARKER=first:2
typeset -g POWERLEVEL9K_SHORTEN_FOLDER_MARKER=google3
Note that both of these parameters already exist in ~/.p10k.zsh
. You need to find them and change their values.
This will display /google/cloud/src/$USER/client_name/google3/foo/bar
as foo/bar
. If you want, you can also display an icon next to the current directory when it gets truncated in this way.
typeset -g POWERLEVEL9K_DIR_CLASSES=(
"/google/cloud/src/${(b)USER}/client_name/google3/*" CITC '')
typeset -g POWERLEVEL9K_DIR_CITC_{NOT_WRITABLE_,}VISUAL_IDENTIFIER_EXPANSION=$'\uF7B7'
Alternatively, add this to ~/.zshrc
:
function citc-directory-name() {
emulate -L zsh -o extended_glob
local root=/google/cloud/src/$USER
case $1 in
d)
local -a match mbegin mend
[[ $2 == (#b)$root/([^/]##)/google3(|/*) ]] || return
local client=$match[1]
typeset -ga reply=(c:$match[1] $(($#2 - $#match[2])))
;;
n)
[[ $2 == c:* ]] || return
typeset -ga reply=($root/${2:2}/google3)
;;
c)
local expl clients=($root/*(/N:t))
_wanted dynamic-dirs expl 'citc directory' compadd -S\]/ -- c:$^clients
;;
*)
return 1
;;
esac
}
zsh_directory_name_functions+=(citc-directory-name)
This will display /google/cloud/src/$USER/client_name/google3/foo/bar
as ~[c:client_name]/foo/bar
. This transformation is reversible and you can use ~[c:client_name]
with regular commands. In addition, typing ~[<TAB>
will complete your client names.
If this is the only dynamic directory you are using, it makes sense to ditch c:
prefix, which would give you ~[client_name]
. Here's the code:
function citc-directory-name() {
emulate -L zsh -o extended_glob
local root=/google/cloud/src/$USER
case $1 in
d)
local -a match mbegin mend
[[ $2 == (#b)$root/([^/]##)/google3(|/*) ]] || return
local client=$match[1]
typeset -ga reply=($match[1] $(($#2 - $#match[2])))
;;
n)
typeset -ga reply=($root/$2/google3)
;;
c)
local expl clients=($root/*(/N:t))
_wanted dynamic-dirs expl 'citc directory' compadd -S\]/ -a clients
;;
*)
return 1
;;
esac
}
zsh_directory_name_functions+=(citc-directory-name)
This is my favorite among all the alternatives. It's really nice when prompt shows directory in the format that you can also use on the command line.
You mentioned that you have a custom prompt segment that displays [client_name]
on the right hand side. Just to make sure you have an efficient implementation, here's how it should go:
function prompt_my_citc_client() {
emulate -L zsh -o extended_glob
local -a match mbegin mend
[[ $PWD == (#b)/google/cloud/src/$USER/([^/]##)(|/*) ]] || return
p10k segment -b 0 -f 7 -i $'\uF7B7' -t ${match[1]//\%/%%}
}
Wonderful, thank you for all your work!
The title might be a little confusing so I'll explain the usecase here. All of this is related to the "dir" prompt element.
Let's say I have a (somewhat contrived) long root working directory:
/home/$USER/documents/dev/cpp_projects/project1/ $
and in that, I'm in a directory further in/home/$USER/documents/dev/cpp_projects/project1/submodules/db/storage/src/ $
p10k does some path truncation:
~/doc/d/cpp/project1/sub/db/s/src $
What I'd like to be able to do is define a mapping that would replace a substring in the path with another string, while still preserving the path-truncation behavior after it:
so the path in my prompt looks like this:
:cpp/project1/sub/db/src $
This would be really useful if working on network/cloud codebases for work:
/employer/cloud/src/$USER/src/branchA/root/foo/bar/baz $
(this is literally what mine actually looks like lol) which is shortened to/e/cl/s/$USER/s/cl/r/f/b/baz $
but I'd really like to be able to do something like this:
//foo/bar/baz $
There seems to be a similar "truncate before anchor files" function that would do this https://github.com/romkatv/powerlevel10k/blob/master/config/p10k-lean.zsh#L244, but it:
If this is already possible in p10k then please let me know.