LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-17-2019, 04:48 PM   #1
Oliems
LQ Newbie
 
Registered: Nov 2016
Distribution: Debian
Posts: 19

Rep: Reputation: Disabled
Post How to make a bash script to switch between color schemes


Hello,

I would like to know how to make a bash script to make changes to .Xresources in order to switch between different color schemes and, if it is possible, to apply the changes without having to close and reopen the terminal.

I am using urxvt and i3.

Here is my .Xresources file :

Code:
! Solarized color scheme for the X Window System
!
! http://ethanschoonover.com/solarized


! Common

#define S_yellow        #b58900
#define S_orange        #cb4b16
#define S_red           #dc322f
#define S_magenta       #d33682
#define S_violet        #6c71c4
#define S_blue          #268bd2
#define S_cyan          #2aa198
#define S_green         #859900


! Dark

! #define S_base03        #002b36
! #define S_base02        #073642
! #define S_base01        #586e75
! #define S_base00        #657b83
! #define S_base0         #839496
! #define S_base1         #93a1a1
! #define S_base2         #eee8d5
! #define S_base3         #fdf6e3


! Light

#define S_base03        #fdf6e3
#define S_base02        #eee8d5
#define S_base01        #93a1a1
#define S_base00        #839496
#define S_base0         #657b83
#define S_base1         #586e75
#define S_base2         #073642
#define S_base3         #002b36


! To only apply colors to your terminal, for example, prefix
! the color assignment statement with its name. Example:
!
! URxvt*background:            S_base03

URxvt*background:              S_base03
URxvt*foreground:              S_base0
URxvt*fading:                  40
URxvt*fadeColor:               S_base03
URxvt*cursorColor:             S_base1
URxvt*pointerColorBackground:  S_base01
URxvt*pointerColorForeground:  S_base1

URxvt*color0:                  S_base02
URxvt*color1:                  S_red
URxvt*color2:                  S_green
URxvt*color3:                  S_yellow
URxvt*color4:                  S_blue
URxvt*color5:                  S_magenta
URxvt*color6:                  S_cyan
URxvt*color7:                  S_base2
URxvt*color9:                  S_orange
URxvt*color8:                  S_base03
URxvt*color10:                 S_base01
URxvt*color11:                 S_base00
URxvt*color12:                 S_base0
URxvt*color13:                 S_violet
URxvt*color14:                 S_base1
URxvt*color15:                 S_base3

URxvt*font:             xft:monospace:size=14
URxvt*inheritPixmap:    false 
URxvt*transparent: 	false
! URxvt*shading: 	110
! URxvt*tint:		white
URxvt*scrollBar:        false
URxvt*cursorBlink:      false
For example, If I want to switch between light and dark mode, I have to uncomment the corresponding lines, then use xrdb to reload .Xresources and finally close and reopen the terminals for the changes to take effect.

Thanks in advance
 
Old 06-17-2019, 06:22 PM   #2
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
keep some originals somewhere safe for a backup!
how do you open a terminal?
make a script that calls to switch out the file, then calls to open a terminal that uses same said switched file, this way you get different colors in different terminals.

this is what I do with one of my terminals
Code:
#!/bin/bash
#BASH Internals 
#to give a random background image to
#mrxvt terminal
#by chaning its resource file
#.mrxvtrc
wd=/media/data1/EtermBGs
mapfile -t ImageA < <(find "$wd" -type f)
sed -i 's|.*mrxvt.Pixmap:.*|mrxvt.Pixmap: '" $( echo ${ImageA[ $RANDOM % ${#ImageA[@]}]})"';70x80|' $HOME/.mrxvtrc
mrxvt &
it sed's config file first changing the bg image then calls to open the terminal. So I get a different image in each terminal I open that is mrxvt, and works like a charm in my case.

so in i3 keyboard short cut calls your script first, then that script calls your terminal.
keeping your different setups for terminal.
Code:
#!/bin/bash

if grep -q test1 $HOME/base/ckfile ; 
then
	cp $HOME/base/filetwo $HOME/base/ckfile
else
	cp $HOME/base/fileone $HOME/base/ckfile
fi

#call terminal here.
in your two different setup files to be copied as .Xresources kept in a safe place. add within them a comment, I used test1, test2 in two different files, and within the ckfile I added !test1. ran that script a few time to look in the ckfile to see if it had changed. so yes.. this work.

you could if you like just use that logic and do your own file to suit your needs.

Last edited by BW-userx; 06-17-2019 at 07:17 PM.
 
Old 06-18-2019, 01:03 AM   #3
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
Various ways!

Includes in ~/.Xresources (*):
Code:
#include </home/ondoho/.local/share/xorg/includes/theme1.xres>
So the file theme1 contains only the desired colors and nothing else.
To change a theme, you could use sed:
Code:
sed -i ... ~/.Xresources
(of course you should back up that file first. you have been warned.)
I leave it to your search-fu to figure out "..."

But even easier is to simply merge the theme into the xrdb database, which exists only in memory:
Code:
xrdb -merge /home/ondoho/.local/share/xorg/includes/theme1.xres
This remains valid until you restart Xorg, so you need to add a script to your startup application that chooses the right theme and performs that merge every time you start X.
It's definitely less intrusive that way.

Quote:
Originally Posted by Oliems View Post
if it is possible, to apply the changes without having to close and reopen the terminal.
with urxvt this is not possible.

(*) My ~/.Xresources contains much more than just a terminal color scheme.

Last edited by ondoho; 06-18-2019 at 01:06 AM.
 
1 members found this post helpful.
Old 06-18-2019, 02:43 PM   #4
Oliems
LQ Newbie
 
Registered: Nov 2016
Distribution: Debian
Posts: 19

Original Poster
Rep: Reputation: Disabled
Thanks for your help guys, I'll manage to do it on my own from then on
 
Old 06-19-2019, 01:28 AM   #5
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
Let us know how you solved it in the end
 
Old 06-19-2019, 03:51 PM   #6
Oliems
LQ Newbie
 
Registered: Nov 2016
Distribution: Debian
Posts: 19

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by ondoho View Post
Let us know how you solved it in the end
I have made a ~/.themes folder with different copies of the original .Xresources file with just changes made to the color related part, then bind a shortcut for each of them in i3 to load them using xrdb

Last edited by Oliems; 06-19-2019 at 04:17 PM. Reason: typo
 
1 members found this post helpful.
  


Reply

Tags
bash scripting, color scheme, xresources



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
vim color schemes only work with sudo tnma Linux - Newbie 1 12-10-2009 10:13 AM
Script to change the Color Schemes in KDE Automatically AJones Linux - Software 3 11-24-2009 02:04 AM
emacs color schemes Four Linux - Newbie 1 10-19-2006 12:11 PM
emacs color schemes Four Linux - Newbie 2 10-08-2006 02:53 PM
dark color schemes-white text doesnt show up in firefox, etc aw76 Linux - General 5 12-01-2005 10:16 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:53 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration