Let's say I have an array defined as such:
typeset -g POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(
os_icon
dir
vcs
newline
prompt_char
)
Now I want to insert an item my_item into the array after vcs. I don't know what index vcs is at, all I know is that I want to insert it right after vcs
I tried
POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(${POWERLEVEL9K_LEFT_PROMPT_ELEMENTS:s/vcs/vcs my_item})
But for some reason that does not work. The followed does work for substitution:
POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(${POWERLEVEL9K_LEFT_PROMPT_ELEMENTS:s/vcs/my_item})
But of course will remove vcs.... I'm obviously not getting the space correct, but can't seem to get it right.
Adding an element of an array in zsh via a somewhat different subscript then that given in Tom Regner's answer. Zsh almost always has more then one way to do the same thing.
Also using indexing to locate the element that the new element will be inserted after. 1 is then added to the index. Note the ,0 in the subscript.
POWERLEVEL9K_LEFT_PROMPT_ELEMENTS[((${POWERLEVEL9K_LEFT_PROMPT_ELEMENTS[(i)vcs]}+1)),0]=my_item
You may also find the following subscript useful.
To add an element before the first element of an array:
POWERLEVEL9K_LEFT_PROMPT_ELEMENTS[1,0]=new_first_element
This effectively shifts all existing elements to the right.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With