在 Linux 上重新映射你的大写锁定键
大写锁定 键盘

> 
> 通过在 GNOME 3 和 Wayland 上重新映射你的键盘，提高你的打字和导航速度，避免重复性压力伤害。
> 
> 
> 


![](/data/attachment/album/202105/22/174755hs0dbmm4idl5rbrr.jpg "Emoji keyboard")


对我来说，有许多改变生活的 Linux 时刻，但大多数都在成为现状后淡忘了。有一个 Linux 教给我的键盘小技巧，每次我使用它的时候（也许每天有 1000 次），我都会想起这件事，那就是把大写锁定键转换为 `Ctrl` 键。


我从不使用大写锁定键，但我整天使用 `Ctrl` 键进行复制、粘贴、在 [Emacs](https://opensource.com/article/20/12/emacs) 内导航，以及 [调用 Bash](https://opensource.com/article/18/5/bash-tricks#key)、[GNU Screen](https://opensource.com/article/17/3/introduction-gnu-screen) 或 [tmux](https://opensource.com/article/19/6/tmux-terminal-joy) 等操作。大写锁定键在我的键盘上占据了宝贵的空间，而将实际上有用的 `Ctrl` 键挤到了难以触及的底部角落。


![手指放在键盘上](/data/attachment/album/202105/22/174804eyso4s2wsyvvgc9g.jpg "Fingers on a keyboard")


*这看起来就痛苦*


重新映射 `Ctrl` 提高了我的打字和导航速度，并可能使我免受重复性压力伤害。


### 消失的控制


系好安全带，这是个过山车式的历史课。


对于像我这样的大写锁定键交换者来说，不幸的是，当 GNOME 3 问世时，它几乎删除了改变 `Ctrl` 键位置的功能。


幸运的是，优秀的 GNOME Tweaks 应用程序带回了这些 “失踪” 的控制面板。


不幸的是，[GNOME 40](https://discourse.gnome.org/t/new-gnome-versioning-scheme/4235) 没有 GNOME Tweaks 应用程序（还没有？）


另外，不幸的是，过去在 X11 上可以工作的老的 `xmodmap` 技巧在新的 [Wayland 显示服务器](https://wayland.freedesktop.org) 上没有用。


有一小段时间（最多一个下午），我觉得对于那些讨厌大写锁定键的人来说人生都灰暗了。然后我想起我是一个开源的用户，总有一种方法可以解决诸如被忽略的 GUI 控制面板之类的简单问题。


### dconf


GNOME 桌面使用 dconf，这是一个存储重要配置选项的数据库。它是 GSettings 的后端，GSettings 是 GNOME 系统应用程序需要发现系统偏好时的接口。你可以使用 `gsetting` 命令查询 dconf 数据库，也可以使用 `dconf` 命令直接设置 dconf 的键值。


### GSettings


dconf 数据库不一定是你可能称为可发现的数据库。它是一个不起眼的数据库，你通常不需要去考虑它，它包含了许多通常无需直接交互的数据。然而，如果你想更好地了解 GNOME 所要管理的所有偏好选项，那么浏览它是很有趣的。


你可以用 `list-schemas` 子命令列出所有 dconf 的模式。在浏览了数百个模式之后，你可以使用 [grep](https://opensource.com/downloads/grep-cheat-sheet) 将你的注意力缩小到一些看起来特别相关的东西上，比如 `org.gnome.desktop`。



```
$ gsettings list-schemas | grep ^org.gnome.desktop
[...]
org.gnome.desktop.background
org.gnome.desktop.privacy
org.gnome.desktop.remote-desktop.vnc
org.gnome.desktop.interface
org.gnome.desktop.default-applications.terminal
org.gnome.desktop.session
org.gnome.desktop.thumbnailers
org.gnome.desktop.app-folders
org.gnome.desktop.notifications
org.gnome.desktop.sound
org.gnome.desktop.lockdown
org.gnome.desktop.default-applications.office

```

无论是通过手动搜索还是通过 [阅读 GSetting 文档](https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/8/html/using_the_desktop_environment_in_rhel_8/configuring-gnome-at-low-level_using-the-desktop-environment-in-rhel-8)，你可能会注意到 `org.gnome.desktop.input-sources` 模式，它有助于定义键盘布局。从设计上来说，GSetting 模式包含了键和值。


### 用 dconf 重新映射大写字母锁


`xkb-options` 键包含了可选的键盘覆写。要设置这个键值，请使用`dconf`，将上面模式中的点（`.`）转换为斜线（`/`），因为 dconf 数据库需要使用 `/`。



```
$ dconf write /org/gnome/desktop/input-sources/xkb-options "['caps:ctrl_modifier']"

```

我把 `caps` 设置为 `ctrl_modifier`，因为我使用 `Ctrl` 修饰键的次数多于其他修饰键，但 Vim 用户可能喜欢把它设置为 `escape`。


### 查看你的设置


这个改变会立即生效，并在重启后仍然生效。这是你在 GNOME 中定义的首选项，在你改变它之前一直有效。


你可以通过 `gsettings` 查看 dconf 中的新值。首先，查看可用的键：



```
$ gsettings list-keys \
    org.gnome.desktop.input-sources
xkb-options
mru-sources
show-all-sources
current
per-window
sources

```

然后用 `xkb-options` 键名查看设置：



```
$ gsettings get \
    org.gnome.desktop.input-sources \
    xkb-options
['caps:ctrl_modifier']

```

### 选项丰富


我在我的 GNOME 3.4 系统上使用这个小技巧来设置大写锁定键以及 [Compose](https://opensource.com/article/17/5/7-cool-kde-tweaks-will-improve-your-life) 键（`compose:ralt`）。虽然我相信正在开发中的 GUI 控件可以控制这些选项，但我也不得不承认，能以编程方式设置这些选项的能力是我的荣幸。作为以前没有可靠方法来调整桌面设置的系统的管理员，能够用命令修改我的首选项使得设置新桌面变得快速而容易。


GSettings 提供了很多有用的选项，而且文档也很详尽。如果你有想要改变的东西，可以看看有什么可用的。




---


via: <https://opensource.com/article/21/5/remap-caps-lock-key-linux>


作者：[Seth Kenlon](https://opensource.com/users/seth) 选题：[lujun9972](https://github.com/lujun9972) 译者：[wxy](https://github.com/wxy) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
There have been many life-changing Linux moments for me, but most fade into my backstory as they become the status quo. There's one little keyboard trick Linux taught me that I'm reminded of every time I use it (maybe 1,000 times a day), and that's converting the **Caps Lock** key to **Ctrl**.

I never use **Caps Lock**, but I use the **Ctrl** key all day for copying, pasting, navigating within [Emacs](https://opensource.com/article/20/12/emacs), and [invoking Bash](https://opensource.com/article/18/5/bash-tricks#key), [GNU Screen](https://opensource.com/article/17/3/introduction-gnu-screen), or [tmux](https://opensource.com/article/19/6/tmux-terminal-joy) actions. **Caps Lock** occupies valuable real estate on my keyboard, forcing the actually useful **Ctrl** key down to the awkward-to-reach bottom corner.

![Fingers on a keyboard Fingers on a keyboard](https://opensource.com/sites/default/files/uploads/bendy-fingers.jpg)

This is as painful as it looks. (Seth Kenlon, CC BY-SA 4.0)

Remapping **Ctrl** increased my typing and navigation speed and has probably saved me from repetitive stress injuries.

## The case of the disappearing control


Buckle in, this is a roller coaster of a history lesson:

Unfortunately for **Caps Lock** swappers like me, when GNOME 3 came out, it all but removed the ability to change the location of the **Ctrl** key.

Fortunately, the excellent GNOME Tweaks app brought back these "missing" control panels.

Unfortunately, [GNOME 40](https://discourse.gnome.org/t/new-gnome-versioning-scheme/4235) has no GNOME Tweaks app (yet?)

Also, unfortunately, the old `xmodmap`

hack that used to work on X11 is useless on the new [Wayland display server](https://wayland.freedesktop.org).

For a short while (an afternoon at best), I felt things were looking dim for people who hate **Caps Lock**. Then I remembered I am a user of open source, and there's *always* a way around something as simple as an overlooked GUI control panel.

## dconf

The GNOME desktop uses dconf, a database that stores important configuration options. It's the backend to GSettings, which is the system GNOME applications interface with when they need to discover system preferences. You can query the dconf database using the `gsetting`

command, and you can set dconf key values directly with the `dconf`

command.

## GSettings

The dconf database isn't necessarily what you might call discoverable. It's a humble database you're not meant to have to think about, and it holds a lot of data you usually don't have to interact with directly. However, it does use a sensible schema that's fun to browse if you want to better understand all of the preference options GNOME has to manage.

You can list all of dconf's schemas with the `list-schemas`

subcommand. After browsing hundreds of schemas, you might use [grep](https://opensource.com/downloads/grep-cheat-sheet) to narrow your focus to something that seems especially relevant, such as `org.gnome.desktop`

:

```
$ gsettings list-schemas | grep ^org.gnome.desktop
[...]
org.gnome.desktop.background
org.gnome.desktop.privacy
org.gnome.desktop.remote-desktop.vnc
org.gnome.desktop.interface
org.gnome.desktop.default-applications.terminal
org.gnome.desktop.session
org.gnome.desktop.thumbnailers
org.gnome.desktop.app-folders
org.gnome.desktop.notifications
org.gnome.desktop.sound
org.gnome.desktop.lockdown
org.gnome.desktop.default-applications.office
```


Whether through a manual search or through [reading GSetting documentation](https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/8/html/using_the_desktop_environment_in_rhel_8/configuring-gnome-at-low-level_using-the-desktop-environment-in-rhel-8), you may notice the `org.gnome.desktop.input-sources`

schema, which helps define the keyboard layout. A GSetting schema, by design, contains keys and values.

## Remapping Caps Lock with dconf

The `xkb-options`

key contains optional keyboard overrides. To set this key, use `dconf`

, converting the dots (`.`

) in the schema above to slashes (`/`

) because the dconf database requires it:

`$ dconf write /org/gnome/desktop/input-sources/xkb-options "['caps:ctrl_modifier']"`


I set `caps`

to `ctrl_modifier`

because I use the **Ctrl** modifier more than any other modifier, but Vim users may prefer to set it to `escape`

instead.

## View your setting

The change takes effect immediately and persists across reboots. It's a preference you've defined in GNOME, so it remains in effect until you change it.

You can view the new value in `dconf`

with `gsettings`

. First, view the available keys:

```
$ gsettings list-keys \
org.gnome.desktop.input-sources
xkb-options
mru-sources
show-all-sources
current
per-window
sources
```


And then view the settings with the `xkb-options`

key:

```
$ gsettings get \
org.gnome.desktop.input-sources \
xkb-options
['caps:ctrl_modifier']
```


## Options aplenty

I use this little trick to set **Caps Lock** as well as the [Compose](https://opensource.com/article/17/5/7-cool-kde-tweaks-will-improve-your-life) key (`compose:ralt`

) on my GNOME 3.4 system. While I believe there are GUI controls in development to control options like these, I also have to admit that the ability to set them programmatically is a luxury I enjoy. As a former admin of systems that had no reliable way to adjust desktop settings, the ability to script my preferences makes setting up a fresh desktop quick and easy.

There are lots of useful options available with GSettings, and the documentation is thorough. If you have something you want to change, take a look at what's available.

## 2 Comments