1

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.

1 Answer 1

5

Not sure I understand your end goal (XY problem?)

But if your end goal to have something like this:

enter image description here

Then 2 mappings for x would look like:

nnoremap x V
xnoremap x <ESC>jV

As feedkeys(), you have to use doublequotes and escape for special keys:

function! Visual_cycle_line()
    if line('.') == 1
      call feedkeys("\<ESC>")
      call feedkeys('^v$')
    else
      call feedkeys("\<ESC>")
      call feedkeys("\<Down>^v$")
    endif
endfunc

Although I doubt that function would work as you think it should anyway (granted you only map x in normal mode).

2
  • Thank you, this works. Btw, this might sounds like an excuse but this isn't an XY problem, as this was just a well suited example, at least to me at the time (if I showed all the other way I would solve it, it wouldn't count as an example right?) Mar 3, 2022 at 9:02
  • also, another minor explanation: in case you were curious, using V in visual mode, does work better than just doing ^v$ but there a slight misrepresentation of the offsets from :echo getpos(''>') where one of the offset will be 2147483647 or so when doing V...which is why I've been using ^v$ instead for my own purpose, but that's beside the point Mar 3, 2022 at 9:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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