So, I'm usually used to making (decently) complex mapping, and sometimes, I'd like to run mapping on very specific conditions. Let's take for example a simple premise:
first line
second ...
long text from here
Say we want to make a mapping that select (in visual mode) one line at a time, (eg: select first line, then select the second one separately, etc) When I say select here, I do mean the full line (until end of line on the current line)
Anyway, here the example mapping for this:
nnoremap x <ESC><Down>^v$
This works fine, except for the first line. To fix this, there is a number of ways, but since this is just a simple example to explain what I mean, let's just check if we're on the first line or not in a function:
function! Visual_cycle_line()
if line('.') == 1
"call feedkeys('<ESC>') cannot be called here since it wouldn't execute right in terminal vim?? (ends up putting us in insert mode somehow)
call feedkeys('^v$')
else
"call feedkeys('<ESC>') same as above
call feedkeys('<Down>^v$')
endif
As shown above, it works fine except for the fact that, the part where it is supposed to escape visual mode, in which using <ESC>
specifically with feedkeys
doesn't work as intended (AFAIK).
I also tried to use normal!
but it doesn't seems to work with the above set up either.
Now back to my initial question: How can I use keymapping grammar directly inside any vim function, without having to care about formatting, or taking into account specific key weird behavior (eg: like with the example with and such) just as if I was defining a mapping with noremap
and the like?
To give some more perspective, I want to do what I explained inside functions, the same way we can use regex there (eg: doing :regexhere
in an if blocks works fine in both functions and commandline).
P.S.: I'm also aware of the possibility of just setting up mapping inside functions using noremap
and the like, instead of running the actual keymapping themselves, but that only work in certain workflows, which depends on the needs/preferences, which is why I still want to do/or at least know this anyway.