27

I would like to setup my gnome terminal's background(#002b36) and foreground color in ubuntu 13, using bash script.

I tried gconftool but couldn't succeed.

GCONFTOOL-2(1)                  User Commands                                                    GCONFTOOL-2(1)

NAME
       gconftool-2 - GNOME configuration tool

My gnome terminal version is

$ gnome-terminal --version
GNOME Terminal 3.6.1

enter image description here

Currently I'm using ubuntu terminal preferences UI to achieve this.

enter image description here

3 Answers 3

24

Method #1 - Using dconf

Background

You can use the dconf tool to accomplish this, however it's a mult-step process.

DESCRIPTION
       The dconf program can perform various operations on a dconf database, 
       such as reading or writing individual values or entire directories.
       This tool operates directly on the dconf database and does not read 
       gsettings schema information.Therefore, it cannot perform type and 
       consistency checks on values. The gsettings(1) utility is an 
       alternative if such checks are needed.

Usage

$ dconf
error: no command specified

Usage:
  dconf COMMAND [ARGS...]

Commands:
  help              Show this information
  read              Read the value of a key
  list              List the contents of a dir
  write             Change the value of a key
  reset             Reset the value of a key or dir
  update            Update the system databases
  watch             Watch a path for changes
  dump              Dump an entire subpath to stdout
  load              Populate a subpath from stdin

Use 'dconf help COMMAND' to get detailed help.

General approach

  1. First you'll need to get a list of your gnome-terminal profiles.

    $ dconf list /org/gnome/terminal/legacy/profiles:/
    <profile id>
    
  2. Using this <profile id> you can then get a list of configurable settings

    $ dconf list /org/gnome/terminal/legacy/profiles:/<profile id>
    background-color
    default-size-columns
    use-theme-colors
    use-custom-default-size
    foreground-color
    use-system-font
    font
    
  3. You can then read the current colors of either the foreground or background

    foreground

    $ dconf read /org/gnome/terminal/legacy/profiles:/<profile id>/foreground-color
    'rgb(255,255,255)'
    

    background

    $ dconf read /org/gnome/terminal/legacy/profiles:/<profile id>/background-color
    'rgb(0,0,0)'
    
  4. You can change the colors as well

    foreground

    $ dconf write /org/gnome/terminal/legacy/profiles:/<profile id>/foreground-color "'rgb(255,255,255)'"
    

    background

    $ dconf write /org/gnome/terminal/legacy/profiles:/<profile id>/background-color "'rgb(0,0,0)'"
    

Example

  1. Get my profile ID

    $ dconf list /org/gnome/terminal/legacy/profiles:/
    :b1dcc9dd-5262-4d8d-a863-c897e6d979b9/
    
  2. Use the profile ID to get a list of settings

    $ dconf list /org/gnome/terminal/legacy/profiles:/:b1dcc9dd-5262-4d8d-a863-c897e6d979b9/
    background-color
    default-size-columns
    use-theme-colors
    use-custom-default-size
    foreground-color
    use-system-font
    font
    
  3. Change your background blue

    $ dconf write /org/gnome/terminal/legacy/profiles:/:b1dcc9dd-5262-4d8d-a863-c897e6d979b9/background-color "'rgb(0,0,255)'"
    

              ss #1

A Note on colors

You can use either the notation rgb(R,G,B) when specifying your colors or the hash notation #RRGGBB. In the both notations the arguments are red, green, and blue. The values in the first notation are integers ranging from 0-255 for R, G, or B. In the second notation the values are in hexidecimal ranging from 00 to FF for RR, GG, or BB.

When providing either of these to dconf you need to wrap it properly in double quotes with single quotes nested inside. Otherwise dconf will complain.

  • "'rgb(0,0,0)'"
  • "'#FFFFFF'"
  • etc.

Method #2 - Using gconftool-2

On my Ubuntu 12.04 system I was able to change the colors via the command line as follows.

NOTE: The options are ultimately stored in this file, $HOME/.gconf/apps/gnome-terminal/profiles/Default/%gconf.xml.

