Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Directory History is not populated when setting PROMPT_COMMAND #943

Closed
solvedbiscuit71 opened this issue Nov 29, 2024 · 5 comments
Closed

Comments

@solvedbiscuit71
Copy link

Issue

I use the bash shell and installed zoxide via homebrew. The issue was that setting PROMPT_COMMAND seem to interfere with zoxide and not populating the directory history as I navigate through files.

My .bashrc

export PS1="\[\e[01;32m\]\u@\h\[\e[m\]:\[\e[01;34m\]\W\[\e[m\]\$(__git_ps1)\n$ "
export VISUAL=nvim
PROMPT_COMMAND="export PROMPT_COMMAND=echo"
HISTCONTROL=ignorespace
HISTFILESIZE=10000
HISTSIZE=2000
HISTTIMEFORMAT="%F %T "
stty -ixon

eval "$(zoxide init --cmd cd bash)"

I am setting PROMPT_COMMAND to echo a newline after executing each shell command so that I can distinguish between each command. This solution works but introduces the mentioned issue.

What I Tried?

I was able to figure out what changes introduced the bug but not sure why is it happening.

  • When setting PROMPT_COMMAND="export PROMPT_COMMAND=echo" the bug is introduced
  • Setting the PROMPT_COMMAND after initializing zoxide doesn't fix the issue.
  • I tried setting it to PROMPT_COMMAND="echo" and no bug, zoxide just works fine.

I am okay with setting PROMPT_COMMAND="echo" as it does get the job done. but puts an unnecessary newline at the start of the every terminal session which can be annoying.

Version

  • bash: GNU bash, version 5.2.37(1)-release (aarch64-apple-darwin24.0.0)
  • zoxide: 0.9.6
@ajeetdsouza
Copy link
Owner

If you just want the default prompt, why are you setting PROMPT_COMMAND at all?

@ajeetdsouza ajeetdsouza added the waiting-for-response Waiting for a response from the issue author. label Nov 30, 2024
@solvedbiscuit71
Copy link
Author

If you meant that why not prepend a '\n' before the PS1? yeah, you can do that instead of setting PROMPT_COMMAND=echo as both variable are means to generate the shell prompt. Though it's cheap there are some issues with it.

  • when a new terminal session is created, there would be a newline before the prompt
  • while executing clear command there would be a newline before the prompt
  • even with ^L shortcut there would be a newline before the prompt

The solution would be to set these variables to fix all the above issues

PROMPT_COMMAND="export PROMPT_COMMAND=echo"
alias clear="unset PROMPT_COMMAND; clear; set PROMPT_COMMAND=export PROMPT_COMMAND='echo'"

But this causes the issue with zoxide. I understand these are small detail, but the reason we setting these are to customize to our needs.

@github-actions github-actions bot removed the waiting-for-response Waiting for a response from the issue author. label Nov 30, 2024
@ajeetdsouza
Copy link
Owner

This command is what I'm not able to understand:

PROMPT_COMMAND="export PROMPT_COMMAND=echo"

Why are you setting PROMPT_COMMAND, within PROMPT_COMMAND? The next time a prompt is invoked, it will just set your PROMPT_COMMAND to echo (which seems like what you want in the first place).

Just initialize it like this:

PROMPT_COMMAND='echo'
eval "$(zoxide init bash)"

@ajeetdsouza ajeetdsouza added the waiting-for-response Waiting for a response from the issue author. label Nov 30, 2024
@solvedbiscuit71
Copy link
Author

When you create a new terminal session you don't want the PROMPT_COMMAND to execute echo but in the subsequent commands you want to execute echo.

On creating a new terminal session

  1. While executing the .bashrc, set PROMPT_COMMAND="export PROMPT_COMMAND=echo"
  2. Immediately after executing the .bashrc, in order to generate the shell prompt, value of PROMPT_COMMAND is executed as if it was pasted in the terminal. So execute export PROMPT_COMMAND=echo which sets the PROMPT_COMMAND=echo and NO newline character printed.
  3. In subsequent command, after execution the value of PROMPT_COMMAND which is echo is executed. therefore newline character is printed before PS1 is rendered.

I hope I made it clear.

@github-actions github-actions bot removed the waiting-for-response Waiting for a response from the issue author. label Nov 30, 2024
@ajeetdsouza
Copy link
Owner

Ensure that __zoxide_hook is being called somewhere in your new PROMPT_COMMAND and this should work fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants