3

I know how to change the Terminal Window title. What I am trying to find out is how to make bash not zsh write out the currently running process so if I say do

$ ls -lF

I would get something like this for the title

/home/me/curerntFolder (ls -lF)

Getting the last executed command would be too late since the command has executed already, so it won't set the title with the command that was executed.

Archimedes Trajano
  • 35,625
  • 19
  • 175
  • 265

5 Answers5

7

In addition to @markp-fuso's answer, here's how I did it to make it work with Starship.

  function set_win_title() {
    local cmd=" ($@)"
    if [[ "$cmd" == " (starship_precmd)" || "$cmd" == " ()" ]]
    then
      cmd=""
    fi
    if [[ $PWD == $HOME ]]
    then
      if [[ $SSH_TTY ]]
      then
        echo -ne "\033]0; ️ @ $HOSTNAME ~$cmd\a" < /dev/null
      else
        echo -ne "\033]0;  ~$cmd\a" < /dev/null
      fi
    else
      BASEPWD=$(basename "$PWD")
      if [[ $SSH_TTY ]]
      then
        echo -ne "\033]0; ️ $BASEPWD @ $HOSTNAME $cmd\a" < /dev/null
      else
        echo -ne "\033]0;  $BASEPWD $cmd\a" < /dev/null
      fi
    fi

  }
  starship_precmd_user_func="set_win_title"
  eval "$(starship init bash)"
  trap "$(trap -p DEBUG |  awk -F"'" '{print $2}');set_win_title \${BASH_COMMAND}" DEBUG

Note this differs from the Custom pre-prompt and pre-execution Commands in Bash instructions in that the trap is set after starship init. Which I have noted in a bug.

Archimedes Trajano
  • 35,625
  • 19
  • 175
  • 265
3

UPDATE: my previous answer (below) displays the previous command in the title bar.

Ignoring everything from my previous answer and starting from scratch:

trap 'echo -ne "\033]0;${PWD}: (${BASH_COMMAND})\007"' DEBUG

Running the following at the command prompt:

$ sleep 10

The window title bar changes to /my/current/directory: (sleep 10) while the sleep 10 is running.

Running either of these:

$ sleep 1; sleep 2; sleep 3
$ { sleep 1; sleep2; sleep 3; }

The title bar changes as each sleep command is invoked.

Running this:

$ ( sleep 1; sleep 2; sleep 3 )

The title bar does not change (the trap does not apply within a subprocess call).

One last one:

$ echo $(sleep 3; echo abc)

The title bar displays (echo $sleep 3; echo abc)).


previous answer

Adding to this answer:

store_command() {
  declare -g last_command current_command
  last_command=$current_command
  current_command=$BASH_COMMAND
  return 0
}
trap store_command DEBUG

PROMPT_COMMAND='echo -ne "\033]0;${PWD}: (${last_command})\007"'

Additional reading materials re: trap / DEBUG:

markp-fuso
  • 28,790
  • 4
  • 16
  • 36
1

You can combine setting the window title with setting the prompt.

Here's an example using s PROMPT_COMMAND:

tputps () {
    echo -n '\['
    tput "$@"
    echo -n '\]'
}

prompt_builder () {
    # Window title - operating system command (OSC) ESC + ]
    echo -ne '\033]0;'"${USER}@${HOSTNAME}:$(dirs)"'\a' >&2

    # username, green
    tputps setaf 2
    echo -n '\u'

    # directory, orange
    tputps setaf 208
    echo -n ' \w'
    tputps sgr0 0
}

prompt_cmd () {
    PS1="$(prompt_builder) \$ "
}

export PROMPT_COMMAND=prompt_cmd
Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
1

For Linux OS Adding following function in bashrc file

Following Steps

  1. Open bashrc file

vi ~/.bashrc

  1. Write a function in bashrc file

    function set-title() {
       if [[ -z "$ORIG" ]]; then
         ORIG=$PS1
       fi
       TITLE="\[\e]2;$*\a\]"
       PS1=${ORIG}${TITLE}
     }

enter image description here

  1. save the file

source ~/.bashrc

  1. call function

set-title "tab1"

enter image description here

Omkesh Sajjanwar
  • 575
  • 8
  • 13
-1

The easiest way to change the title of the terminal I could think of is to use echo in shell script

echo "\033]0;Your title \007"

And to change open a new tab with new title name is

meta-terminal--tab-t"Your title"