.Xdefaults vs .Xresources
January 14th, 2012 pesaMany X programs have user-level configuration options that can be set in the X server’s resource database.
The two most common ways to achive persistent configuration for your X programs are to store them in either ~/.Xdefaults
or ~/.Xresources
files.
I will not go into detail of the syntax of these files. However, there are some things to take into consideration between the choice of the two files.
Consider the X program xterm
. You want to make the background black and the foreground white. This can be achived by passing some command line arguments to xterm
:
$ xterm -bg black -fg white
This is fine if you want this for only one time, but if you want it to be the default you put it in a configuration file.
XTerm*background: black
XTerm*foreground: white
If you put this in the ~/.Xdefaults
file you will notice that the next time you start an xterm
it has the new colors. Great! Or, is it?
It is easy to change configuration options and it will be in effect next time you start the program, but it comes at the cost that basically all X programs needs to parse this file everytime you run one.
A better way is to put the configuration options in the ~/.Xresources
file and store the configuration in the X server’s resource database by using the xrdb
command:
$ xrdb -merge ~/.Xresources
Doing this, (in simple terms) xterm
will “ask” the X server for its resource options rather than reading a file. This is much quicker and avoids unnecessary reads on disk.
Another great thing about this is that remote X programs (e.g., tunneled through SSH) will be able to catch these options.
Once configurations are read into the X server’s resource databse (using xrdb
), X programs should not look for configuration options in ~/.Xdefaults
file.
As many desktop environments (like Gnome or KDE) will store the configurtion from both files into the resource database upon the start of X, the drawbacks stated above may not be a problem.
However, if you don’t run a desktop environment (like me) you need to manually run xrdb
when starting X (e.g., by adding xrdb
command line above to the ~/.xinitrc
file).
If you run xrdb
manually, you can of course name the ~/.Xresources
file whatever you want, but it’s the defacto standard.