1

everyone!

In my iterm2 (with zsh, oh-my.zsh and powerline2) terminal if I go to certain directories I have a prompt like this:

$ pokemon/electric/pichu/pikachu/raichu

I'd like to have a shorter, but still complete, path representation like this:

$ P/E/P/P/raichu

I have seen this kind of configuration but I haven't been able to set it.

====== EDIT ====== Graphical example: enter image description here

Can you help with this?

Thanks in advance!

Kusalananda
  • 320,670
  • 36
  • 633
  • 936
mabvmex
  • 21
  • 6
  • Have a look at this: https://github.com/ohmyzsh/ohmyzsh/issues/7089 and this plugin: https://github.com/ohmyzsh/ohmyzsh/tree/master/plugins/shrink-path – thanasisp May 08 '22 at 23:27
  • I rolled back your recent edit tagging the title with "SOLVED". Accepting an answer will mark the question as resolved. – Kusalananda Sep 03 '22 at 07:12

4 Answers4

2

You could do something like:

$ set -o extendedglob -o promptsubst
$ PS1='${${${PWD/#%(#b)$HOME(|\/*)/$match[1]}//(#b)([^\/])([^\/]#)\//$match[1]:u/}//\%/%%}$ '
$ cd install/cvs/oh-my-zsh
/I/C/oh-my-zsh$ cd /usr/share/zsh/functions/Misc
/U/S/Z/F/Misc$ 

Or compute that in a chpwd() hook to only compute it when the current working directory changes (and also avoid having to enable the dangerous promptsubst along with having to escape the %s) and store it in $psvar referenced with %v in your prompts:

shorten_PWD() {
  set -o localoptions -o extendedglob
  psvar[1]=${PWD/#%(#b)$HOME(|\/*)/$match[1]}
  psvar[1]=${psvar[1]//(#b)([^\/])([^\/]#)\//$match[1]:u/}
}
chpwd_functions+=(shorten_PWD)
shorten_PWD
PS1='%1v$ '
Stéphane Chazelas
  • 522,931
  • 91
  • 1,010
  • 1,501
1
  1. We need to edit the .p10k.zsh file (in my case, I am using powerlevel10k on zsh/oh-my-zsh/iTerm)
    nano .p10k.zsh
    
    (or use any other editor of your choice).
  2. We search for the line typeset -g POWERLEVEL9K_SHORTEN_STRATEGY and add the value as follows:
    typeset -g POWERLEVEL9K_SHORTEN_STRATEGY=truncate_from_right
    
  3. We search for the line typeset -g POWERLEVEL9K_SHORTEN_DELIMITER and set it empty:
    typeset -g POWERLEVEL9K_SHORTEN_DELIMITER=
    
  4. Finally we search for the line typeset -g POWERLEVEL9K_SHORTEN_DIR_LENGTH and add the value 1:
    typeset -g POWERLEVEL9K_SHORTEN_DIR_LENGTH=1
    

You should be able to see something like this:
screenshot of prompt with directory shown as "~/D/I/L/[/3/1/Workbook"


In case you are using a standard zsh theme (without powerlevel10k theme) you can try this:

Add to ~/.zshrc:

setopt prompt_subst
PROMPT='\$ /$(printf "%c/" ${(s./.)PWD:h})${PWD:t} '
  • (s./.) – splits a path at /.
  • printf "%c/" – prints the first character of each directory piece.
  • ${PWD:h} – the 'head' of the current directory, i.e. everything but the last element.
  • ${PWD:t} – the 'tail' / last element in the directory path.

extracted from Gairfowl's answer to fish function prompt_pwd but in zsh?

mabvmex
  • 21
  • 6
  • Note that `printf %c` prints the first byte not first character (at least not for multibyte characters). You'd need `printf %.1s` for the first character. You'd also need `$PWD:h:u` to convert to uppercase. – Stéphane Chazelas Sep 03 '22 at 08:02
  • I’m not sure what you were trying to accomplish with that weird formatting.  Please review my edit, and, if I’ve changed your meaning, [fix it](https://unix.stackexchange.com/posts/715983/edit). – G-Man Says 'Reinstate Monica' Sep 04 '22 at 03:48
0

You can create links

ln -s pokemon P

Then you can either do cd pokemon or cd P and you would get into the same directory.

White Owl
  • 4,511
  • 1
  • 4
  • 15
  • 1
    Thanks! The point is not to modify the folders names links or creating aliases just the prompt of the terminal. What I've seen is that while you navigate through your file systems (regular names) you see the representation on the prompt in a shorter form! – mabvmex May 08 '22 at 02:45
0

I've make this test :

pwd | awk -F/ 'BEGIN{ ORS="/" } END{for (i=1; i<=NF; i++){print $i}}'
result : /opt/tools/intel/oneapi

and then this one :

pwd | awk -F/ 'BEGIN{ ORS="/" } END{for (i=1; i<=NF; i++){print substr($i,1,1)}}'
result : /o/t/i/o

So, we can try to modify your bashrc like this :

function generate_pwd { 
pwd | awk -F/ 'BEGIN{ ORS="/" } END{for (i=1; i<=NF; i++){print substr($i,1,1)}}' 
}
export PS1="\$(generate_pwd) => "

It works, normally.

Satnur
  • 21
  • 3