Ubuntu – How to get the current GNOME terminal profile

dconfgnome-terminalgsettings

I know about gsettings and dconf, but I can't find any mention of active profile. It seems that you can only list profiles without knowing what is the current profile.

I want to query background color to set background in Vim appropriately.

Best Answer

The question was modified in comments: How to get the background color? For this, the command xtermcontrol --get-bg can be used.

This particular command relies on the terminal emulator supporting the OSC 11 escape sequence, which usually goes together with OSC 12 (foreground; xtermcontrol --get-fg), OSC 4 (palette colors; xtermcontrol --get-color0 .. --get-color15) and a few others. Some popular terminal emulators implement these, some other popular ones don't, so these commands don't work everywhere. The VTE widget, and hence in turn VTE-based terminal emulators such as gnome-terminal do support these.

These escape sequences are quite tricky when they are used for querying the colors rather than setting them: The response is injected by the terminal emulator exactly as if it came from the keyboard, that is, as if the user typed it. xtermcontrol reads this input and then prints as output, hiding this technical detail. (And if you really wish to, you can fool xtermcontrol to report a wrong answer by manually typing one from the keyboard, haha.)


Back to the original question:

There is no way to query the current profile in gnome-terminal. Why?

  • Reading gsettings or dconf cannot work for two reasons:

    1. They contain the static configuration (settings) of gnome-terminal, not runtime information. Even if they contained runtime information, they'd have to contain the information for all the open gnome-terminal windows and tabs, each of which might be using a separate profile. And then somehow we'd still need to be able to identify which one to look for.

    2. They can only work locally, not across e.g. a su or ssh.

  • gnome-terminal could initialize an environment variable when a window or tab is opened, but there's no way to update it subsequently if the profile is changed. (See also the discussion about $COLORFGBG at https://bugzilla.gnome.org/show_bug.cgi?id=733423.)

  • It could report the current profile as a response to some escape sequence, as if the user typed it from the keyboard (similarly to the aforementioned OSC 4, 10, 11). However, there's no standard for this, and it would even have security implications (see https://bugzilla.gnome.org/show_bug.cgi?id=151260), plus presumably no support in the xtermcontrol utility for many years to come (heck it doesn't even support 256 colors yet).