user1773603
user1773603

Reputation:

Zsh with iTerm2 on MacOS with a small pane and a long prompt : space after prompt removed

SOLUTION STILL NOT FOUND :

I am faced different prolems with zsh which are not expected on MaOS Catalina/iTerm2.

I) Indeed, if I divide for example a big original window of iTerm2 (only once terminal) into 4 panes, and moreover, if I have a long prompt, then the first space between end of prompt and the command is removed.

II) Other stange behaviors : for example, if I do in this pane a ls command, and do after a request of completion with TAB, the second letter 's' of 'ls' is removed OR another behavior : there is no more space between ls command and the directories/files suggested by completion.

Here are below scree captures in order to illustrate and help you to understand :

1) Initial panes where I reproduce bug into bottom right pane :

enter image description here

2) Completion with 'ls' command : cursor on second letter of 'ls', i.e 's'

enter image description here

3) Example of completion, here with 'ls' command : no more space between command 'ls' and directories/files completionned or space between the last purple pipe symbol and command 'ls' removed.

enter image description here

I would like to get some advices to fix the problems 2) and 3). Ideally, I would like to keep my long prompt (due to a long PATH of current directory) and understand why I have these strange behaviors.

My prompt is defined into .zshrc :

PS1='%F{magenta}|%F{green}%n@%F{cyan}%m%F{magenta}|%f%T%F{magenta}|%B%~%b%F{magenta}|%f '

you can see the space at the end the prompt |%f ', which doesn't seem to be respected

I know that my issues come surely from the long prompt (If I apply the same command 'ls' into $HOME, everyting is working fine, i.e completion doesn't remove any space.

That's why I have also tried to use this code in .zshrc (from http://aperiodic.net/phil/prompt/) to truncate the PATH :

###
# Truncate the path if it's too long.

PR_FILLBAR=""
PR_PWDLEN=""

local promptsize=${#${(%):---(%n@%m:%l)---()--}}
local pwdsize=${#${(%):-%~}}

if [[ "$promptsize + $pwdsize" -gt $TERMWIDTH ]]; then
  ((PR_PWDLEN=$TERMWIDTH - $promptsize))
else
  PR_FILLBAR="\${(l.(($TERMWIDTH - ($promptsize + $pwdsize)))..${PR_HBAR}.)}"
fi

But it does nothing, PATH remains the same ...

If someone has had the same type of problems, this would be fine to tell the potential solutions to fix them. And Moreover, any feedback is also welcome.

UPDATE 1: I get better results, as I would improve it, i.e I would like that zsh automatically truncates a long directory name with a limit fixed before.

Before that, I show you the kind of result that I would like to obtain :

The PS1 used for this figure below is got with :

PS1='%F{magenta}|%F{green}%n@%F{cyan}%m%F{magenta}|%f%T%F{magenta}|%F{75}%-2d~/.../%F{13}%1~%F{magenta}| '

and the result on the third line :

zsh prompt

REQUEST 1:

It's a first interesting resutl for me since I want the current directory alone to have a different color from the above paths that I want to truncate.

As you can see, an issue remains, i.e the directory

Travail_3_Alain_Injection_des_Biais_Spectro_dans_XSAF_avec_GCph+WL+XC_et_rajout_dun_Prior_dans_la_matrice_de_Fisher_de_SpecSAF_15_JANVIER_2020

is very, very long.

REQUEST 2:

So, I know it could be difficult to implement but is it possible, for a given number of maximal characters fixed, to be able to truncate all components of the full path (I mean each above directories) to another components on which we have applied a truncation between the start and the end of directory ?

For example, the directory

 Travail_3_Alain_Injection_des_Biais_Spectro_dans_XSAF_avec_GCph+WL+XC_et_rajout_dun_Prior_dans_la_matrice_de_Fisher_de_SpecSAF_15_JANVIER_2020

would become :

Travail_3_...SpecSAF_15_JANVIER_2020

I want to keep the 2 different colors between the full path (truncated, excepted for current directory, with color 75) and the current directory (color 13 in my last result above).

By the way, what is the $reset_color function : is it mandatory to use it ?

UPDATE 2: I have set for the moment the following prompt :

PS1='%F{13}|%F{green}%n@%F{cyan}%m%F{13}|%f%T%F{13}|%F{75}%-4d/…… %F{13}%1~%F{13}|%F{15} '

which gives the following rendering :

enter image description here

As you can see, I can display up to the fourth (with %~4d) depth of directories in the first part of my prompt. And after this first part, I make display the current directory (by %1~).

REQUEST 3:

It is almost the result that I would like to get : having firstly the parent directory up to fourth depth in a defined color (here %F{75}) and after the ……symbol, the current directory in another defined color (in my case, %F{13}.

Almost since when I type the cd command, I get the following result :

`|fab@astro|6:50|/Users/fab/……~|`

As you can see, I would like to get only :

`|fab@astro|6:50|~|`

i.e, to make disappear the path /Users/fab/……into prompt since ~ and /Users/fab/ make reference to the same thing.

More generally, I would like to replace always /Users/fab/by ~, i.e for all prompts that I make display.

How to fix it ?

REQUEST 4: it remains the issue of space between prompt and command which interfer when I am into a small pane. I have no idea how to prevent this space to disappear. I have a serious track : I have realized this behavior occurs when the prompt is displayed on multiple lines due to the fact that pane is small and so prompt can't be contained on ony one line, like this :

zsh prompt on multiple lines since pane is small

There too, I would like to rectify this bug of "space removing". I think one solution may be to truncate the second part of my prompt (current directory) in order to make it contain on only one line (like I have indicated it above in REQUEST 2) but how to implement it ?

SOLUTION STILL NOT FOUND : sorry, I believed that zsh-5.8 could fix the issue but actually, from my last tests, this is not the case.

Upvotes: 2

Views: 909

Answers (1)

lhd
lhd

Reputation: 436

regarding request 3, could you show your $HOME? \w or \W will always replace $HOME for ~ as described here (I'm guessing your $HOME might not be set)...

Alternatively and not so clean but it does the trick, (if $HOME is properly set), you could try to add something like this into it:

if [[ "${PWD}" == "${HOME}" ]] ; then printf \~; else echo -n ${PWD##*/}; fi

as discussed here

this could also do the trick for request 4, assuming you have a super long folder and want to shrink it a bit:

pwdvar="${PWD}"
len_pwd=${#pwdvar}
limit_char=50
letters_beg=10
letters_ignored=50
shortpwdvar=$(echo $pwdvar | cut -d '/' -f 5-)
if [[ len_pwd > limit_char  ]] ; then echo ${pwdvar} ; else echo "~/.../"${shortpwdvar::letters_beg}"..."${shortpwdvar:letters_ignored}; fi

Upvotes: 0

Related Questions