vimtricks.wiki Concise Vim tricks, one at a time.

How do I create a mapping that applies only in visual mode but not in select mode?

Answer

:xmap

Explanation

:vmap applies to both visual mode and select mode, which can silently break snippet plugins (like UltiSnips, LuaSnip) that use select mode to position the cursor inside snippet placeholders. Using :xmap instead targets visual mode exclusively, leaving select mode untouched.

How it works

Vim has two overlapping modes that v activates depending on context:

  • Visual mode — manual selection with v, V, or <C-v>
  • Select mode — selection started by a mouse click or plugin (resembles GUI editor selection)
Map command Visual Select
:vmap
:xmap
:smap

Snippet engines enter select mode after expanding a snippet so you can type directly over the placeholder text. A :vmap that triggers on a common key (like <Tab> or <CR>) can intercept those keystrokes.

Example

Suppose you map <Tab> to indent the selection:

" WRONG — interferes with snippet placeholder navigation
vnoremap <Tab> >gv

" CORRECT — visual only, select mode is untouched
xnoremap <Tab> >gv

With :xnoremap, pressing <Tab> inside a snippet placeholder passes through to the snippet engine instead of triggering the indent mapping.

Tips

  • The noremap variants are :xnoremap, :snoremap, :vnoremap
  • Use :xmap for all visual mappings unless you explicitly need select mode behavior
  • To check what mode a mapping applies to: :verbose xmap {key} shows visual-only, :verbose vmap {key} shows both modes

Next

How do I center or right-align lines of text using Vim's built-in Ex commands?