简体   繁体   中英

Vim: how to remap a "general" key

One thing I really like in Vim is it's hability to have multiple clipboards available. However I hate to write "ay to yank and "ap to paste, I'd rather have something closer to the classical Ctrl-c, Ctrl-v, like ←a (←: AltGr + y) and þa (þ: AltGr + p).

I could make a remap like nnoremap ←a "ay in this case, but then I would only have the buffer "a" available to use this way. So the question is: could I make a remap such as nnoremap ←{key} "{key}y in vim, that would replace the {key} with whatever I typed, so that I could use any character as a register with only one remap? ( ←q becomes "qy , ←w becomes "wy , etc...)

Btw: yes, AltGr keys like "←" and "þ" works just like any other letter for commands.

The left-hand side of a mapping can't be dynamic.

The easiest way to deal with that limitation is simply to loop through a list:

for reg in 'abcdefghijklmnopqrstuvwxyz'
    execute 'nnoremap ←' .. reg .. ' "' .. reg .. 'y'
    execute 'nnoremap þ' .. reg .. ' "' .. reg .. 'p'
endfor

See :help:for , :help:execute , :help expr-.. .


Note that y is an operator and an operator is supposed to "operate" on a motion. This means that all those "{char}y normal mode mappings are useless. For this to actually be useful, you would need to:

  • make visual mode mappings for y ,
  • make a custom operator for use in normal mode, see :help:map-operator .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM