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

Tab titles don't reflect current directory or process running #1188

Open
stereokai opened this issue Dec 13, 2016 · 47 comments
Open

Tab titles don't reflect current directory or process running #1188

stereokai opened this issue Dec 13, 2016 · 47 comments
Labels
📜 Type: Guide Issues has evolved into a helpful document where users help other users

Comments

@stereokai
Copy link

  • OS version and name: Windows 10 64bit
  • Hyper.app version: 1.0.0
  • Link of a Gist with the contents of your .hyper.js: hyper.js gist
  • Relevent information from devtools: N/A
  • The issue is reproducible in vanilla Hyper.app: Reproducible in vanilla

Issue

Current directory is not displayed. All tab titles display "Shell" regardless of which shell I am using (tried cmd and Bash on Ubuntu on Windows).

@audunolsen
Copy link

This is true of macOS too.

@stereokai
Copy link
Author

@audunolsen in version 1.0.0?

I assumed it was Windows only because of this notice: parro-it/hyperterm-title#5

@audunolsen
Copy link

Yes.

screenshot 2016-12-14 01 12 42

Tab titles doesn't reflect directory or process running.

@stereokai stereokai changed the title Tab titles are not behaving correctly on Windows Tab titles don't reflect current directory or process running Dec 14, 2016
@stereokai
Copy link
Author

I stole your comment for a better issue title :)

@parro-it
Copy link
Contributor

Hi, since my plugin hyperterm-title is now embedded in core, tabs title is now managed directly by your shell. There is a special escape sequence command that is meant to set terminal title.

You have to setup a shell prompt that uses this escape sequence in order to avoid "Shell" as tab title.

@parro-it
Copy link
Contributor

Checkout pure prompt for zsh. I'm sure there are similar ones for bash or fish too

@parro-it
Copy link
Contributor

I agree that a better default behaviour is useful... Show the Shell name should be simple to implement
The cwd or current foreground process instead was not trivial, previous hyper versions uses a polling procedure that is too resource heavy...

@stereokai
Copy link
Author

@parro-it thanks for chiming in! I appreciate your work on this plugin as it is a feature probably most users if not everyone take it for granted.

With that said, I think an ideal solution would be supporting all terminals, in particular now that Hyper is meant to be used on all major operating systems. So perhaps additional methods of retrieving the shell title are needed :)

@stereokai
Copy link
Author

@parro-it you were quicker than me!

@parro-it
Copy link
Contributor

@stereokai 😁 any idea on how we Can listen for cwd change or foreground process change without polling?

@stereokai
Copy link
Author

For what's it worth, on Windows, Cmder is capable of pulling the cwd, it might be possible to learn how they do it. Although it is only relevant to Windows terminals (ie. CMD and PowerShell) - as it doesn't work with the native Bash on Windows
cmder

@parro-it
Copy link
Contributor

I found this issue there: cmderdev/cmder#815.
Seems they are using terminal escape codes too to set the title... but I'm not sure.
/cc @samvasko @Stanzilla @MartiUK @Jackbennett

@parro-it
Copy link
Contributor

This is my linux terminal, with two tabs running bash:

image

It display current shell user and name of the shell. I had to comment lines on my .bashrc that set the title using escape codes. The window title reflect that of the current tab.

I think this behavior could be acceptable for hyper too...

@stereokai
Copy link
Author

stereokai commented Dec 14, 2016

@parro-it Excellent find!

Using this, I can confirm Hyper recognizes and picks up the titles from all shells on my machine, that is, PowerShell, cmd, and native Bash on Ubuntu on Windows.

I guess you deserve a huge applause because it's your code that's doing the magic ;)

Can you check if echo -ne '\e]0;HelloCmder\a' is working for you in your Linux shells? Then we could narrow it down to a single, cross-OS method

Edit: Would be better to just include the code from the link here:
PowerShell:

$host.ui.RawUI.WindowTitle = "HelloCmder"

cmd:

title HelloCmder

bash:

echo -ne '\e]0;HelloCmder\a'

@parro-it
Copy link
Contributor

Can you check if echo -ne '\e]0;HelloCmder\a' is working for you in your Linux shells? Then we could narrow it down to a single, cross-OS method

It work, that is exactly the escape code we use to set tab title on hyper 😄.
If you set your prompt to contains the same escape, you can get replace HelloCmder with your CWD, and you get cwd on the title:

image

The tab on the right is running bash without custom prompt, the tab on the left is running zsh with pure

@stereokai
Copy link
Author

What do you mean technically by "set your prompt to contain the same escape"? Can you be more clear?

@parro-it
Copy link
Contributor

Try to run this on bash:

PS1="\[\033]0;\w\007\]>"

That command set your prompt to an escape sequence that change the title to the cwd every time you run a command (and add a '>' to the prompt just for convenience)

Here are various code you can use in the prompt:
http://www.tldp.org/HOWTO/Bash-Prompt-HOWTO/bash-prompt-escape-sequences.html

@stereokai
Copy link
Author

stereokai commented Dec 14, 2016

@parro-it doesn't seem to work in Bash on Windows, but anyway - it won't be supported in cmd and PowerShell - so what's the use? It is a Linux-only infrastructure. Or maybe you can enlighten me :)

@parro-it
Copy link
Contributor

@parro-it doesn't seem to work in Bash on Windows, but anyway - it won't be supported in cmd and PowerShell - so what's the use? It is a Linux-only infrastructure. Or maybe you can enlighten me :)

It work very well on macOS too... it's very useful to have custom prompt in these two operating system. anyway, I still think that a better default for hyper is needed that does not use ansi escape sequences.

@audunolsen
Copy link

PS1="\[\033]0;\w\007\]>"

@parro-it This works great for me on macOS, thank you! Do you know if there is an additional way to make it reflect a process like e.g. node? Like if it's possible to prepend the process name to the cwd?

@audunolsen
Copy link

hyper tabs

I managed to find a solution to show the basedir and append bash commands to it!

case "$TERM" in
xterm*|rxvt*)
    PROMPT_COMMAND='echo -ne "\033]0;${PWD##*/}\007"'
    show_command_in_title_bar()
    {
        case "$BASH_COMMAND" in
            *\033]0*)
                ;;
            *)
                echo -ne "\033]0;${BASH_COMMAND} - ${PWD##*/}\007"
                ;;
        esac
    }
    trap show_command_in_title_bar DEBUG
    ;;
*)
    ;;
esac

I threw the above code in ~.profile, it's based on this.

@parro-it
Copy link
Contributor

@audunolsen awesome!

@zpnk
Copy link

zpnk commented Dec 23, 2016

As I mentioned on #1162 (same bug) the tabs are just displaying "Shell" with Hyper 1.0.0 on Yosemite.

Setting PS1="\[\033]0;\w\007\]>" had no effect.

The bash script above does work.

@anirudhmurali
Copy link

@audunolsen My current tab title was blank, and all tabs just showed "Shell". Placed the code you gave in ~/.profile. All tab titles got replaced by the name of current User.
How do I get tab titles like the one in your GIF?

screen shot 2017-01-29 at 11 36 59 pm

@cfjedimaster
Copy link

Sorry to pile on @audunolsen - I also pasted the code into .profile and no change. Using Bash on Windows10.

@cfjedimaster
Copy link

Nevermind - user error. I put this in .bashrc and it worked. Now to figure out how to get it to show the complete path, not just the current directory. :)

@achekulaev
Copy link

achekulaev commented Jun 29, 2017

autotitle_plus

I decided to extend audunolsen's solution. I wanted the auto-title he created but I also wanted to have ability to set a static title to a tab when needed.

So I did this in .bashrc

function title {
    export TITLE_OVERRIDDEN=1
    PROMPT_COMMAND=''
    echo -ne "\033]0;"$*"\007"
}

case "$TERM" in
xterm*|rxvt*)
    PROMPT_COMMAND='echo -ne "\033]0;${PWD##*/}\007"'
    show_command_in_title_bar()
    {
        if [[ "$TITLE_OVERRIDDEN" == 1 ]]; then return; fi
        case "$BASH_COMMAND" in
            *\033]0*)
                ;;
            *)
                echo -ne "\033]0;${BASH_COMMAND} - ${PWD##*/}\007"
                ;;
        esac
    }
    trap show_command_in_title_bar DEBUG
    ;;
*)
    ;;
esac

