Bash PS1 Settings - how to return the current folder as a terminal header

I recently added these lines to my ~ / .bashrc file to show the current branch if I'm in the git working folder and it works great for this. However, I lost the fact that the current folder name that was shown on the tab for terminal i is open, and now it is not: it always just says "Terminal". Can I get this back and still save the git stuff? Here the lines in question are the second problem, that is the problem, since commenting only on the second line fixes the problem.

source /etc/bash_completion.d/git PS1='\h:\w$(__git_ps1 "\[\e[32m\][%s]\[\e[0m\]")$ ' 

I was looking for an explanation of the options for PS1, but I see nothing about the name of the terminal window. Can anyone advise? thanks max

EDIT

I actually manipulate PS1 in order to have a terminal with the format

<rvm version and gemset> <computer name> <current folder> <git branch>

with each part of a different color, but I have never seen documents before, so thanks for the link to this. My current PS1 setup

 \[\033[0;31m\]$(__my_rvm_ruby_version)\[\033[0;33m\]\h\[\033[0;37m\]:\[\033[1;33m\]\W\[\033[1;32m\]$(__git_branch)\[\033[1;32m\]$(__git_dirty) \[\033[0;37m\]$ 

Presumably I can do something like

 export "<something> $PS1" 

to set the name of my terminal tab without losing my existing settings. I did all this, but failed to do it.

EDIT - figured this out with some of the answers below - thanks everyone! I wrapped it in a shell script

 #!/usr/bin/env bash #renames the current terminal tab via the PS1 env var source ~/.bashrc export PS1="$PS1""\[\e]0;$1 \a\]" 

it is called "renametab", so now I can name it, for example,

 source renametab mytabname 

a "source" is needed to export changes to the current shell: if I just do renametab mytabname , the export just goes into a subshell, which gets killed when the script ends.

Thanks again for your help!

+7
git bash
source share
6 answers

What I have by default on my Ubuntu regarding the terminal name:

 PS1='\[\e]0;\u@\h: \w\a\]' 

Prepare PS1 for it and it should be fine

+4
source share

You can try:

 PS1="$PS1"'\h:\w$(__git_ps1 "\[\e[32m\][%s]\[\e[0m\]")$ ' 

But this would help to find out that PS1 is installed earlier in ~/.bashrc or in /etc/bash.bashrc .

+5
source share

If you're lucky, your terminal is Xterm compatible, and you can use ANSI sequences to set the name of the terminal.

Instead of giving you consistency, I will point you to the documentation . Now you can colorize the hint. The sequence you need is described as xterm title hack, which is.

You can also use checklists in a tooltip to help install PS1 . They can be found in the BASH manpage in PROMPTING .

And here is what I have in my .bashrc :

 PS1="\e]0;BASH: \u@\h\aBASH \v: \u@\h:\w\n\$ " 
  • \e]0;BASH: \u@\h\a - Set the window title
    • \e] - send an ESC character followed by a closed left bracket. This is the beginning of the ANSI command sequence.
    • 0; - zero followed by a semicolon. The next line sets the title.
    • BASH: \u@\h\ - The name of my window
      • \u - Username
      • @ - @ Sign
      • \h - hostname
    • \a - BEL (Cnrl-G), which ends my header
  • BASH \v: \u@\h:\w\n\$ - sets my invitation
    • BASH \v: is the BASH string followed by the version. My default shell is Ksh, so when I use the BASH shell, I want to see the word BASH in my prompt
    • \u - Username
    • \h - hostname
    • \w - current working directory (based on HOME)
    • \n - New line - my invitation - two lines. Thus, long directories do not occupy command-line space.
    • \$ is the dollar sign or # if you have root privileges.

Play and have fun. Try different escape sequences. Add color. Insert GIT material. To explore.

Remember, this is just a computer. The worst thing you can do is make the whole system crash and delete all your valuable work, lose your job and become a hopeless pariah, the only way to earn income will be to ask customers if they want to free. If you do not work in a bank. Then you can also lower the entire global economy.

+2
source share

Another way to do this is to use PROMPT_COMMAND and give PS1 just an invitation. For example:

PROMPT_COMMAND = 'echo -ne "\ 033] 0; $ {USER} @ $ {HOSTNAME %%. *}: $ {PWD / # $ HOME / ~}"; echo -ne "\ 007" '

0
source share

I use this:

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

As a result, the window title looks like this:

 /home/tkirk - tkirk@hostname 
0
source share

For the true helpless (like me). Here is my Bash RC, which includes the git branch name at the prompt and sets the window title. Note that this uses PROMPT_COMMAND for __git_ps1, which sets PS1 internally. When using __git_ps1 in this way, it takes 2 arguments. The first argument is the line before the branch name, the second argument is the line after the branch name.

 # # ~/.bashrc # # If not running interactively, don't do anything [[ $- != *i* ]] && return source ~/git/contrib/completion/git-prompt.sh alias ls='ls --color=auto' alias cd..='cd ..' c_reset='\[\e[0m\]' c_prompt='\[\e[1;31m\]' c_path='\[\e[0;33m\]' if [ -f ~/git/contrib/completion/git-prompt.sh ]; then . ~/git/contrib/completion/git-prompt.sh GIT_PS1_SHOWDIRTYSTATE=true GIT_PS1_SHOWCOLORHINTS=true GIT_PS1_UNTRACKEDFILES=true # Below, The section "\[\e]0;\w\a\]" sets the window title PROMPT_COMMAND="__git_ps1 '${c_path}\w${c_reset}' '\n\u@\t${c_prompt}-->${c_reset}\[\e]0;\w\a\]' " fi #PS1 is set by the __git_ps1 script above, so comment out below #PS1='\w\n[\u@\t]$>' 
0
source share

All Articles