Change gnome-terminal title to reflect the current directory?

孤者浪人 提交于 2021-01-20 15:25:11

问题


I want to change the title of the gnome-terminal window to reflect the current directory. Is there a way to do that? A script may be? The "change terminal title" threads did not help much. Manually when we use the command:

gnome-terminal --title=$PWD 

it works, but a new terminal instance is created (as expected). I want to be able to use the

--title=$PWD 

with the cd command.

Is there a way to achieve that?


回答1:


since gnome-terminal uses the same control commands as xterm this page might be helpful.

Xterm Title for bash and other shells

TL;DR:

add following to your .bashrc

PROMPT_COMMAND='echo -ne "\033]0;${USER}@${HOSTNAME}: ${PWD}\007"'




回答2:


PROMPT_COMMAND='echo -ne "\033]0;$(basename ${PWD})\007"' will display only the current directory as the title




回答3:


I'm doing it like this to override the cd command and set pwd into the title:

function title { echo -en "\033]2;$1\007"; }
function cd { dir=$1; if [ -z "$dir" ]; then dir=~; fi; builtin cd "$dir" && title `pwd`; }
cd `pwd`

I just threw this into my ~/.bash_aliases. You could probably tweak this to do what you want.




回答4:


I'm not an expert but you should try to edit your ~/.bashrc file. If I understood your problem correctly you can change your .bashrc according to my (I'm using Ubuntu 12.04). The "old" line is commented out and the new one is below it (with additional comment).

case "$TERM" in
xterm*|rxvt*)
  # OLD PS1 directive
    #PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]$PS1"
  # NEW PS1 directive, shows only current directory name as terminal window name
    PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\W\a\]$PS1"
    ;;
*)
    ;;
esac

The result is that my terminal title is equal to my current directory title eg. after cd Documents/projects my terminal title is projects (if file is open the terminal name is its name)




回答5:


Updated Answer For White Space Problem by 'basename'

PROMPT_COMMAND='echo -ne "\033]0;$(basename "$(pwd)")\007"'

Upadate for answered Oct 23 '13 at 1:47 user2909452



来源:https://stackoverflow.com/questions/10517128/change-gnome-terminal-title-to-reflect-the-current-directory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!