How to map Ctrl+A and Ctrl+Shift+A differently?
Asked Answered
C

8

109

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
Creaturely answered 1/10, 2009 at 22:3 Comment(3)
It's possible: vim.wikia.com/wiki/Mapping_fast_keycodes_in_terminal_VimApache
@vivoconunxino That link makes no mention of adding a shift modifier to ctrl-a.Merline
With modifyOtherKeys feature it's possible by default, see key bindings - How to map <C-i> separate from <Tab>? - Vi and Vim Stack Exchange -- and nowadays gVim supports this by default as well.Welton
M
57

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/

Merline answered 1/2, 2010 at 20:22 Comment(3)
To remap multiple keys, look at this document: Separate key entries with \n. Use backslash to split lines. E.g. to also map <kbd>C-S-B</kbd> and <kbd>C-S-F</kbd> use this: XTerm*vt100.translations: #override Ctrl ~Meta Shift <Key>a: string(0x1b) string("[65;5u") \n Ctrl ~Meta Shift <Key>b: string(0x1b) string("[66;5u") \n Ctrl ~Meta Shift <Key>f: string(0x1b) string("[70;5u")Mackie
For completeness sake, maybe add to the answer also how to map this in vi? E.g. map <ESC>[66;5u :echo "ctrl-shift-b received"<CR> to map ctrl-shift-b to print a message in the status line. On AskUbuntu there was a similar Q and I've summarized this.Mackie
If you are scratching your head: I have written a more detailed and complete Q&A inspired in this answer, with several several examples. unix.stackexchange.com/q/631241Syst
T
11

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.
Trevethick answered 18/10, 2016 at 4:27 Comment(6)
Thanks heaps. Theres lots of stuff out there about using Autokey for key remapping but no actual instructions on how to do it. Finally your answer does that. I can't believe no one else has upvoted you.Semibreve
God damn autokey! I did everything I could but I couldn't get it installed on my ubuntu 16.04.Brigidabrigit
apt install autokey-gtk ?Trevethick
Before installing extra software, you might check to see if your window manager handles this directly. For example, in i3 my .config has this: bindsym --release Control+Shift+h exec --no-startup-id xdotool key --clearmodifiers comma w m h, which presses ,wmh when I press <C-S-h>, and then vim knows how to handle ,wmhChanna
^^ This is good too. However, I wanted to limit the keys-translation to only gnome-terminal or gvim. Not other apps. Other applications should continue to receive <C-S-H> when I press that.Trevethick
autokey was a lifesaver for me. Saved me hours of work and debugging. How can I buy you a beer?Airsickness
G
8

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.

Germaun answered 5/12, 2017 at 15:8 Comment(0)
S
5

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

Sheepherder answered 7/4, 2011 at 10:31 Comment(0)
B
4

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

Bethlehem answered 9/4, 2015 at 20:6 Comment(4)
Not quite, unless you enable CSI codes in your terminal. The CTRL-SHIFT variants that Neovim does support out of the box are mostly non-printable characters (examples listed at neovim.org/doc/user/vim_diff.html#nvim-features-new ). However, meta chords (including <m-s-...> variants) do mostly work completely out of the box.Flywheel
That document says ALT (|META|) chords always work (even in the |TUI|). Map |<M-| with any key: <M-1>, <M-BS>, <M-Del>, <M-Ins>, <M-/>, <M-\>, <M-Space>, <M-Enter>, etc. Case-sensitive: <M-a> and <M-A> are two different keycodes. There's a line break before the "Case-sensitive:" part, making this ambiguous. Thanks for the correction, @JustinM.Keyes. Also, vim 8.1 appears to differentiate between <M-a> and <M-A> as well.Xantha
@justin-m-keyes <c-s-l> is perfectly distinguished from <c-l> in Konsole without any extra hassleStratiform
Corrected URL from first comment: neovim.io/doc/user/vim_diff.html#nvim-featuresGlycoside
A
1

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.

Acceptable answered 1/10, 2009 at 23:1 Comment(2)
I guess the point of my question was that given that gvim is capable of making such a distinction (even though plain vim cannot), is there any gvim-specific extension that I could rely on to differentiate Ctrl+Shift+V vs Ctrl+V.Creaturely
how to check the state of Shit key? I searched, but didn’t find anything even remotely useful.Reganregard
U
0

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
Utterance answered 19/1, 2021 at 21:32 Comment(0)
S
0

shift + char = Capital of that Char

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

uppercase
<C-S-a> -> <C-A> 
Sherard answered 8/1 at 7:24 Comment(1)
Doesn't work on Vim 9.0Beaufort

© 2022 - 2024 — McMap. All rights reserved.