如何在VIM中映射CAPS LOCK键?

发布于 2024-08-19 20:14:40 字数 165 浏览 8 评论 0 原文

我在 Windows 下使用 GVIM。 并且想要将 CAPSLOCK 映射到 Ctrl+^

有什么方法可以做到这一点吗?

顺便说一句,我在网上看到了大量如何使用注册表 hack 交换 CAPS 和 Esc 的示例,但它们都没有使用 VIM 映射命令,而是使用外部工具和注册表更改。

I'm using GVIM under Windows.
And want to map CAPSLOCK to Ctrl+^

Any way to do this?

Btw, I see tons of samples over the web how to swap CAPS and Esc using registry hack, but none of them use VIM map command, instead external tools and registry changes.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(16

灼痛 2024-08-26 20:14:40

Linux?对于 X,使用 xmodmap 更改键映射,例如

xmodmap -e 'clear Lock' -e 'keycode 0x42 = Escape'

将 Esc 映射到 CapsLock 键。谷歌获取更多示例。

Linux? With X, use xmodmap to alter the key mapping, e.g.

xmodmap -e 'clear Lock' -e 'keycode 0x42 = Escape'

Will map Esc to the CapsLock key. Google for more examples.

甜`诱少女 2024-08-26 20:14:40

如果您只是为了避免在 Vim 之外工作,您可以将这些行放入 .vimrc 中:

au VimEnter * !xmodmap -e 'clear Lock' -e 'keycode 0x42 = Escape'
au VimLeave * !xmodmap -e 'clear Lock' -e 'keycode 0x42 = Caps_Lock'

当您进入 Vim 时,第一行将转义映射到大写锁定键,第二行在您退出时将正常功能返回到大写锁定。

这需要安装了 xorg-xmodmap 软件包的 Linux。

If your intention is just to avoid working outside of Vim, you can put these lines in your .vimrc:

au VimEnter * !xmodmap -e 'clear Lock' -e 'keycode 0x42 = Escape'
au VimLeave * !xmodmap -e 'clear Lock' -e 'keycode 0x42 = Caps_Lock'

The first line maps escape to the caps lock key when you enter Vim, and the second line returns normal functionality to caps lock when you quit.

This requires Linux with the xorg-xmodmap package installed.

一腔孤↑勇 2024-08-26 20:14:40

对于 Mac OS,您可以在“系统首选项”中重新映射系统范围内的“大写锁定”键。

按照此路径:

系统首选项>键盘>修饰键

然后单击“大写锁定”旁边的下拉框并选择“^ Control”。

For Mac OS, you can remap the 'caps lock' key system wide in 'system preferences'.

Follow this path:

system preferences > keyboard > modifier keys

Then click the drop down box next to 'caps lock' and choose '^ Control'.

回忆那么伤 2024-08-26 20:14:40

窗户下?使用AutoHotkey。这不是 vim 映射,但正如其他人所说,你无法映射它。我使用 AHK 将 CAPSLOCK 映射到 CTRL。

Under windows? Use AutoHotkey. It's not a vim mapping, but as the others have stated you can't map it. I use AHK to map my CAPSLOCK to CTRL.

时光无声 2024-08-26 20:14:40

在 Linux 系统中,这可以使用 xmodmap 来完成。

将其保存在主文件夹中的文本文件中 使用

! Swap caps lock and escape
remove Lock = Caps_Lock
keysym Escape = Caps_Lock
keysym Caps_Lock = Escape
add Lock = Caps_Lock

.capstoescswitc 之类的名称保存此文件

然后通过终端执行此文件。

xmodmap ~/.capstoescswitc 

如果想要尊重它,只需切换脚本文件中的关键变量即可。

有关更多信息,请参阅此页面

In Linux systems this can be done with xmodmap.

Save this in a text file in the home folder

! Swap caps lock and escape
remove Lock = Caps_Lock
keysym Escape = Caps_Lock
keysym Caps_Lock = Escape
add Lock = Caps_Lock

Save this file with a name like .capstoescswitc

Then execute this file via the terminal.

xmodmap ~/.capstoescswitc 

If want to reveres it simply switch the key variables in the script file.

For more info refer this page

梦醒灬来后我 2024-08-26 20:14:40

Capslock(以及 Control、Shift 等)是修饰键,这意味着它与另一个普通键一起使用来修改该键的含义。 AFAIK 操作系统不会将修改键传递给应用程序,除非也按下了普通键,例如应用程序不会看到按 CTRL,但按 CTRL-C将。

Capslock (and Control, and Shift etc.) is a modifier key, which means that it's used with another normal key to modify the meaning of that key. AFAIK the OS does not pass the modifier keys to the application unless a normal key has also been pressed, e.g. pressing CTRL will not be seen by the application, but CTRL-C will be.

说好的呢 2024-08-26 20:14:40

安装自动热键之外不会破坏 Caps Lock 的解决方案

在 vim Windows

  1. 运行 autohotkey 脚本:
;caps_to_esc.ahk
#IfWinActive, ahk_class Vim ; vim window class
Capslock::Esc
#IfWinActive

Ubuntu

运行此命令:

wget -O - https://raw.githubusercontent.com/grabantot/scripts/master/install/install_caps_to_esc.sh | bash

或手动执行这些操作:

  1. sudo apt-get install xdotool xbindkeys。我们还将使用 xpropxset (应该默认安装)。
  2. 创建~/caps_to_esc.sh脚本:
debug_file=/dev/shm/caps_to_esc.debug
debug_msg () {
  echo $(date +%s%3N) "$@" >> $debug_file
}

caps_off () {
  is_caps_on="false"
  xset q | grep "Caps Lock:\s*on" && is_caps_on="true"
  debug_msg "is_caps_on ""$is_caps_on"

  [ "$is_caps_on" == "false" ] && return 3
  debug_msg "Sending Caps Lock"
  debug_msg "ignore_next"
  xdotool key Caps_Lock
}

should_ignore="false"
tail -n 1 $debug_file | grep "ignore_next" && should_ignore="true"

if [ "$should_ignore" == "true" ]; then
  debug_msg "ignored"
  exit 1
fi

echo -n "" > $debug_file

# get wm_class by 'xprop | grep WM_CLASS'
declare -a wm_classes=( \
  'WM_CLASS(STRING) = "gnome-terminal-server", "Gnome-terminal"' \
  'WM_CLASS(STRING) = "gvim", "Gvim"' \
  'WM_CLASS(STRING) = "code", "Code"' \
  'WM_CLASS(STRING) = "google-chrome", "Google-chrome"' \
)

active_window_id=$(xdotool getactivewindow)
active_window_wm_class=$(xprop -id $active_window_id WM_CLASS)
debug_msg "active_wm_class   ""$active_window_wm_class"

detected_wm_class=""
for wm_class in "${wm_classes[@]}"; do
  # debug_msg "$wm_class"
  if [ "$active_window_wm_class" == "$wm_class" ]; then
    detected_wm_class="$wm_class"
    debug_msg "detected_wm_class ""$detected_wm_class"
  fi
done

[ "$detected_wm_class" == "" ] && exit 2
xdotool keyup "Caps_Lock" # !!! very important
caps_off
debug_msg "Sending Escape"
xdotool key "Escape"
debug_msg "sent"
  1. 将新的bindnig添加到~/.xbindkeysrc
"bash $HOME/caps_to_esc.sh"
Caps_Lock
  1. killall xbindkeys && xbindkeys

工作原理:

  1. xbindkeys 将检测何时按下 Caps_Lock 并调用脚本
  2. 中的 caps_to_esc.sh 脚本通过 xprop 检测活动窗口 wm_class code>
  3. 检查 wm_class 是否对我们感兴趣(gnome-terminal、vscode、gvim、chrome),如果不感兴趣则退出
  4. 通过 xdotool
  5. 检查 Caps Lock 是否已打开 通过 xset 如果是,则通过 xdotool 发送 Caps_Lock 键
  6. xbindkeys 将检测我们发送的 Caps_Lock,但我们会忽略它

Solution that doesn't break Caps Lock outside of vim

Windows

  1. Install autohotkey.
  2. Run autohotkey script:
;caps_to_esc.ahk
#IfWinActive, ahk_class Vim ; vim window class
Capslock::Esc
#IfWinActive

Ubuntu

Run this command:

wget -O - https://raw.githubusercontent.com/grabantot/scripts/master/install/install_caps_to_esc.sh | bash

Or perform these actions manually:

  1. sudo apt-get install xdotool xbindkeys. We will also use xprop and xset (should be installed by default).
  2. Create a ~/caps_to_esc.sh script:
debug_file=/dev/shm/caps_to_esc.debug
debug_msg () {
  echo $(date +%s%3N) "$@" >> $debug_file
}

caps_off () {
  is_caps_on="false"
  xset q | grep "Caps Lock:\s*on" && is_caps_on="true"
  debug_msg "is_caps_on ""$is_caps_on"

  [ "$is_caps_on" == "false" ] && return 3
  debug_msg "Sending Caps Lock"
  debug_msg "ignore_next"
  xdotool key Caps_Lock
}

should_ignore="false"
tail -n 1 $debug_file | grep "ignore_next" && should_ignore="true"

if [ "$should_ignore" == "true" ]; then
  debug_msg "ignored"
  exit 1
fi

echo -n "" > $debug_file

# get wm_class by 'xprop | grep WM_CLASS'
declare -a wm_classes=( \
  'WM_CLASS(STRING) = "gnome-terminal-server", "Gnome-terminal"' \
  'WM_CLASS(STRING) = "gvim", "Gvim"' \
  'WM_CLASS(STRING) = "code", "Code"' \
  'WM_CLASS(STRING) = "google-chrome", "Google-chrome"' \
)

active_window_id=$(xdotool getactivewindow)
active_window_wm_class=$(xprop -id $active_window_id WM_CLASS)
debug_msg "active_wm_class   ""$active_window_wm_class"

detected_wm_class=""
for wm_class in "${wm_classes[@]}"; do
  # debug_msg "$wm_class"
  if [ "$active_window_wm_class" == "$wm_class" ]; then
    detected_wm_class="$wm_class"
    debug_msg "detected_wm_class ""$detected_wm_class"
  fi
done

[ "$detected_wm_class" == "" ] && exit 2
xdotool keyup "Caps_Lock" # !!! very important
caps_off
debug_msg "Sending Escape"
xdotool key "Escape"
debug_msg "sent"
  1. Add new bindnig to ~/.xbindkeysrc:
"bash $HOME/caps_to_esc.sh"
Caps_Lock
  1. killall xbindkeys && xbindkeys

How it works:

  1. xbindkeys will detetect when Caps_Lock is pressed and call caps_to_esc.sh script
  2. in the script detect active window wm_class by xprop
  3. check if wm_class is of interest for us (gnome-terminal, vscode, gvim, chrome), exit if it is not
  4. send Escape key via xdotool
  5. check if Caps Lock is on via xset and if it is then send Caps_Lock key via xdotool
  6. xbindkeys will detect the Caps_Lock sent by us but we ignore it
烟若柳尘 2024-08-26 20:14:40

我认为你不能。
我相信 CAPS-LOCK 可能在 vim 看到它之前就被操作系统翻译了。
因此,您需要在操作系统级别进行黑客攻击,就像您已经看到的注册表黑客攻击一样。

编辑:autohotkey 看起来可以用来弥合 vim-OS 差距。这样,第三方应用程序就可以在操作系统级别进行黑客攻击,而您只需挂钩该应用程序即可。

I dont think you can.
I believe CAPS-LOCK is probably translated by the OS before vim ever sees it.
So you'd need to do a hack at the OS level, like the registry hacks you've already seen.

EDIT: autohotkey looks like it could be used to bridge the vim-OS gap. This way a thirdparty app is doing the hacks at the OS level, and you're just hooking that app.

无所的.畏惧 2024-08-26 20:14:40

@rsoren 的回答有效。但问题是,如果打开多个缓冲区,从一个缓冲区退出,也会恢复所有其他缓冲区的映射。用 BufEnter 和 BufLeave 替换 VimEnter 和 VimLeave 对我来说很有效。

au BufEnter * silent! !xmodmap -e 'clear Lock' -e 'keycode 0x42 = Escape'
au BufLeave * silent! !xmodmap -e 'clear Lock' -e 'keycode 0x42 = Caps_Lock'

@rsoren's answer works. But the problem with that is if multiple buffers are opened, exiting from one, reverts the mapping for all of the others too. Replacing VimEnter and VimLeave with BufEnter and BufLeave, did the trick for me.

au BufEnter * silent! !xmodmap -e 'clear Lock' -e 'keycode 0x42 = Escape'
au BufLeave * silent! !xmodmap -e 'clear Lock' -e 'keycode 0x42 = Caps_Lock'
土豪我们做朋友吧 2024-08-26 20:14:40

由于有针对 Linux 和 Windows(Autohotkey) 的解决方案,我想建议使用 Mac 版 pckeyboardhack 可在各处重新映射 CapsLock。

Since there is a solution for Linux and Windows(Autohotkey), I´d like to suggest to use pckeyboardhack for Mac to remap CapsLock everywhere.

故人爱我别走 2024-08-26 20:14:40

在 Windows 上将 CAPSLOCK 重新映射为 ESCCTRL

如果您想将 CAPSLOCK 重新映射

  1. ESC< /code>(单独按下时)
  2. CTRL(与其他键一起按下时)
    您可以使用 ililim 的这个小开源软件

您不需要管理员权限来执行此操作,ESCCTRL 仍按预期工作。

我用它来享受方便的 CTRL + 任何东西 按下而不伤害我的小指,并切换我在 MobaXterm 中通过 ssh 使用的 Vim 模式。如需安装,请按照此说明进行操作。

在 Linux 上将 CAPSLOCK 重新映射为 ESCCTRL

要在 Linux 上执行相同的操作,您可以使用 XCAPE。要在 Ubuntu 上安装它,请使用:

sudo apt install xcape

对于实际映射,请执行以下操作:

setxkbmap -option ctrl:nocaps
xcape -e 'Control_L=Escape'

第一个命令是将 CAPSLOCK 映射到 ESC,而第二个命令负责 CTRL > 用其他键按下。

要使其在 X 会话中永久工作,您可以将其添加到 ~/.xprofile 中。

Remap CAPSLOCK to ESC and CTRL on Windows

If you want to remap CAPSLOCK to both

  1. to ESC (when pressed alone)
  2. to CTRL (when pressed with other keys)
    you can use this little piece of open source software by ililim.

You do not need Admin privileges for this and ESC and CTRL are still working as expected.

I use it to enjoy convenient CTRL + anything presses without hurting my pinky and to toggle modes in Vim that I use via ssh in MobaXterm. For installation just follow this description.

Remap CAPSLOCK to ESC and CTRL on Linux

To do the same on Linux, you can use XCAPE. To install it on Ubuntu use:

sudo apt install xcape

For the actual mapping do:

setxkbmap -option ctrl:nocaps
xcape -e 'Control_L=Escape'

The 1st command is to map CAPSLOCK to ESC whereas the 2nd one takes care of CTRL pressed with other keys.

To have this permanently working in X sessions, you can add this to ~/.xprofile.

年华零落成诗 2024-08-26 20:14:40

我自己查看了这个问题的答案,但我正在寻找稍微不同的东西:

  1. 我正在使用 Linux
  2. 我很乐意更改 CAPS 即使在 Vim 之外 strong>(我几乎总是错误地按下,无论如何我都可以将其保留在 Esc 按钮上,以达到我使用它的目的)
  3. 我想尽可能使用最常用的软件,而不是如果需要任何进一步的安装,
  4. 我更愿意 KISS,甚至避免进一步的需要冗长的脚本
  5. 我想同时拥有 CAPS ->; Esc AND CAPS + ; -> Ctrl + ,如果可能的话

那么我当前的解决方案是:

# ~/.xprofile
xmodmap $HOME/.xmodmap

并且:

! ~/.xmodmap
clear lock
keycode 66 = Escape
keycode 9 = Caps_Lock
add lock = Caps_Lock

clear control
add control = Control_L Control_R Escape

看起来它暂时像一个魅力,但我正在寻找反馈:)

I went through the answers to this question to do it my own, but I was looking for something slightly different:

  1. I'm using Linux
  2. I'm happy to change CAPS even outside Vim (I press almost always by mistake, and I can keep it on the Esc button anyhow for the goals I'm using it)
  3. I'd like to use the most common software as possible, not to require any further installation
  4. I'd prefer to KISS, and then even avoid the need of further lengthy scripts
  5. I'd like to have both CAPS -> Esc AND CAPS + <something> -> Ctrl + <something>, if possible

Then my current solution it is:

# ~/.xprofile
xmodmap $HOME/.xmodmap

and:

! ~/.xmodmap
clear lock
keycode 66 = Escape
keycode 9 = Caps_Lock
add lock = Caps_Lock

clear control
add control = Control_L Control_R Escape

Looks like it's working like a charm for the time being, but I'm looking for feedbacks :)

尛丟丟 2024-08-26 20:14:40

我想这样做的原因之一是创建一个软大写锁定,就像其他人提到的那样,可能是为了避免在正常模式下保持大写锁定。
我使用 vimcaps 插件在离开插入时关闭大写锁定模式,似乎工作正常。

I guess one of the reasons for doing this is to create a soft capslock, like others have mentioned, possibly to avoid keeping capslock on while in normal mode.
I've used the vimcaps plugin to turn off capslock when leaving insert mode, seems to work okay.

随波逐流 2024-08-26 20:14:40

在 Mac 上,也可以使用 Karabiner (https://pqrs.org/osx/karabiner/)

$brew cask 安装 karabiner-elements

安装后,您可以在简单修改选项卡中将 capslock 键映射到 esc 键。需要注意的是,这是系统范围内的,这意味着您在任何地方都会丢失大写锁定键。 IMO 需要大写锁定。

On mac, it is also possible to use Karabiner (https://pqrs.org/osx/karabiner/)

$ brew cask install karabiner-elements

Once installed, you can map capslock key to esc key in the simple modifications tab. Caveat is this is system wide, meaning that you lose capslock key everywhere. IMO who needs capslock.

预谋 2024-08-26 20:14:40

截至 2020 年,对于像我这样的普通 WINDOWS 用户来说,一个工作示例是使用 AutoHotkey

目前使用这个 1 行脚本:

Capslock::Esc

运行一下,问题就解决了。希望有帮助。 :)

A working example for a mere Mortal WINDOWS user like me as of 2020 is using AutoHotkey

Currently using this 1 line script:

Capslock::Esc

Run it and your problem is solved. Hope it helps. :)

暮光沉寂 2024-08-26 20:14:40

对于 X (Linux),正如 @Dan Andreatta 提到的,使用 xmodmap 更改键映射,例如

xmodmap -e 'clear Lock' -e 'keycode 0x42 = Escape'

Esc 映射到 CapsLock 键。

您还可以通过将其添加到 X 初始化文件(例如 .xinitrc)(在使用 startx 时运行)中来将其固定下来:

clear lock
keycode 0x42 = Escape

通常,将其放入.Xmodmap 文件,然后在 .xinitrc 内运行。

Vim 的文档有一整页专门讨论这个主题,评论部分特别有用。

With X (Linux), as @Dan Andreatta mentioned, use xmodmap to alter the key mapping, e.g.

xmodmap -e 'clear Lock' -e 'keycode 0x42 = Escape'

Will map Esc to the CapsLock key.

You could also set this in stone by adding it into an X initialization file, such as .xinitrc — which is run when using startx —:

clear lock
keycode 0x42 = Escape

Typically, this is put inside an .Xmodmap file and then it's run inside .xinitrc.

Vim's documentation has an entire page dedicated to this topic, and the comments section is especially helful.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文