3

I find that in visual line mode, if I press a shortcut to print the current mode, it will print n instead of V. Is this the expected behavior?

My mapping:

xnoremap <a-j> :<c-u>echo mode()<CR>

Then select some lines of text, and press <a-j>, it will print n (normal mode) instead of V (visual line).

  • Once you’ve entered your command, you drop back to normal mode. So I’m inclined to think that’s intentional, since you’re no longer in Visual. – Jake Grossman Sep 23 at 13:00
2

Yes, pressing some keys in Vim could switch the current mode. Still surprised?

This is documented under :h mode-switching. Obviously, here you have even two transitions: one by colon, and another one by enter key.

But, I admit, it's confusing on the first encounter. In Neovim they even invented :h <Cmd> to deal with this. But in plain Vim you have only :h map-<expr> to stay Visual, while executing script.

However, the main point is that you rarely need to stay Visual for real. Just read the bookmarks (:h '< and :h '>) and move on. Do :h gv when needed to enter Visual mode again.

| improve this answer | |
  • 1
    Thanks for the explanations! I guess I can then use '< and '> to get the previously-selected lines inside a function, which are called from the maping. – jdhao Sep 23 at 13:58

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.