Navigation Menu

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

setting terminal tab title #5700

Closed
shiyuangu opened this issue Dec 9, 2016 · 13 comments
Closed

setting terminal tab title #5700

shiyuangu opened this issue Dec 9, 2016 · 13 comments
Labels
Area: core Issue or PR related to core parts of the project Support Request for support

Comments

@shiyuangu
Copy link

shiyuangu commented Dec 9, 2016

Hi,
On Ubuntu 16.04(Xenial Xerius): I try to set the terminal tab title following this post:

print -Pn "\e]1;test_title\a"

which has actually no effect. I wonder whether this problem is caused by interference with oh-my-zsh. I notice that lib/termsupport.zsh does define a title() function which has no effect either:

title short_tab

Any help/suggestion on this is appreciated.

@heruan
Copy link

heruan commented Jun 6, 2017

Any news on this? I'm experiencing it too (tested on macOS Sierra Terminal and iTerm2).

@saurfangg
Copy link

@shiyuangu You probably need to edit your ~/.zshrc file and uncomment the following line:

DISABLE_AUTO_TITLE="true"

After that you can change a tab's title with something like:
echo -e "\033];this is the title\007"

@tareqalam
Copy link

@shiyuangu solution works, just added a method inside .zshrc like this:

function set-title(){
  TITLE="\[\e]2;$*\a\]"
  echo -e ${TITLE}
}

then I can call the method in terminal like this:
set-title 'whatever i want'

@cameck
Copy link

cameck commented Jul 18, 2017

You can put right underneath the DISABLE_AUTO_TITLE="true" (as stated by @saurfangg) this:

function precmd () {
  window_title="\033]0;${PWD##*/}\007"
  echo -ne "$window_title"
}

That will set the tab to have the title of the current file directory and using the precmd will enable it to execute before each prompt.

Better yet, you should put this code in your own custom profile to keep things neat.

@mcornella mcornella added Area: core Issue or PR related to core parts of the project Support Request for support labels Apr 7, 2019
@llinfeng
Copy link

llinfeng commented Aug 3, 2019

Managed to put hostname + emoji in the tab title as well (using Windows Terminal and WSL). My tab title looks like: Hostname😎Foldername with the following script in the ~/.zshrc.

# To display Hostname😎PWD_Name as title text
HOSTNAME=$(hostname)
DISABLE_AUTO_TITLE="true"
## Let zsh launch with the custom title.
window_title="\033]0;$HOSTNAME😎${PWD##*/}\007"
echo -ne "$window_title"
## Refresh the custome title when the directory changes. Changed from precmd as it shall suppress the set-title function below
function chpwd () {
  window_title="\033]0;$HOSTNAME😎${PWD##*/}\007"
  echo -ne "$window_title"
}
# Setting your own title text on demand. (Don't change dir after setting it 😎)
function set-title(){
  TITLE="\[\e]2;$*\a\]"
  echo -e ${TITLE}
}

@cameck @saurfangg One more question though: how to further let the tab-title rename itself when I enter the command-line Vim? The terminal does not opt to display Vim's own titlestring setting. Of course, I prefer to have the tab-title resume its Hostname😎Foldername format when I exit Vim.

@jacobpierce1
Copy link

On MacOS 10.14, enabling DISABLE_AUTO_TITLE="true" enables me to set the table title using the above, but when I execute e.g. echo -e "\033];this is the title", my tab and window title are then both <current directory > -- this is the title -- -zsh. Even worse, if I am in a remote ssh session (which is how I usually work), the tab title also contains the command I used to start the ssh session, namely ssh <user@remote>). Thus even if I change the tab title, there's too much extra information so I can't actually see the new title.

On the other hand, when I press cmd + i to open the terminal settings, if I manually set the tab and window titles to this is the title, then the window title is the same as before; however, the tab title is just this is the title, i.e. the <current directory>, -zsh, and ssh information parts are removed.

I would like to set the tab title from the command line so that the extra information is similarly omitted. Does anyone know if this is possible? Ideally I would also like the window title to omit the current directory and -zsh, but that's not as important for me.

