1

I have these two key mappings to surround the visual selection with double asterisks:

" This one works for line selections done with V, but not selections done with v
vmap <A-b> <ESC>'<I**<ESC>'>A**<ESC>gv
" This one works for selections done with v, but not selections done with V
vmap <A-b> o<ESC>i**<ESC>gvO<ESC>2la**<ESC>gv4l

They both work, so I'd like to make it so that they only execute in visual line (V) and visual select (v), exclusively.

According to the documentation for mapmode-x and mapmode-s, there is a way to create mappings exclusive to visual line and visual select:

    COMMANDS                                  MODES  
                                          Visual    Select  
:vmap  :vnoremap  :vunmap  :vmapclear       yes      yes
:xmap  :xnoremap  :xunmap  :xmapclear       yes       -
:smap  :snoremap  :sunmap  :smapclear       -        yes

However, when I map either of them to smap, when I select some text with v and press <A-b>, nothing happens.

  • e.g., smap <A-b> o<ESC>i**<ESC>gvO<ESC>2la**<ESC>gv4l

When I map either of them to xmap and make a selection with v or V, <A-b> will executes regardless.

  • e.g., xmap <A-b> o<ESC>i**<ESC>gvO<ESC>2la**<ESC>gv4l

(I tested smap and xmap individually with --clean)

1 Answer 1

1

The difference between xmap and smap is that:

  • The first (xmap) applies in Visual mode and
  • The second (smap) applies in Select mode.

The difference between Visual mode and Select mode is not that the first is triggered with V and the second with v.

Select Mode

The Select mode has been introduced to have a mode similar to the select mode of other text editors: You select a part of the text and when you type some text the selection is replaced by the typed text.

The Select mode is somehow similar to Visual mode. Like in Visual mode there are tree sub Select modes:

  • Line wise that you can start with gH
  • Character wise that you can start with gh
  • Block wise that that you can start with gCtrl h

Remark: Select mode is different from Visual mode in the sense that:

  • The selection can only by changed using motion keys (i.e. Up, Down, Left, Right, End, Home and their Ctrl combinations).
  • If you type any printable key the selection will be replaced by the typed text (e.g.: h, j, k, l, gg, ...)

There is an alternative way way to enter in select mode that depends of the selectmode option:

If selectmode contains mouse:

set selectmode+=mouse

Then Vim enter in select mode if you select an area using the mouse.

If selectmode contains cmd:

set selectmode=+cmd

Then Vim enter in select mode when you hit v, V or Ctrl v More information with :help 'selectmode'

If selectmode contains key and keymodel is startsel,endsel then Vim enter in Select mode when you hit a shifted cursor motion (e.g. Shift Left, Shift Up, Shift Right, Shift Down, Shift Home, Shift End, ...) and quit Select mode when you hit any other key.

Remark: You can set these two options using the :behave mswin command.

Remark:

  • You can switch from Select mode to Visual mode and back using Ctrl g
  • You can switch to normal mode for one action with Ctrl o (like in Insert mode)

Solution

For your problem having two mappings depending on the mode is probably not the solution.

But you can detect the mode you are in using the mode() function and do your mapping the following way:

vnoremap <expr> <A-b> mode()==#'V' ? "<ESC>'<I**<ESC>'>A**<ESC>gv" : "o<ESC>i**<ESC>gvO<ESC>2la**<ESC>gv4l"
2
  • 1
    I think you can also toggle select mode from visual mode with C-g or something. This answer could also point to the list of modes and the explanations of Visual and Select modes in the help (and maybe explain the differences in Visual and Select mode with more than how to trigger them).
    – D. Ben Knoble
    Dec 30, 2023 at 12:59
  • 1
    @D.BenKnoble I have enriched the select mode paragraph :-) Dec 30, 2023 at 15:58

Your Answer

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

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