How can I remap vim, how can I "grab" and reuse "any" character?
In my .vimrc, I have the following, which, in insert mode, nicely adds a half line to the end of the line and returns the cursor to its original location:
inoremap <leader><leader>; <C-o>mp<C-o>A;<C-o>`p
How can I do the same, but for ANY / ALL characters? For example, something like this pseudocode:
inoremap <leader><leader><any> <C-o>mp<C-o>A<any><C-o>`p
Or using regex, something like this pseudocode ...
inoremap <leader><leader>(\w) <C-o>mp<C-o>A$1<C-o>`p
Is even such capture and reuse possible?
source to share
I think this does what you want:
inoremap <expr> <leader><leader> "<C-o>mp<C-o>A" . (nr2char(getchar())) . "<C-o>`p"
To break it down, there is a command for inoremap called <expr>
. This command changes the inoremap as follows: inoremap <expr> {keys} {function}
When you press the keys defined in a section {keys}
, it is called by a function in the section {function}
and inserts the return value of that function. Anything in quotes in the section will be added {function}
. So here's a breakdown of the functions section:
"<C-o>mp<C-o>A" . (nr2char(getchar())) . "<C-o>`p"
^ | ^
|-------------------|-----------------|
These just do |
what the original |
mapping does |
This just gets a single character
from the user and concatenates it to your
original mapping, in place of the ';'
Read them for more information on these topics:
:help :map-<expr>
:help getchar()
:help nr2char()
Note. getchar
not really taken from the collation itself, it just asks the user for a character and then inserts it.
source to share