@myounus96
Copy link

myounus96 commented Jan 23, 2020

add this in ~/.zshrc

function set-title() {
  echo -en "\e]2;$@\a"
}

ref : stackoverflow link

@cafink
Copy link

cafink commented Feb 20, 2020

@jacobpierce1 I believe the additional information is being set by the OSX terminal application, not by zsh. You can disable some of all of it via the terminal preferences, under "Profiles." There's a "tab" section that lets you choose what information to display in the tab title.

@Justsoos
Copy link

actually, it is a function of oh my zsh, here:

function omz_termsupport_precmd {

# Runs before showing the prompt
function omz_termsupport_precmd {
  [[ "$DISABLE_AUTO_TITLE" == true ]] && return
  title $ZSH_THEME_TERM_TAB_TITLE_IDLE $ZSH_THEME_TERM_TITLE_IDLE
}

@llinfeng the best way to auto-change the title and same time containing your custom titles is to change this function here.

so, have people release some open API for it and avoid modifying the source code ?
for example, adding some Sign-Title for user custom proxy running/quit mode ? like appending some custom code on string $ZSH_THEME_TERM_TAB_TITLE_IDLE ?

@jonsmithers
Copy link

I'd like to customize the title used for an idle zsh shell, but I don't want to disable the "auto title" feature.

Examples of why "auto title" is nice: when I run vim, the title auto changes to vim. If I run sleep 1000, the title auto changes to sleep 1000. When I "DISABLE_AUTO_TITLE" and do it all myself, I lose this behavior.

@mcornella
Copy link
Member

@jonsmithers I've now documented the automatic title settings in the wiki, have a look.

@jonsmithers
Copy link

@mcornella oh that's amazing. Thank you!

And yeah, like it says, I just have to make sure it's set after the source $ZSH/oh-my-zsh.sh line. This is perfect.

@regbo
Copy link

regbo commented Oct 28, 2021

I like to use this snipped of code to get everything setup on a new server. Specifically:

Window title: USER@HOSTNAME
Prompt: USER@HOSTNAME

To do so you can just paste this into ~/.zshrc:

DISABLE_AUTO_TITLE="true"
HOSTNAME=$(hostname)
TITLE_PRE="\033]0;"
TITLE_POST="@$HOSTNAME\007"

window_title="$TITLE_PRE${PWD##*/}$TITLE_POST"
echo -ne "$window_title"

function chpwd () {
  window_title="$TITLE_PRE${PWD##*/}$TITLE_POST"
  echo -ne "$window_title"
}

function set-title(){
  TITLE="\[\e]2;$*\a\]"
  echo -e ${TITLE}
}

PROMPT="$fg[yellow]%}$USER$fg[white]%}@%{$fg[cyan]%}%m ${PROMPT}"

But I find it easiest to keep this copy pasta around for a new server. It will update everything:

(

sed -i '/#TITLE_PROMPT_CODE_START/,/#TITLE_PROMPT_CODE_END/d' ~/.zshrc

source ~/.zshrc

read -r -d '' TITLE_PROMPT_CODE << EOM

#TITLE_PROMPT_CODE_START

DISABLE_AUTO_TITLE="true"
HOSTNAME=$(hostname)
TITLE_PRE="\033]0;"
TITLE_POST="@$HOSTNAME\007"

window_title="$TITLE_PRE${PWD##*/}$TITLE_POST"
echo -ne "$window_title"

function chpwd () {
  window_title="$TITLE_PRE${PWD##*/}$TITLE_POST"
  echo -ne "$window_title"
}

function set-title(){
  TITLE="\[\e]2;$*\a\]"
  echo -e ${TITLE}
}

PROMPT="$fg[yellow]%}$USER$fg[white]%}@%{$fg[cyan]%}%m ${PROMPT}"

#TITLE_PROMPT_CODE_END

EOM

echo $TITLE_PROMPT_CODE >> ~/.zshrc

source ~/.zshrc

)

The above code can be run multiple times shoudl you forget that you've already run it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: core Issue or PR related to core parts of the project Support Request for support
Projects
None yet
Development

No branches or pull requests