如何在VIM中映射CAPS LOCK键?
我在 Windows 下使用 GVIM。 并且想要将 CAPSLOCK 映射到 Ctrl+^
有什么方法可以做到这一点吗?
顺便说一句,我在网上看到了大量如何使用注册表 hack 交换 CAPS 和 Esc 的示例,但它们都没有使用 VIM 映射命令,而是使用外部工具和注册表更改。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(16)
Linux?对于 X,使用 xmodmap 更改键映射,例如
将 Esc 映射到 CapsLock 键。谷歌获取更多示例。
Linux? With X, use xmodmap to alter the key mapping, e.g.
Will map Esc to the CapsLock key. Google for more examples.
如果您只是为了避免在 Vim 之外工作,您可以将这些行放入 .vimrc 中:
当您进入 Vim 时,第一行将转义映射到大写锁定键,第二行在您退出时将正常功能返回到大写锁定。
这需要安装了 xorg-xmodmap 软件包的 Linux。
If your intention is just to avoid working outside of Vim, you can put these lines in your .vimrc:
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.
对于 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'.
窗户下?使用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.
在 Linux 系统中,这可以使用 xmodmap 来完成。
将其保存在主文件夹中的文本文件中 使用
.capstoescswitc 之类的名称保存此文件
然后通过终端执行此文件。
如果想要尊重它,只需切换脚本文件中的关键变量即可。
有关更多信息,请参阅此页面
In Linux systems this can be done with xmodmap.
Save this in a text file in the home folder
Save this file with a name like .capstoescswitc
Then execute this file via the terminal.
If want to reveres it simply switch the key variables in the script file.
For more info refer this page
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, butCTRL-C
will be.安装自动热键之外不会破坏
Caps Lock
的解决方案在 vim Windows
Ubuntu
运行此命令:
或手动执行这些操作:
sudo apt-get install xdotool xbindkeys
。我们还将使用xprop
和xset
(应该默认安装)。~/caps_to_esc.sh
脚本:~/.xbindkeysrc
:killall xbindkeys && xbindkeys
工作原理:
xbindkeys
将检测何时按下 Caps_Lock 并调用脚本caps_to_esc.sh
脚本通过xprop 检测活动窗口 wm_class
code>xdotool
xset
如果是,则通过xdotool
发送 Caps_Lock 键xbindkeys
将检测我们发送的 Caps_Lock,但我们会忽略它Solution that doesn't break
Caps Lock
outside of vimWindows
Ubuntu
Run this command:
Or perform these actions manually:
sudo apt-get install xdotool xbindkeys
. We will also usexprop
andxset
(should be installed by default).~/caps_to_esc.sh
script:~/.xbindkeysrc
:killall xbindkeys && xbindkeys
How it works:
xbindkeys
will detetect when Caps_Lock is pressed and callcaps_to_esc.sh
scriptxprop
xdotool
xset
and if it is then send Caps_Lock key viaxdotool
xbindkeys
will detect the Caps_Lock sent by us but we ignore it我认为你不能。
我相信 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.
@rsoren 的回答有效。但问题是,如果打开多个缓冲区,从一个缓冲区退出,也会恢复所有其他缓冲区的映射。用 BufEnter 和 BufLeave 替换 VimEnter 和 VimLeave 对我来说很有效。
@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.
由于有针对 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.
在 Windows 上将
CAPSLOCK
重新映射为ESC
和CTRL
如果您想将
CAPSLOCK
重新映射ESC< /code>(单独按下时)
CTRL
(与其他键一起按下时)您可以使用 ililim 的这个小开源软件。
您不需要管理员权限来执行此操作,
ESC
和CTRL
仍按预期工作。我用它来享受方便的
CTRL + 任何东西
按下而不伤害我的小指,并切换我在 MobaXterm 中通过 ssh 使用的 Vim 模式。如需安装,请按照此说明进行操作。在 Linux 上将
CAPSLOCK
重新映射为ESC
和CTRL
要在 Linux 上执行相同的操作,您可以使用 XCAPE。要在 Ubuntu 上安装它,请使用:
对于实际映射,请执行以下操作:
第一个命令是将
CAPSLOCK
映射到ESC
,而第二个命令负责CTRL
> 用其他键按下。要使其在 X 会话中永久工作,您可以将其添加到
~/.xprofile
中。Remap
CAPSLOCK
toESC
andCTRL
on WindowsIf you want to remap
CAPSLOCK
to bothESC
(when pressed alone)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
andCTRL
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
toESC
andCTRL
on LinuxTo do the same on Linux, you can use XCAPE. To install it on Ubuntu use:
For the actual mapping do:
The 1st command is to map
CAPSLOCK
toESC
whereas the 2nd one takes care ofCTRL
pressed with other keys.To have this permanently working in X sessions, you can add this to
~/.xprofile
.我自己查看了这个问题的答案,但我正在寻找稍微不同的东西:
CAPS ->; Esc
ANDCAPS +; -> Ctrl + ,如果可能的话
那么我当前的解决方案是:
并且:
看起来它暂时像一个魅力,但我正在寻找反馈:)
I went through the answers to this question to do it my own, but I was looking for something slightly different:
CAPS -> Esc
ANDCAPS + <something> -> Ctrl + <something>
, if possibleThen my current solution it is:
and:
Looks like it's working like a charm for the time being, but I'm looking for feedbacks :)
我想这样做的原因之一是创建一个软大写锁定,就像其他人提到的那样,可能是为了避免在正常模式下保持大写锁定。
我使用 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.
在 Mac 上,也可以使用 Karabiner (https://pqrs.org/osx/karabiner/)
安装后,您可以在简单修改选项卡中将 capslock 键映射到 esc 键。需要注意的是,这是系统范围内的,这意味着您在任何地方都会丢失大写锁定键。 IMO 需要大写锁定。
On mac, it is also possible to use Karabiner (https://pqrs.org/osx/karabiner/)
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.
截至 2020 年,对于像我这样的普通 WINDOWS 用户来说,一个工作示例是使用 AutoHotkey
目前使用这个 1 行脚本:
运行一下,问题就解决了。希望有帮助。 :)
A working example for a mere Mortal WINDOWS user like me as of 2020 is using AutoHotkey
Currently using this 1 line script:
Run it and your problem is solved. Hope it helps. :)
对于 X (Linux),正如 @Dan Andreatta 提到的,使用
xmodmap
更改键映射,例如将 Esc 映射到 CapsLock 键。
您还可以通过将其添加到 X 初始化文件(例如
.xinitrc
)(在使用startx
时运行)中来将其固定下来:通常,将其放入
.Xmodmap
文件,然后在.xinitrc
内运行。Vim 的文档有一整页专门讨论这个主题,评论部分特别有用。
With X (Linux), as @Dan Andreatta mentioned, use
xmodmap
to alter the key mapping, e.g.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 usingstartx
—: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.