1

I would like to swap my left shift key with my left alt key, and I would like to know if it’s possible to achieve this with vim key mapping.

Maybe not swapping but copy the whole shift key map to alt?

  • You can't really do that in Vim, there's a limited version of that you could do with much work... But you're definitely better off by looking at remapping keys on your OS's keyboard driver, where you can easily accomplish something exactly like that and it would work on any application you use. I suggest you ask in Superuser (or Ask Different if on a Mac) about how to remap those keys in your OS. – filbranden Jan 24 at 6:21
0

In vim, nvi, vi, and so forth (all of the terminal vi clones, to be precise), no. A terminal session doesn't transmit the modifier key presses separate from the keys they're modifying. It's true that terminal emulators have come a long way since when they were first made, and they have a lot more capabilities now. But that's something that's pretty fundamental to the data flow on how old terminals worked, and I don't see it as something that would be possible to change without breaking compatibility with all sorts of things they're supposed to be compatible with.

I've checked to see if there's a gvim capability to do this. As of 8.2, based on my reading of :help :map, especially the section :help :map-alt-keys, there does not appear to be. gvim does recognize the modifier keys itself, but it appears to fundamentally not make a distinction between the left or right modifier key of any set that has a pair, and it fundamentally doesn't recognize the modifier key presses except when there's also a non-modifier key pressed.

The only things that would stop you from doing a full list of

:noremap <A-A> <A-a>
:noremap <A-a> <A-A>
:norelmap <A-A> <A-a>
:norelmap <A-a> <A-A>
:noremap <A-B> <A-b>
:noremap <A-b> <A-B>
:norelmap <A-B> <A-b>
:norelmap <A-b> <A-B>
...

would be your patience with going through all of them and the confusion factor from the amount that you've already gotten used to them being the thing they are by default. I don't know of a way to get a listing of all of the keycodes that actually do something, so there's no way I know of to be thorough for all the keys that matter but not bother with the ones that don't.

Of course, simply mapping the shifted alt keys to the unshifted alt keys would cause you to lose access to the shifted alt keys' functions, unless you mapped something else to get there.

Note that I'm using the :noremap command there because a simple :map both directions like that would result in an infinite loop. I'm using both :noremap and :norelmap because you didn't indicate what contexts you want the remap to happen, and that's the simplest way to get virtually all contexts.

There's a table of map commands at :help map-modes, which was very confusing to me when I first started using vim, but it should be helpful to you here. My confusion was because the three columns of commands aren't marked. The first column is recursive mappings (that is, after the key press is translated, the new code gets looked up to see if it gets mapped in some way, too), the second column is non-recursive mappings (what I naturally expected from map commands), and the third is how to undo either type of mapping.


That having been said, if you have access to your OS keyboard layout, you can do that sort of change. The simplest way would make that change for everything on the system. The details of how to do that would probably be under either an OS specific SE, or Super User SE.

| improve this answer | |
  • Then can I access the whole vim key map file and change all shift to alt? – Andrew.Wolphoe Jan 24 at 5:22
  • @Andrew.Wolphoe wdym? What vim key map file? – D. Ben Knoble Jan 24 at 14:01
  • @D.BenKnoble Maybe like using vim script and first list out all key bond start with shift then remap them to alt key. Is it possible? – Andrew.Wolphoe Jan 24 at 14:02
0

Is this close to what you want?

let s:str='abcdefghijklmnopqrstuvwxyz'
for i in range(len(s:str))
    let s:c=s:str[i]
    exe 'map <A-'.s:c.'> <S-'.s:c.'>'
    exe 'lmap <A-'.s:c.'> <S-'.s:c.'>'
    exe 'tmap <A-'.s:c.'> <S-'.s:c.'>'
endfor

where s: specify the scope of the variables (see :help s:) and command exe executes the string following it. . concatenates strings together. For example, if i is 3, then s:c becomes d, and map <A-d> <S-d> will be executed.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.