General approach

  1. First you'll need to get the tree for gnome-terminal's profile.

    $ gconftool-2 --get /apps/gnome-terminal/global/profile_list
    [Default]
    
  2. Using the resulting tree we can find out what attributes are configurable.

    $ gconftool-2 -a "/apps/gnome-terminal/profiles/Default" | grep color
     bold_color_same_as_fg = true
     bold_color = #000000000000
     background_color = #FFFFFFFFFFFF
     foreground_color = #000000000000
     use_theme_colors = false
    
  3. Get/Set the background_color & foreground_color attributes

    $ gconftool-2 --get "/apps/gnome-terminal/profiles/Default/foreground_color"
    #000000000000
    
    $ gconftool-2 --set "/apps/gnome-terminal/profiles/Default/background_color" --type string "#000000FFFFFF"    
    
  4. Confirm

    $ gconftool-2 -R /apps/gnome-terminal/profiles/Default | grep color
     bold_color_same_as_fg = true
     bold_color = #000000000000
     background_color = #000000FFFFFF
     foreground_color = #000000000000
     use_theme_colors = true
    

References

10
  • 4
    Loads of thanks for this great answer. But my terminal gave no profiles on dconf list /org/gnome/terminal/legacy/profiles:/ Commented Jun 1, 2014 at 13:20
  • @PrayagUpd - I'm looking at a Ubuntu 12.04 install and it seems like gnome-terminal is not the default. Debian X terminal emulator is the def. You can check w/ this command under Utilities tab, exo-preferred-applications. Details are here: askubuntu.com/questions/356842/…. Can you confirm this? The above was done on a Fedora 19 GNOME 3.10 install.
    – slm
    Commented Jun 1, 2014 at 14:15
  • Yes, Debian X terminal emulator was the default in 13.04, changing it to gnome terminal didn't work either. Commented Jun 2, 2014 at 3:53
  • @PrayagUpd - does method #2 work for you?
    – slm
    Commented Jun 3, 2014 at 2:35
  • 1
    as of Ubuntu 18.04 you now must use sudo dconf whatever when issuing commands Commented Aug 22, 2018 at 16:24
1

I have created some functions, based on Github code from other threads. You can put these functions in your ~/.bashrc file. As you can see, if you call create_random_profile:

  1. It will check and delete any previous random profile you have created.
  2. It will create a random name profile in gnome terminals.
  3. It will set that name in an environment variable that you can use to change your color in predefined functions. See the last function, setcolord.

This should be useful, to have many terminals with different colors. Besides, with predefined functions you can change these colors on the fly.

function create_random_profile() {
    #delete previous profiles in case there were something
    #delete_one_random_profile
    prof="`mktemp -u HACK_PROFILE_XXXXXXXXXX`"
    gconftool-2 --set "/apps/gnome-terminal/profiles/$prof/use_theme_colors" --type bool false
    gconftool-2 --type list --list-type string --set $prof_list "`gconftool-2 --get $prof_list | sed "s/]/,$prof]/"`"
    file="`mktemp`"
    gconftool-2 --dump "/apps/gnome-terminal/profiles/Default" | sed "s,profiles/$2,profiles/$prof,g" > "$file"
    gconftool-2 --load "$file"
    gconftool-2 --type string --set "/apps/gnome-terminal/profiles/$prof/visible_name" "$prof"
    rm -f -- "$file"
    export __TERM_PROF=$prof
}

function delete_one_random_profile() {
    regular="HACK_PROFILE_"
    prof=$(gconftool-2 --get /apps/gnome-terminal/global/profile_list | sed -n "s/.*\(HACK_PROFILE_..........\).*/\1/p")
    if [ ! -z "$prof"]; then
        echo "size ${#prof}"
        echo "size of regular ${#regular}"
        echo "DO DELETE of $prof"
        #if not empty
        gconftool-2 --type list --list-type string --set $prof_list "`gconftool-2 --get $prof_list | sed "s/$prof//;s/\[,/[/;s/,,/,/;s/,]/]/"`"
        gconftool-2 --unset "/apps/gnome-terminal/profiles/$prof"
    else
        echo "NOTHING TO DELETE"
    fi
}