Now when I need title it's title My new title

@stilren
Copy link

stilren commented Sep 7, 2017

I am still having trouble getting the location in tab title using powershell

the following line of code does the trick in ConEmu and in vanilla powershell but does not work when opening powershell in hyper. What gives?

$host.UI.RawUI.WindowTitle = Get-Location

Setting it to a string (se below) works fine.

$host.UI.RawUI.WindowTitle = "Hello"

@luismasg
Copy link

luismasg commented Sep 7, 2017

I'm a bit confused.
my tabs are showing the correct command on hyper 1.4.3 on mac.
but instead of "ng serve" I'd like something like 'myProject-ui' and the other tab could be 'myProject-service' instead of just 'node index.js'
so is it posible to manually set title just the way you want?

@chabou
Copy link
Collaborator

chabou commented Sep 8, 2017

@luismasg
@achekulaev's solution seemd perfect for your needs

@audunolsen
Copy link

Hello all, here's a small update benefiting all of you using zsh. I recently made the switch from bash which broke my previous solution. Luckily, fixing this for zsh wasn't too difficult.

screenshot 2017-09-27 19 56 20

# ~/.zshrc

# Override auto-title when static titles are desired ($ title My new title)
title() { export TITLE_OVERRIDDEN=1; echo -en "\e]0;$*\a"}
# Turn off static titles ($ autotitle)
autotitle() { export TITLE_OVERRIDDEN=0 }; autotitle
# Condition checking if title is overridden
overridden() { [[ $TITLE_OVERRIDDEN == 1 ]]; }
# Echo asterisk if git state is dirty
gitDirty() { [[ $(git status 2> /dev/null | grep -o '\w\+' | tail -n1) != ("clean"|"") ]] && echo "*" }

