Skip to content

Introduce vim surround keymappings

Enno edited this page Mar 2, 2024 · 9 revisions

Introduce vim-surround keymappings

If you want to use vim-sandwich with vim-surround keymappings, add the following line to your vimrc.

runtime macros/sandwich/keymap/surround.vim

ys, yss, yS, ds, cs in normal mode and S in visual mode are available. dss and css (not found in vim-surround) are also available: These are similar to ds and cs but determine deleted/replaced texts automatically. See the file directly for details.


NOTE that the vim-sandwich plugin should be in 'runtimepath' option when the :runtime command is executed. If you are using a plugin manager, like plug, the :execute command should be placed after plug#end() in your vimrc.

plug#begin()
~~~
Plug 'machakann/vim-sandwich'
~~~
plug#end()
runtime macros/sandwich/keymap/surround.vim

Or just use the :source command instead.

source {full/path/to}/macros/sandwich/keymap/surround.vim

Please change the {full/path/to} part for your environment.

Text objects

Additionally, vim-sandwich provides several text objects. They would also be helpful, give it a try!

Text objects to select a text surrounded by brackets or user-specified characters.

xmap is <Plug>(textobj-sandwich-query-i)
xmap as <Plug>(textobj-sandwich-query-a)
omap is <Plug>(textobj-sandwich-query-i)
omap as <Plug>(textobj-sandwich-query-a)
  • Examples
   |<---- is( ---->|
{[(a_surrounded_text)]}
  |<----- as( ----->|

  |<----- is[ ----->|
{[(a_surrounded_text)]}
 |<------ as[ ------>|

 |<------ is{ ------>|
{[(a_surrounded_text)]}
|<------- as{ ------->|

     |<  is_ >|
{[(a_surrounded_text)]}
    |<-  as_ ->|

Text objects to select the nearest surrounded text automatically.

xmap iss <Plug>(textobj-sandwich-auto-i)
xmap ass <Plug>(textobj-sandwich-auto-a)
omap iss <Plug>(textobj-sandwich-auto-i)
omap ass <Plug>(textobj-sandwich-auto-a)
  • Examples
   |<---- iss ---->|
{[(a_surrounded_text)]}
  |<----- ass ----->|

   |<---- iss ---->|
({[a_surrounded_text]})
  |<----- ass ----->|

   |<---- iss ---->|
[({a_surrounded_text})]
  |<----- ass ----->|

Text objects to select a text surrounded by user-specified characters.

xmap im <Plug>(textobj-sandwich-literal-query-i)
xmap am <Plug>(textobj-sandwich-literal-query-a)
omap im <Plug>(textobj-sandwich-literal-query-i)
omap am <Plug>(textobj-sandwich-literal-query-a)
  • Examples
     |<  im_ >|
{[(a_surrounded_text)]}
    |<-  am_ ->|

See also Bracket with spaces.