22

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

20

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

  • 3
    Loads of thanks for this great answer. But my terminal gave no profiles on dconf list /org/gnome/terminal/legacy/profiles:/ – prayagupd Jun 1 '14 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 Jun 1 '14 at 14:15
  • Yes, Debian X terminal emulator was the default in 13.04, changing it to gnome terminal didn't work either. – prayagupd Jun 2 '14 at 3:53
  • @PrayagUpd - does method #2 work for you? – slm Jun 3 '14 at 2:35
  • 1
    as of Ubuntu 18.04 you now must use sudo dconf whatever when issuing commands – Scott Stensland Aug 22 '18 at 16:24
0

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
}

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.