emacs终端bash(PS1)提示重复 [英] emacs terminal bash (PS1) prompt duplicated

查看:203
本文介绍了emacs终端bash(PS1)提示重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个有趣的问题,但这里什么也没有!
我最近更新了我的bash提示符,显示在此线程的最后一篇文章中: Bash:自定义PS1,工作目录路径很好
相关的bash代码(从该线程帖子)被复制到这里:

 #使用heredoc符号定义awk脚本容易修改
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


#我替换\w提示扩展
导出MYPSDIR ='$(echo -n$ {PWD /#$ HOME /〜}| awk$ MYPSDIR_AWK)'

#花式彩色提示:[0 user @ host〜]%
#返回代码为绿色,用户@host以粗体/白色
导出PS1 ='[\ [\033 [1; 32m\] $?\ [\033 [0; 0m\] \ [\033 [0; 1m\] \u @ \h\ [\033 [0; 0m\] $(evalecho $ {MYPSDIR})]%'

#设置x / ssh窗口标题以及
导出PROMPT_COMMAND ='echo -ne\033] 0; $ {USER}@${HOSTNAME%%.*} $(evalecho $ {MYPSDIR}) \007'

此提示大致如此(非emacs终端):

  [0 user @ host〜/ my_dir]%

上面的0为绿色,user @ host为粗体。
(请注意,0可以是各种数字,代表最后一个命令的返回值。)



我遇到的问题特定于在emacs中运行的shell(对于emacs中的终端交互的大多数变体:'term','ansi-term','shell'和'eshell')都是特定的。
在emacs终端中,提示出现两次(稍微中断),如下所示:

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

提示符的第二版本,从...开始,包括[看起来很好。
这是前面的文本,没有任何样式(即没有绿色,没有粗体)。
那么,emacs必须将提示的某些部分解释为输入,我的猜测是附加到提示符的0和user @ host部分的颜色或粗体转义的指示符?



有没有人知道如何告诉emacs正确解释转义?
或者,或者,如何修改提示设置命令,以便两个emac都不会讨厌它,它仍然可以在非emacs终端工作?
甚至还有另外一种选择:如何使用emacs友好的修改字符串添加终端类型的测试(emacs中的'eterm-color')?

解决方案

错误来自导出PROMPT_COMMAND = ... 语句。



您可以通过检查您的emacs中是否运行shell来避免在配置中读取。这里环境变量 INSIDE_EMACS 变得方便。从Emacs手册( Sect。32.7 ):


Emacs将subshel​​l中的环境变量INSIDE_EMACS设置为version,comint,其中版本是Emacs版本(例如, 24.1' )。程序可以检查此变量以确定它们是否在Emacs子shell内运行


在您的示例中,您需要
export PROMPT_COMMAND ='echo -ne\033] 0; $ {USER}@${HOSTNAME%%.*} $(evalecho $ {MYPSDIR})\007只有当你不在emacs时执行,否则你会得到这个讨厌的双提示。您的代码中的以下条件语句将有所帮助。

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

它检查您是否不在emacs内,只有那个 PROMPT_COMMAND 变量设置为您所需的值。


This is a bit of a convoluted question, but here goes nothing! I've recently updated my bash prompt to the nice version appearing in the last post of this thread: Bash: custom PS1 with nice working directory path. The relevant bash code (from that thread post) 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 roughly like so (in non-emacs terminals):

[0 user@host ~/my_dir]%

Where the "0" above is green and the "user@host" is bold. (Note that the "0" can be all sorts of numbers, and represents the return value of the last command.)

The issue I'm experiencing is specific to shells running within emacs (and it occurs for most variants of terminal-interaction within emacs: 'term', 'ansi-term', 'shell', and 'eshell'). The prompt appears twice (and slightly broken) in emacs terminals, like so:

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

The 'second' version of the prompt, starting from and including the "[" looks just fine. It's the preceding text, which appears without any styling (i.e. no green and no bold). So, emacs must be interpreting some portion of the prompt as input, and my guess is the color or bold escaped indicators attached to the "0" and "user@host" portions of the prompt?

Might anyone know how to tell emacs to interpret the escapes correctly? Or, alternatively, how to modify the prompt-setting commands such that both emacs will not hate it and it'll still work in non-emacs terminals? And maybe even another alternative: how to add a test for the terminal type ('eterm-color' within emacs) with a modified string that is emacs-friendly?

解决方案

The error comes from the export PROMPT_COMMAND=... statement.

You can avoid this being read in your configuration, by checking whether you have a shell running inside emacs or not. Here the environment variable INSIDE_EMACS becomes handy. From the Emacs manual (Sect. 32.7):

Emacs sets the environment variable INSIDE_EMACS in the subshell to ‘version,comint’, where version is the Emacs version (e.g., ‘24.1’). Programs can check this variable to determine whether they are running inside an Emacs subshell

In your example, you want export PROMPT_COMMAND='echo -ne "\033]0;${USER}@${HOSTNAME%%.*} $(eval "echo ${MYPSDIR}")\007" only being executed when you are not in emacs, otherwise you get this nasty "double prompt". 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 whether you are not inside emacs, and only then the PROMPT_COMMAND variable is set to your desired value.

这篇关于emacs终端bash(PS1)提示重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