5

Gnome Terminal allows to have different profiles. Is it possible to get the profile name under which that terminal started from the command line?

4 Answers 4

3

This doesn't seem to be possible, however you can find out the default terminal title, so all you need to do to distinguish between tabs or profiles is to configure different default titles.

The control sequence ESC [ 2 1 t asks the terminal to insert its title on the terminal input stream. (See Xterm control sequences for more information about escape sequences for xterm and similar programs). Specifically, the terminal (if it supports this feature; gnome-terminal does) responds with ESC ] l title ESC \.

Here's a bash function that sets the variable whose name is passed as the first argument to the xterm title, if available. It times out after one second if the terminal doesn't support the feature, and returns a non-zero error code.

read_xterm_title () {
  # Clear IFS so that read doesn't do any word splitting.
  local IFS= read_xterm_title_header=
  # $1 is expected to be a parameter name. Do a crude format check.
  if [[ $1 = '' || $1 = *[!0-9_A-Za-z]* ]]; then return 120; fi
  eval $1=
  # Expect "ESC ] l title ESC \\"
  read -p $'\e[21t' -s -t 1 -r -n 3 read_xterm_title_header &&
  [[ $read_xterm_title_header = $'\e]l' ]] &&
  read -p '' -s -t 1 -r -d $'\e' $1 &&
  read -s -t 1 -r -n 1
}
read_xterm_title title
case $title in ...
1

You used to be able to do this with gconftool2 and then with dconf and now you have to do with gsettings but it's a GUID, so something like this may be what you want:

gsettings get org.gnome.Terminal.ProfilesList default | tr -d \'

and in context of setting a variable:

gsettings set org.gnome.Terminal.Legacy.Profile:/org/gnome/terminal/legacy/profiles:/:$(gsettings get org.gnome.Terminal.ProfilesList default | tr -d \')/ cursor-blink-mode off

per https://wiki.archlinux.org/index.php/GNOME/Tips_and_tricks

1

This seems to do the trick:

gsettings get org.gnome.Terminal.Legacy.Profile:$dconfdir/":"$1/ visible-name

Stolen from here.

2
  • What does $1 mean here? May 10, 2021 at 15:38
  • @IgorMikushkin I can't remember and it's not immediately obvious to me, either, so apologies for the ambiguous answer! Please let me know if you figure it out :) Regardless, it looks like I ultimately went the route of this answer, using gsettings get org.gnome.Terminal.ProfilesList list | tr -d "[]\'," to get the GUID when there is exactly one profile available.
    – c24w
    May 10, 2021 at 16:20
0

It doesn't exactly answer the question. But if you want to distinguish profiles, you may try to check a background color for example.

[[ "$(xtermcontrol --get-bg)" == "rgb:3b3b/3c3c/3e3e" ]] && echo profile-1 || echo profile-2

It returns a background color of the current profile; however, not necessarily the one that was started with, because you can change a profile on a fly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.