function setcolord() {
    echo "Dont forget to change to Profile0 in the menu of your terminal->Change Profile->Profile_0"
    gconftool-2 --set "/apps/gnome-terminal/profiles/$__TERM_PROF/background_color" --type string white
    gconftool-2 --set "/apps/gnome-terminal/profiles/$__TERM_PROF/foreground_color" --type string black
}

function setcolor_cyan() {
    echo "Dont forget to change to $__TERM_PROF in the menu of your terminal->Change Profile->Profile_0"
    gconftool-2 --set "/apps/gnome-terminal/profiles/$__TERM_PROF/background_color" --type string "#8DCBCC"
    gconftool-2 --set "/apps/gnome-terminal/profiles/$__TERM_PROF/foreground_color" --type string black
}
0

The Gogh theme picker has lots of scripts for adding themes to your Gnome terminal preferences. The simplest is just to see my answer to another question for using Gogh.

However if you want to look into the raw scripts this is roughly how it works.

You can see the basic bash variables it uses for the colours, for example https://github.com/Gogh-Co/Gogh/blob/master/themes/solarized-light.sh:

export COLOR_01="#073642"           # HOST
export COLOR_02="#DC322F"           # SYNTAX_STRING
export COLOR_03="#859900"           # COMMAND
export COLOR_04="#B58900"           # COMMAND_COLOR2
export COLOR_05="#268BD2"           # PATH
export COLOR_06="#D33682"           # SYNTAX_VAR
export COLOR_07="#2AA198"           # PROMP
export COLOR_08="#EEE8D5"           #

export COLOR_09="#002B36"           #
export COLOR_10="#CB4B16"           # COMMAND_ERROR
export COLOR_11="#586E75"           # EXEC
export COLOR_12="#657B83"           #
export COLOR_13="#839496"           # FOLDER
export COLOR_14="#6C71C4"           #
export COLOR_15="#93A1A1"           #
export COLOR_16="#FDF6E3"           #

export BACKGROUND_COLOR="#FDF6E3"   # Background Color
export FOREGROUND_COLOR="#657B83"   # Text
BOLD_COLOR="#586E75"         # Bold
export CURSOR_COLOR="$FOREGROUND_COLOR" # Cursor
export PROFILE_NAME="Solarized Light"

This then uses those parameters with https://github.com/Gogh-Co/Gogh/blob/master/apply-colors.sh. This would appear to be the core of it:

set_theme() {
  dset visible-name                    "'${PROFILE_NAME}'"
  dset background-color                "'${BACKGROUND_COLOR}'"
  dset foreground-color                "'${FOREGROUND_COLOR}'"
  dset cursor-colors-set               "true"
  dset cursor-background-color         "'${CURSOR_COLOR}'"
  dset cursor-foreground-color         "'${BACKGROUND_COLOR}'"

  if [[ -n "${HIGHLIGHT_BG_COLOR:-}" ]]; then
    dset   highlight-colors-set        "true"
    dset   highlight-background-color  "'${HIGHLIGHT_BG_COLOR}'"
    if [[ -n "${HIGHLIGHT_FG_COLOR:-}" ]]; then
      dset   highlight-foreground-color  "'${HIGHLIGHT_FG_COLOR}'"
    fi
  fi

  if [[ -n "${BOLD_COLOR:-}" ]]; then
    dset   bold-color                  "'${BOLD_COLOR}'"
    dset   bold-color-same-as-fg       "false"
  else
    dset   bold-color                  "'${FOREGROUND_COLOR}'"
    dset   bold-color-same-as-fg       "true"
  fi
  dset     use-theme-colors            "false"
  dset     use-theme-background        "false"
  dset     use-theme-transparency      "${USE_SYS_TRANSPARENCY:-false}"
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .