Xorg – Difference Between .Xresources and .Xdefaults

xorg

These two files seem to have the same function. What is the difference between the two if any?

Best Answer

~/.Xdefaults is the older method of storing X resources. This file is re-read every time an Xlib program is started. If X11 is used over the network, the file must be present on the same filesystem as the programs.

~/.Xresources is newer. It is loaded with xrdb into the RESOURCE_MANAGER property of the X11 root window. Whenever any program looks up a resource, it is read straight from RESOURCE_MANAGER.

If this property does not exist, Xlib falls back to the old method of reading .Xdefaults on every program startup. Note that most distributions will load ~/.Xresources automatically if it is present, causing .Xdefaults to be ignored even if you have never run xrdb manually.

The advantage of the new method is that it's enough to call xrdb once, and the resources will be available to any program running on this display, whether local or remote. (The name ~/.Xresources is only a convention – you can use xrdb to load any file, even .Xdefaults.)

Xlib Programming Manual P.441:

Prior to X11R2, X resource settings were read from .Xdefaults file in users home directory and optionally on whatever machine the X client was running on, so multiple files was hard to maintain.

Later on, xrdb program was made to store users resource settings from in .Xresources into the XA_RESOURCE_MANAGER property of the root window on the current X server, so all clients connected to the same server has access to them. If the user hasn't called xrdb to set the property, then .Xdefaults is read.

Wikipedia:

[...] the X resources are stored in two standard locations, depending on whether they apply to all screens or to a particular one:

  • the RESOURCE_MANAGER property of the root window of screen 0
  • the SCREEN_RESOURCES property of the root window of an arbitrary screen

It doesn't quite end at that.

  • There also is the $XENVIRONMENT variable, which defaults to ~/.Xdefaults-hostname if not set. This is used in the same way as .Xdefaults, but is always read regardless of whether RESOURCE_MANAGER is present. You can use .Xdefaults-hostname files to keep some settings machine-specific while using xrdb for the global ones.

Both items #4 and #5 listed below appear to be used only by pure Xlib programs – not GTK 3 or other toolkits.

  • The fourth location is the directory pointed to by the $XAPPLRESDIR environment variable. (Oddly, if the variable is not set, $HOME is used as the default.) When a program is started, it looks if any of the following files exist (the file name being the same as the program's class name):

    • $XAPPLRESDIR/$LC_CTYPE/XTerm
    • $XAPPLRESDIR/language/XTerm
    • $XAPPLRESDIR/XTerm

    (language is derived from $LC_CTYPE by stripping all but the first component; for example, en_US.utf-8en.)

  • The fifth location is the system-wide "app-defaults" directories. Again, the app-defaults directories are checked on program startup if they have a file named after the program. For example, XTerm (on Arch Linux) uses:

    • /etc/X11/$LC_CTYPE/app-defaults/XTerm
    • /etc/X11/language/app-defaults/XTerm
    • /etc/X11/app-defaults/XTerm
    • /usr/share/X11/$LC_CTYPE/app-defaults/XTerm
    • /usr/share/X11/language/app-defaults/XTerm
    • /usr/share/X11/app-defaults/XTerm

    The app-defaults files are usually installed into /usr/share along with the program itself; administrator overrides would go to /etc.


Everything mentioned above is documented in great detail in the X.org toolkit documentation – article X Toolkit Intrinsics - C Language Interface, section Loading the Resource Database.

Unfortunately, I could not find any recent user guide that describes X resources, mostly because they are irrelevant in year 2012. But for historical (read: horribly out of date) information, you can read X Window System user's guide for X11 R3 and R4 of the X Window System section 9: Setting Resources (starting with page 181).

Related Question