Emacs terminal bash (PS1) invitation is duplicated

This is a bit confusing question, but nothing happens here! I recently updated the bash prompt to a nice version that appeared in the last post of this thread: Bash: a custom PS1 with a good working directory . The corresponding bash code (from this stream entry) is copied here:

# define the awk script using heredoc notation for easy modification MYPSDIR_AWK=$(cat << 'EOF' BEGIN { FS = OFS = "/" } { if (length($0) > 16 && NF > 4) print $1,$2,".." NF-4 "..",$(NF-1),$NF else print $0 } EOF ) # my replacement for \w prompt expansion export MYPSDIR='$(echo -n "${PWD/#$HOME/~}" | awk "$MYPSDIR_AWK")' # the fancy colorized prompt: [0 user@host ~]% # return code is in green, user@host is in bold/white export PS1='[\[\033[1;32m\]$?\[\033[0;0m\] \[\033[0;1m\]\ u@ \h\[\033[0;0m\] $(eval "echo ${MYPSDIR}")]% ' # set x/ssh window title as well export PROMPT_COMMAND='echo -ne "\033]0;${USER}@${HOSTNAME%%.*} $(eval "echo ${MYPSDIR}")\007"' 

This prompt looks something like this (in terminals without emacs):

 [0 user@host ~/my_dir]% 

If "0" is higher than green and "user @host" is in bold. (Note that "0" can be all kinds of numbers and represents the return value of the last command.)

The problem I am experiencing is specific to shells running in emacs (and this happens for most terminal interaction options inside emacs: "term", "ansi-term", "shell" and "eshell"). The request appears twice (and slightly broken) in emacs terminals, for example:

 0; user@host ~/my_dir[0 user@host ~/my_dir]% 

The "second" version of the tooltip, starting with and including "[", looks just fine. This is the preceding text that appears without any style (i.e., Without green and without courage). So, emacs should interpret some part of the prompt as input, and my guess is the colored or bold shielded indicators attached to the parts of the string β€œ0” and β€œuser host”?

Can anyone know how to tell emacs to correctly interpret escape? Or, alternatively, how to change the hint configuration commands so that both emacs do not hate it, and it will still work on terminals without emacs? And perhaps another alternative: how to add a test for terminal type ('eterm-color' in emacs) with a modified line that is emacs-friendly?

+1
source share
2 answers

The error occurs from the export PROMPT_COMMAND=...

You can avoid reading this file in your configuration by checking to see if you have a shell running inside emacs or not. The INSIDE_EMACS environment variable is convenient INSIDE_EMACS . From the Emacs manual ( Section 32.7 ):

Emacs sets the INSIDE_EMACS environment variable in the subshell to version, comint, where version is the version of Emacs (for example, '24 .1). Programs can check this variable to determine if they are running inside an Emacs subitem

In your example, you want to export PROMPT_COMMAND='echo -ne "\033]0;${USER}@${HOSTNAME%%.*} $(eval "echo ${MYPSDIR}")\007" is executed only when you are not in emacs, otherwise you will get this nasty "double invitation". The following conditional statement in your code will help.

 if [ -z "$INSIDE_EMACS" ]; then export PROMPT_COMMAND='echo -ne "\033]0;${USER}@${HOSTNAME%%.*} $(eval "echo ${MYPSDIR}")\007"' else export PROMPT_COMMAND='' fi 

It checks to see if you are inside emacs, and only then the PROMPT_COMMAND variable PROMPT_COMMAND set to the desired value.

+3
source

The PROMPT_COMMAND display comes from the contents of the variable PROMPT_COMMAND . emacs does not seem to understand the character selection sequence of OSC 0 xterm and therefore outputs the result.

+1
source

Source: https://habr.com/ru/post/1490788/


All Articles