X 資源

出自 Arch Linux 中文维基

本文或本節需要翻譯。要貢獻翻譯,請訪問簡體中文翻譯團隊

附註: 請提供模板的第一個位置參數以更詳細的指示。(在 Talk:X 資源# 中討論)

X resources file is a user-level configuration dotfile, typically located at ~/.Xresources. It can be used to set X resources, which are configuration parameters for X client applications.

Among other things they can be used to:

Installation[編輯 | 編輯原始碼]

Install the xorg-xrdb package for X server resource database utility and xorg-docs for X.org documentations.

Configuration[編輯 | 編輯原始碼]

X(7) § RESOURCES and XrmGetDatabase(3) § FILE SYNTAX provide detailed information on X resources mechanism and file syntax.

~/.Xresources is a conventional file name, xrdb does not claim it. You can use any other file names, like ~/.config/X11/Xresources and ~/.config/X11/Xresources.d/application-name (also see #Samples and #Include files).

Basic syntax[編輯 | 編輯原始碼]

The syntax of an X resources file is a sequence of resource lines as follows:

application_name.Class.resourceName: value
      application_name.resourceName: value
                 Class.resourceName: value
      application_name*resourceName: value
                      *resourceName: value

application_name and Class substrings will never contain a dot (.), the resourceName substring may contain a dot. For example, Dialog.bodyFont is a XScreenSaver internal resource that is specified to set the body font and fallback font:

xscreensaver-auth.default.Dialog.bodyFont: times new roman 12, dejavu serif 12
application_name
The name of the application, such as urxvt, xpdf, xterm, etc. Also may be called the instance name.
Class
The classification used to group resources together. Class names are typically uppercase.
resourceName
The name of the resource whose value is to be set. Resources are typically lowercase with uppercase concatenation.
value
The actual value of the resource. This can be one of three types:
  • Integer (whole numbers)
  • Boolean (true/false, yes/no, on/off)
  • String (a string of characters) — for example a word (white), a color (#ffffff), or a path (/usr/bin/firefox)
delimiters
  • A dot (.) is a tight binding and is used to separate immediately adjacent components (in other words, to signify each step down into the hierarchy) — in the above example we start at application name, then descend into Class, and finally into the resource itself.
  • An asterisk (*) is a loose binding and is used to represent any number of components, including none.
  • A colon (:) is used to separate the resource name from the value.
注意:
  • From resource file syntax point of view, everything before a colon (:) is a resource name. From the user perspective, we often call resource only the rightmost component. In other words, resource name is a string consisted of application name, Class and resource name substrings. That might be the source of confusion.
  • Resource naming schema is totally application-dependent. While one application might use application_name.Class.resourceName, another might understand application_name.resourceName and Class.resourceName only.

Wildcard matching[編輯 | 編輯原始碼]

Question mark (?) and asterisk (*) can be used as wildcards, making it easy to write a single rule that can be applied to many different applications or elements. ? is used to match any single component name, while * is used to represent any number of intervening components including none.

Using the previous example, if you want to apply the same font to all programs (not just XScreenSaver) that contain the class name Dialog which contains the resource name headingFont, you could write:

?.Dialog.headingFont:     -*-fixed-bold-r-*-*-*-100-*-*-*-*-iso8859-1

If you want to apply this same rule to all programs that contain the resource headingFont, regardless of its class, you could write:

*headingFont:    -*-fixed-bold-r-*-*-*-100-*-*-*-*-iso8859-1
注意:
  • Question mark (?) is like component name, so binding character — dot (.) or asterisk (*) — next to it is required.
  • Asterisk (*) is a binding character itself, there is no need in placing dot next to it, and a sequence of dot(s) and asterisk(s) is replaced with a single asterisk during file processing.

See XrmGetResource(3) § MATCHING RULES for more information.

Comments[編輯 | 編輯原始碼]

Lines starting with an exclamation mark (!) are ignored, for example:

! The following rule will be ignored because it has been commented out
!Xft.antialias: true
注意:
  • The exclamation mark must be the first character on the line.
  • If you have a C preprocessor, such as GNU CPP (gcc), installed you can use C-style (// and /* … */) comments (also see #Samples).

Include files[編輯 | 編輯原始碼]

To spread resource configuration across multiple files (e.g. to use its own file for each application), use C preprocessor #include directive:

~/.config/X11/Xresources
#include "Xresources.d/fonts"
#include "Xresources.d/rxvt-unicode"
#include "Xresources.d/xscreensaver"
#include "Xresources.d/xterm"

If files that are referenced with #include are not reachable from the applied configuration file directory, you need to pass a directory to search for:

$ xrdb -load -I${HOME}/.config/X11 ~/.Xresources

Default settings[編輯 | 編輯原始碼]

To see the default settings for your installed X11 applications, look in /usr/share/X11/app-defaults/.

Detailed information on program-specific resources is usually provided in the man page for the program. xterm(1) § RESOURCES is a good example, as it contains a list of X resources and their default values.

To see the currently loaded resources:

$ xrdb -query -all

Samples[編輯 | 編輯原始碼]

Usage[編輯 | 編輯原始碼]

Load resource file[編輯 | 編輯原始碼]

Resources are stored in the X server, so have to only be read once. They are also accessible to remote X11 clients (such as those forwarded over SSH).

Load a resource file (such as the conventional .Xresources), replacing any current settings:

$ xrdb ~/.Xresources

Load a resource file, and merge with the current settings:

$ xrdb -merge ~/.Xresources
注意:
  • Most Display managers load the ~/.Xresources file on login.
  • The older ~/.Xdefaults file is read when an X11 program starts, but only if xrdb has not been used in the current session. [1]

xinitrc[編輯 | 編輯原始碼]

If you are using a copy of the default xinitrc as your .xinitrc it already merges ~/.Xresources.

If you are using a custom one, add:

.xinitrc
[[ -f ~/.Xresources ]] && xrdb -merge -I$HOME ~/.Xresources
注意: Do not background the xrdb command within ~/.xinitrc. Otherwise, programs launched after xrdb may look for resources before it has finished loading them.

Getting resource values[編輯 | 編輯原始碼]

If you want to get the value of a resource (for example if you want to use it in a bash script) you can use xgetresAUR:

$ xgetres xscreensaver.Dialog.headingFont
-*-fixed-bold-r-*-*-*-100-*-*-*-*-iso8859-1

Troubleshooting[編輯 | 編輯原始碼]

Parsing errors[編輯 | 編輯原始碼]

Display managers such as GDM may use the --nocpp argument for xrdb.

No output from xrdb -query[編輯 | 編輯原始碼]

It is not rare for xrdb -query to output nothing. Try following #Load resource file and #xinitrc from above. And note some of the files mentioned there could be empty.

See also[編輯 | 編輯原始碼]