X 资源 - Arch Linux 中文维基

跳转到内容

# X 资源

来自 Arch Linux 中文维基

相关文章

- X Logical Font Description
- dotfiles

翻译状态：

- 本文（或部分内容）译自 X resources，最近一次同步于 2024-8-1，若英文版本有所更改，则您可以帮助同步与翻译更改的内容。
- 您可以在 ArchWiki 的对应页面找到本文翻译的原始修订历史。
- 本文可能与英文原文存在出入。

本文或本节需要翻译。要贡献翻译，请访问简体中文翻译团队。

附注： 请提供模板的第一个位置参数以更详细的指示。（在 Talk:X 资源# 中讨论）

X 资源 文件是一个用户级配置点文件，通常位于 `~/.Xresources`。 它可用于设置X 资源，即 X 客户端程序的配置参数。

除其他用途外，它们可用于：

- 配置终端首选项（如终端颜色）
- 设置 DPI、抗锯齿、hinting 和其他 X 字体设置
- 更改 Xcursor 主题
- 主题 XScreenSaver
- 配置低级 X 应用程序，如 xorg-xclock包、xpdf包、rxvt-unicode)

## 安装

安装 xorg-xrdb包 软件包来获得 X 服务器资源数据库工具，安装 xorg-docs包 来获得 X.org 文档。

## 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

- Color output in console#Terminal emulators
- Cursor themes#X resources
- Font configuration#Applications without fontconfig support
- rxvt-unicode#Configuration
- xpdf(1) § OPTIONS
- Xterm#Configuration
- klassiker (mrdotx) — Rxvt-unicode patch developer dotfiles

## 用法

### 加载资源文件

资源存储在 X 服务器中，因此只需读取一次。远程X11 客户端（如通过 SSH 转发）也可以访问这些资源。

加载资源文件（如传统的 `.Xresources`），替换任何当前设置：

    $ xrdb ~/.Xresources

加载资源文件，并与当前设置合并：

    $ xrdb -merge ~/.Xresources

注意：

- 大多数显示管理器会在登录时加载 `~/.Xresources` 文件。
- X11 程序启动时会读取较旧的 `~/.Xdefaults` 文件，但前提是当前会话中未使用过 xrdb。[1]

### xinitrc

如果使用默认的 xinitrc 副本作为 `.xinitrc` ，它已经合并了 `~/.Xresources`。

如果您使用的是自定义的，请添加：

    .xinitrc

    [[ -f ~/.Xresources ]] && xrdb -merge -I$HOME ~/.Xresources

注意：请勿在 `~/.xinitrc` 中设置 xrdb 命令的后台。否则，在 xrdb 之后启动的程序可能会在加载完成前查找资源。

### 获取资源值

如果要获取某个资源的值（例如在 bash 脚本中使用），可以使用 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.

## 参见

- Using the .Xdefaults File 由 Purdue Engineering Computer Network 提供
- Overlooked Points of X Resources 由 Thomas Dickey 提供

分类：​

- 配置文件
- X 服务器

隐藏分类：​

- 使用“模板:Translateme”标记的页面或章节

X 资源
添加话题