# Show cwd when shell prompts for input.
precmd() {
   if overridden; then return; fi
   pwd=$(pwd) # Store full path as variable
   cwd=${pwd##*/} # Extract current working dir only
   print -Pn "\e]0;$cwd$(gitDirty)\a" # Replace with $pwd to show full path
}

# Prepend command (w/o arguments) to cwd while waiting for command to complete.
preexec() {
   if overridden; then return; fi
   printf "\033]0;%s\a" "${1%% *} | $cwd$(gitDirty)" # Omit construct from $1 to show args
}

I've commented each step, making it easy to modify the script to your likings (full path vs. basename only, show command with or without args, git dirty state). I've also incorporated @achekulaev's static titles, letting you override auto-titles with your own static titles ($ title My new title), remove the static title with $ autotitle.

@darkdreamingdan
Copy link

For windows bash users, I upgraded to Fall Creators update using the Windows update assistant, and this resolved itself for me 😄

@nathonius
Copy link

nathonius commented Jan 8, 2018

Titles are broken again on Windows (using cmd) when "Use legacy console" is not enabled. Switching to the legacy console works fine, but breaks WSL bash. Clink (and Cmder) had a similar, possibly related issue: mridgers/clink#464

Something about the new conhost. Stanzilla/clink@4217e6d

@mackmmiller
Copy link

mackmmiller commented Jan 9, 2018

I'm running into an issue while using @audunolsen 's zsh script. My titles come out as %15<..<, am I missing something simple?

@audunolsen
Copy link

@mackmmiller: I'm using zsh version 5.3 (x86_64-apple-darwin17.0). I don't use zsh in conjunction with any other framework, e.g. oh-my-zsh, if you are, then maybe there's some clash there.

@ugrupp
Copy link

ugrupp commented Jan 19, 2018

Using zsh with ohmyzsh and hyper-tabs-enhanced and @audunolsen 's solution worked for me. It's even getting the process icons right which is pretty cool.
Took me a good hour to get to this point though.
It might me worthy to note that ohmyzsh has an implementation of title() itself, which got me really confused at first: https://github.com/robbyrussell/oh-my-zsh/blob/master/lib/termsupport.zsh

@ugrupp
Copy link

ugrupp commented Jan 19, 2018

After working with @audunolsen 's solution a bit, I realized there's one problem. Using precmd() and preexec() directly causes conflicts with other zsh plugins which are also using these hooks. In my case, the git plugin wouldn't show the repo status anymore since its hooks were overridden by our tab title functions.

I managed to solve it, thanks to a similar solution posted in direnv/direnv#24. The idea is to attach our handlers to the array of existing handlers, rather than directly defining precmd() and preexec(). So, extending @audunolsen 's solution:

# ~/.zshrc

# Override auto-title when static titles are desired ($ title My new title)
title() { export TITLE_OVERRIDDEN=1; echo -en "\e]0;$*\a"}
# Turn off static titles ($ autotitle)
autotitle() { export TITLE_OVERRIDDEN=0 }; autotitle
# Condition checking if title is overridden
overridden() { [[ $TITLE_OVERRIDDEN == 1 ]]; }
# Echo asterisk if git state is dirty
gitDirty() { [[ $(git status 2> /dev/null | grep -o '\w\+' | tail -n1) != ("clean"|"") ]] && echo "*" }

# Show cwd when shell prompts for input.
tabtitle_precmd() {
   if overridden; then return; fi
   pwd=$(pwd) # Store full path as variable
   cwd=${pwd##*/} # Extract current working dir only
   print -Pn "\e]0;$cwd$(gitDirty)\a" # Replace with $pwd to show full path
}
[[ -z $precmd_functions ]] && precmd_functions=()
precmd_functions=($precmd_functions tabtitle_precmd)

# Prepend command (w/o arguments) to cwd while waiting for command to complete.
tabtitle_preexec() {
   if overridden; then return; fi
   printf "\033]0;%s\a" "${1%% *} | $cwd$(gitDirty)" # Omit construct from $1 to show args
}
[[ -z $preexec_functions ]] && preexec_functions=()
preexec_functions=($preexec_functions tabtitle_preexec)

@alexh
Copy link

alexh commented Feb 18, 2018

I'm getting the same issue as @mackmmiller

@nikitakatchik
Copy link

@audunolsen thanks for your solution but I have a problem with it.
$_ pretty much always contains show_command_in_title_bar.

@nikitakatchik
Copy link

nikitakatchik commented Mar 13, 2018

Seems like this helps.

@marshalhayes
Copy link

marshalhayes commented Mar 15, 2018

screen shot 2018-03-14 at 9 18 22 pm

I just did a clean install of Hyper (and added plugin 'hyper-tab-icons') and the tab titles are not changing. Nor are the icons, like they are supposed to with hyper-tab-icons.

Do I need to do something in order for the title/icons to show?

@Stanzilla Stanzilla added the 📜 Type: Guide Issues has evolved into a helpful document where users help other users label Apr 24, 2018
@ghost
Copy link

ghost commented Apr 27, 2018

@ugrupp I imported your code suggested at #1188 (comment) but I am having issues since the title is all messed up and it looks like this:

Do you happen to know how I may fix it?

@ghost
Copy link

ghost commented Apr 27, 2018

@ugrupp Fortunately I found a quick fix. Since I am using oh-my-zsh I had to uncomment the line DISABLE_AUTO_TITLE="true" in my ~/.zshrc to make it work again.

@El-Dringo-Brannde
Copy link
Contributor

El-Dringo-Brannde commented May 16, 2018

really late to the party, but #3015 fixes this issue. Should work on all shells too.

ghost referenced this issue in sydrawat01/dotfiles May 11, 2020
Added fix to resolve the % problem
@williammu6
Copy link

williammu6 commented Feb 2, 2021

putting it in my ~/.zshrc or ~/.bashrc worked for me

function title() { echo -e "\033]0;$(basename $PWD)" }

@NotesOfReality
Copy link

$host.UI.RawUI.WindowTitle = Get-Location

@stilren hopefully this is still somehow not too late.
It's $(Get-Location) instead of Get-Location.

@NotesOfReality
Copy link

To all the windows powershell (10 or less?I don't use 11) users, the most effective workaround atm seems to be leveraging $Host.UI.RawUI.WindowTitle by a PS profile.

For instance, $host.UI.RawUI.WindowTitle = $(Get-Location) would set the PS window title bar (which is inherited by the tabs extensions of hyper terminal) to the working directory at the time the command is run.
If you want the tab/window title to update put the line in the prompt function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
📜 Type: Guide Issues has evolved into a helpful document where users help other users
Projects
None yet
Development

No branches or pull requests