PS1 corruption running ansi-term bash within emacs

87 views
Skip to first unread message

Chap Harrison

unread,
Aug 8, 2023, 6:31:54 PM8/8/23
to iterm2-discuss
I'm using shell integration, and I customize my bash prompt with prompt_command. It all works fine until I try to start a shell within emacs with the command ansi-term RET /opt/homebrew/bin/bash RET. My prompts come out looking like this:

Screenshot 2023-08-08 at 5.53.26 PM.png

The normal prompt is just the colorized Air:~$ .

Things I've checked:
  • the very last lines of .bashrc are
    • export ITERM2_SQUELCH_MARK=1
    • test -e "${HOME}/.iterm2_shell_integration.bash" && source "${HOME}/.iterm2_shell_integration.bash"
  • my PS1 setting in .bashrc is 
    export PS1="\[\e[${hostcolor};1m\]\${clh_hostname}:\[\e[22;${pathcolor}m\]\${newPWD}\[\e[91;1m\]\$\[\e[0m\] "
  • $clh_hostname, $newPWD, and $host/$pathcolor do not contain any non-printables.
  • I can fix it by sourcing .bash_profile which in turn sources bashrc (but not by sourcing just .bashrc by itself)
  • My prompt_command function simply exports $newPWD:
    function prompt_command {
       local pwdmaxlen=30 # Max len of $PWD 
       local trunc_symbol="..."
       local myPWD=${PWD/~/\~}  # Shorten "/Users/${USER}" to "~"
       if [ ${#myPWD} -gt $pwdmaxlen ] # Trunc $myPWD
       
    then
         
    local pwdoffset=$(( ${#myPWD} - $pwdmaxlen ))                   newPWD="${trunc_symbol}${myPWD:$pwdoffset:$pwdmaxlen}"
       else
          newPWD=${myPWD}
     
      fi
      
     export newPWD
    }

Any suggestions how to correct the behavior of PS1 inside emacs?

Thanks!
Chap

George Nachman

unread,
Aug 9, 2023, 1:14:13 PM8/9/23
to iterm2-...@googlegroups.com
I suspect that emacs is failing to parse some of the control sequences. I'd recommend not using shell integration inside emacs, because it doesn't really stand a chance of working correctly anyway since emacs draws the screen itself.

--
You received this message because you are subscribed to the Google Groups "iterm2-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to iterm2-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/iterm2-discuss/421f7a67-c4e5-4cd8-9104-d24dfd62866dn%40googlegroups.com.

Chap Harrison

unread,
Aug 9, 2023, 3:42:23 PM8/9/23
to iterm2-discuss
Thanks for your reply. I tried not enabling shell integration inside emacs by putting this at the end of my .bashrc file, to avoid sourcing shell integration when inside emacs (as indicated by a convenient env. variable called INSIDE_EMACS).

echo "INSIDE_EMACS='$INSIDE_EMACS'"
if [ -z "$INSIDE_EMACS"  ]
then
    echo "Not inside emacs so enabling shell integration"

    export ITERM2_SQUELCH_MARK=1
    test -e "${HOME}/.iterm2_shell_integration.bash" && source "${HOME}/.iterm2_shell_integration.bash"
else
    echo "Inside emacs so NOT enabling shell integration"
fi

Here's the output from the emacs shell:

INSIDE_EMACS='29.1,term:0.96'
Inside emacs so NOT enabling shell integration
bash: __bp_precmd_invoke_cmd: command not found
bash: __iterm2_prompt_command: command not found
bash: __bp_interactive_mode: command not found
Air:~$


So I'm no longer getting a trashy prompt, but I get those three bash errors before every prompt. Is there something additional I should do? I suspect something's being inherited from the shell I was in when I executed emacs.

Chap Harrison

unread,
Aug 9, 2023, 7:47:51 PM8/9/23
to iterm2-discuss
Solution may be simply to (re-)issue export PROMPT_COMMAND=my_prompt_command in addition to bypassing sourcing shell integration.
Seems to work.
Reply all
Reply to author
Forward
0 new messages