Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash PS1 settings - how to get the current folder back as the terminal title

Tags:

git

bash

I recently added these lines to my ~/.bashrc file to show the current branch if i'm in a git working folder, and it works nicely for that. However, what i've lost is that the current folder name used to be shown in the tab for the terminal i have open, and now it isn't: it always just says 'Terminal'. Can i get that back and still keep the git stuff? Here's the lines in question - it's the second one that's the issue, as commenting out just the second line fixes the problem.

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

I've been looking at explanations of the options for PS1 but can't see anything about the terminal window's title in there. Can anyone advise? thanks, max

EDIT

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

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

, with each part in a different color, but i've never actually seen the docs before, so thanks for the link to that. My current PS1 setting is

\[\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 my terminal tab name without losing my existing settings. I've been poking around with this though and not managed to do it.

EDIT - figured this out with the help of some of the answers below - thanks all! I wrapped it up 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's called "renametab" so i can now call it with eg

source renametab mytabname

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

Thanks again all, for the help!

like image 947
Max Williams Avatar asked Mar 09 '10 16:03

Max Williams


People also ask

How do I find my current directory name in shell?

Once you start looking through directories, it is easy to get lost or forget the name of your current directory. By default, bash shows just your current directory, not the entire path. To determine the exact location of your current directory within the file system, go to a shell prompt and type the command pwd.

How can I change my bash prompt to show my working directory?

In value of PS1 , \w or \W can be used to include working directory in the prompt. Change the value of PS1 in your $HOME/. bashrc file to change it for every terminal.

What is the PS1 variable in Linux?

PS1 is one of the few variables used by the shell to generate the prompt. As explained in the bash manual, PS1 represents the primary prompt string (hence the “PS”) - which is what you see most of the time before typing a new command in your terminal.

What is the bash prompt?

The default BASH prompt is the one you see when you first open a terminal or command line. It usually looks something like this: username@hostname:~$ Alternatively, it may look like this: (base) [username@localhost ~]$ The first part of the prompt tells you the user that's currently logged in.


1 Answers

You can try:

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

But it would help to know what PS1 is being set to earlier in ~/.bashrc or in /etc/bash.bashrc.

like image 116
Dennis Williamson Avatar answered Sep 28 '22 06:09

Dennis Williamson