如何以不同方式映射 Ctrl+A 和 Ctrl+Shift+A?

发布于 2024-08-06 07:46:34 字数 389 浏览 4 评论 0原文

在终端中,无法区分 Ctrl+ACtrl+Shift+A 因为它们都发出相同的关键代码,所以我明白为什么 Vim 不能做到这一点。但是 gVim 作为一个 X 应用程序,可以区分 Ctrl+ACtrl+Shift+A 。有没有办法以不同的方式映射这两件事?

对于初学者,我想做如下操作:使“从剪贴板粘贴”像 Gnome 终端一样工作,同时保持 Ctrl+V 为可视模式。

:nmap <C-S-V> "+gP

In a terminal, one cannot distinguish Ctrl+A and Ctrl+Shift+A as they both emit the same key code, so I can see why Vim can't do it. But gVim, being an X application, can differentiate Ctrl+A and Ctrl+Shift+A. Is there any way to map those two things differently?

For starters, I'd like to do something like the following: Make "paste from clipboard" work like Gnome terminal, while keeping Ctrl+V to the visual mode.

:nmap <C-S-V> "+gP

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

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

发布评论

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

评论(8

苦妄 2024-08-13 07:46:34

Gvim 不这样做是因为 vim 不能这样做(正常情况下)。抱歉,但事情就是这样。


然而...

某些终端(例如,xterm 和 iterm2)可以配置为发送任意按键组合的任意转义序列。

例如,将以下内容添加到 .Xresources 中,以便 xterm 发送 [65;5u for CtrlShiftA。然后您可以在 Vim 中将其映射到 。 (65 是 shift-a 的十进制 Unicode 值,5 是 ctrl 修饰符的位。本例中的 u 代表“unicode”。)

! .Xresources
XTerm*vt100.translations: #override Ctrl ~Meta Shift <Key>a: string(0x1b) string("[65;5u")

iTerm 和 [u]rxvt 也可以配置为执行此操作(未提供示例) )。

更多信息:http://www.leonerd.org.uk/hacks/fixterms/

Gvim doesn't do it because vim cannot do it (under normal circumstances). Sorry, but that's just how it is.


However...

Some terminals (e.g., xterm and iterm2) can be configured to send an arbitrary escape sequence for any combination of keys.

For example, add the following to .Xresources for xterm to send <Esc>[65;5u for CtrlShiftA. You can then map that in Vim to <C-S-a>. (65 is the decimal Unicode value for shift-a and 5 is the bit for the ctrl modifier. The u in this case stands for "unicode".)

! .Xresources
XTerm*vt100.translations: #override Ctrl ~Meta Shift <Key>a: string(0x1b) string("[65;5u")

iTerm and [u]rxvt can also be configured to do this (examples not provided).

More info: http://www.leonerd.org.uk/hacks/fixterms/

半衬遮猫 2024-08-13 07:46:34

正如已经指出的,没有办法以不同于 的方式映射

但是,使用 autokey 等工具(适用于 Linux 和 Windows) ) 或 autohotkey(对于 Windows),您可以重新映射 < /code> 为特定应用程序发送不同的击键。

例如,在我的系统上,我在 autokey 中有此设置:

$ cat ~/.config/autokey/data/gnome-terminal/ctrlshifta-gnome-terminal.py
#ctrl+shift+a sends '<S-F1>a'
keyboard.send_keys("<shift>+<f1>a") # Note that `f` in `f1` needs to be in lower case.

为其指定以下属性:

  1. 键盘快捷键为 ctrl+shift+a
  2. 窗口类:gnome-terminal- server.Gnome-terminal

然后你的 ~/.vimrc 可以为 a 创建映射来执行你想要的任何操作。


注意:

  1. 我使用 作为检测 的领导密钥。这是因为我的终端不接受 - 等键。如果您的应用程序支持它(我认为是 gvim),建议使用这些键。
  2. 我主要在 gnome-terminal 中使用 vim。所以我使用 window class = gnome-terminal-server.Gnome-terminal 作为过滤器。如果需要,请将其修改为使用 gvimautokey 支持用于捕获任何其他窗口属性(例如类/标题)的按钮。

As already pointed out, there are no ways to map <C-S-A> differently from <C-A>.

However, using tools like autokey (for linux & windows) or autohotkey (for windows), you can remap <C-S-A> to send a different key-stroke(s) for specific applications.

e.g. On my system, I have this setting in autokey:

$ cat ~/.config/autokey/data/gnome-terminal/ctrlshifta-gnome-terminal.py
#ctrl+shift+a sends '<S-F1>a'
keyboard.send_keys("<shift>+<f1>a") # Note that `f` in `f1` needs to be in lower case.

Assign it these properties:

  1. keyboard-shortcut as ctrl+shift+a
  2. window class: gnome-terminal-server.Gnome-terminal

Then your ~/.vimrc can create mapping for <S-F1>a to do whatever you want.


Notes:

  1. I have used <S-F1> as kind of leader key for detecting <C-S>. This was because my terminal did not accept <F13>-<F37> etc keys. If your application supports it, (gvim does I think) using those keys is recommended.
  2. I mainly vim in gnome-terminal. So I used window class = gnome-terminal-server.Gnome-terminal as filter. Modify it to use gvim if you want. autokey supports a button for capturing any other window's properties like class/title.
不…忘初心 2024-08-13 07:46:34

由于键盘输入的内部处理方式,不幸的是,这在今天通常是不可能的,即使在 GVIM 中也是如此。某些组合键,例如 Ctrl + 非字母无法映射,以及 Ctrl + 字母与 Ctrl + Shift + 字母无法区分。 (除非您的终端为其发送不同的 termcap 代码,而大多数情况下不会。)在插入或命令行模式下,尝试键入组合键。如果没有发生/插入,则无法使用该组合键。这也适用于 / / > / / 等(唯一例外的是 / < ;CH>。)这是一个已知的痛点,也是 vim_dev 和 #vim IRC 频道上各种讨论的主题。

有些人(首先是 Paul LeoNerd Evans)想要解决这个问题(即使是支持此功能的终端中的控制台 Vim),并提出了各种建议,cp。 http://groups.google.com/group/vim_dev/browse_thread/thread /626e83fa4588b32a/bfbcb22f37a8a1f8

但截至今天,尚未出现任何补丁或志愿者,尽管许多人表示希望在未来的 Vim 版本中提供此功能。

Due to the way that the keyboard input is handled internally, this unfortunately isn't generally possible today, even in GVIM. Some key combinations, like Ctrl + non-alphabetic cannot be mapped, and Ctrl + letter vs. Ctrl + Shift + letter cannot be distinguished. (Unless your terminal sends a distinct termcap code for it, which most don't.) In insert or command-line mode, try typing the key combination. If nothing happens / is inserted, you cannot use that key combination. This also applies to <Tab> / <C-I>, <CR> / <C-M> / <Esc> / <C-[> etc. (Only exception is <BS> / <C-H>.) This is a known pain point, and the subject of various discussions on vim_dev and the #vim IRC channel.

Some people (foremost Paul LeoNerd Evans) want to fix that (even for console Vim in terminals that support this), and have floated various proposals, cp. http://groups.google.com/group/vim_dev/browse_thread/thread/626e83fa4588b32a/bfbcb22f37a8a1f8

But as of today, no patches or volunteers have yet come forward, though many have expressed a desire to have this in a future Vim release.

土豪 2024-08-13 07:46:34

如果您担心失去现有的 CV 功能,您可以使用 CQ。请参阅:帮助 CTRL-V-替代。

If what bothers you is loosing existing C-V functionality, you can use C-Q instead. See, :help CTRL-V-alternative.

巡山小妖精 2024-08-13 07:46:34

NeoVim 现在为其终端和 GUI 客户端提供此功能。请参阅 :h nvim-features-new

NeoVim now offers this functionality for both its terminal and gui clients. See :h nvim-features-new

人│生佛魔见 2024-08-13 07:46:34

正如您所注意到的,您会得到相同的密钥代码。因此区分它们的唯一方法是检查事件处理函数中 Shift 键的状态。当然,如果按键和处理之间的延迟超过 0.5 秒,您将错过一些点击。

As you've noted, you get the same keycode. So the only way to distinguish them is to check the state of the Shift key in your event handling function. Of course, if you have more than 0.5 second delay between keypress and processing, you'll miss some hits.

几味少女 2024-08-13 07:46:34

默认情况下,大多数终端仿真器将控制加移位简单地视为控制。相反,您通常将这些组合键映射到转义序列并在终端应用程序内监听该序列。

第 1 步:配置终端模拟器,将 Ctrl+Shift+A 绑定到序列 Esc< /kbd>,A

终端模拟器是显示终端实际窗口的程序。通过 SSH 访问服务器时,终端仿真器是本地计算机上的一个程序。绑定键在不同的终端模拟器中的工作方式有所不同。例如:

  • 对于 urxvt,将 URxvt.keysym.Control-Shift-A: \033A 添加到 ~/.Xresources 配置文件中,并使用 xrdb ~ 重新加载它/.Xresources
  • 对于 iTerm2,打开首选项 ->键,添加一个条目,并将 Ctrl+Shift+A 绑定到操作“发送转义序列”并输入 A 进入下面的字段。

第 2 步:EscA 绑定到 Vim 中的命令。

将按键映射添加到您的 ~/.vimrc 配置中,并使用 :source ~/.vimrc 重新加载它:

nnoremap <esc>a your command here

Most terminal emulators treat control plus shift simply as control by default. Instead, you usually map those key combinations to an escape sequence and listen to that inside the terminal application.

Step 1: Configure your terminal emulator to bind Ctrl+Shift+A to the sequence Esc,A.

Your terminal emulator is the program that shows the actual window of the terminal. When accessing a server via SSH, the terminal emulator is a program on your local machine. Binding keys works differently in different terminal emulators. For example:

  • For urxvt, add URxvt.keysym.Control-Shift-A: \033A to the ~/.Xresources configuration file and reload it with xrdb ~/.Xresources.
  • For iTerm2, open Preferences -> Keys, add an entry, and bind Ctrl+Shift+A to the action "Send Escape Sequence" and type A into the field below.

Step 2: Bind Esc,A to a command in Vim.

Add the key mapping to your ~/.vimrc configuration and reload it with :source ~/.vimrc:

nnoremap <esc>a your command here
别低头,皇冠会掉 2024-08-13 07:46:34

shift + char = 该 Char 的大写字母

lowercase
<C-a> -> <C-a>

uppercase
<C-S-a> -> <C-A> 

shift + char = Capital of that Char

lowercase
<C-a> -> <C-a>

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