Adding a newline above PS1 that survives prepending

In OBSD ksh, I had a PS1 which prepended a blank line:

PS1="n[u@h] wn$"

The problem with this was that pyenv prepends the name of a virtual environment when that environment is activated. Here is the relevant bit of the pyenv activation script:

PS1="(porcupine) ${PS1:-}"

This eats the blank line, turning

[myusername@myhostname] ~/some/path $ echo foo
foo

[myusername@myhostname] ~/some/path $

into

(virtual-environment-name)
[myusername@myhostname] ~/some/path $ echo foo
foo
(virtual-environment-name)
[myusername@myhostname] ~/some/path $

This is annoying, so I tried to fix it with tput:

PS1_TEXT="[u@h] wn$ "
PS1="$(tput sc il1 nw)$(tput rc DO 1)n$PS1_TEXT"

This moves the cursor to the row above the virtual environment name, makes it a blank line, then returns and sticks in my original PS1.

This almost works, but when I hit the bottom of the terminal window, the virtual environment name vanishes. This is annoying, again.

Asked By: user1093043

||

Assuming I checked the correct pyenv source I would recommend to set PYENV_VIRTUALENV_DISABLE_PROMPT=1 and then build your own custom PS1 without interference from pyenv.

The activated environment should be available as PYENV_VIRTUAL_ENV (or you check what is set after activate with env).

https://github.com/pyenv/pyenv-virtualenv/blob/c85c1c9ed520d28cbb34e10e1fe82c6ee37130bc/bin/pyenv-sh-activate#L244

Also quite interesting. You can find quite allot of questions/discussions around this variable and the "deprecation" of prompt modification. For example https://stackoverflow.com/questions/74611317/enable-pyenv-virtualenv-prompt-at-terminal

This deprecation was recently removed and pyenv plans to keep the prompt modification (including this way to disable it). See https://github.com/pyenv/pyenv-virtualenv/commit/77a7644ff7a7a624ba4dfebe5c7441a57be3cc48.

PS: assuming $() works the same in ksh as in bash adding the tput as command instead of it’s output is probably unnecessary bad for your shell performance.

Answered By: Paul Pazderski

Thanks. For posterity, the details in OBSD ksh are, in .kshrc:

VIRTUAL_ENV_DISABLE_PROMPT=1

function prompt_command {
   if [[ -n  ${VIRTUAL_ENV} ]]; then
      print -r "(${VIRTUAL_ENV##*/})"
   fi   
   print -n "[u@h] wn$ "
}
PS1='n$(prompt_command)'

Note the environment variable VIRTUAL_ENV_DISABLE_PROMPT and the use of single quotes for running prompt_command (I mistyped double quotes in my question).

Answered By: user1093043
Categories: Answers Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.