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

How do I add, change, or delete surrounding characters using vim-sandwich?

Answer

sa{motion}{char} / sd{char} / sr{old}{new}

Explanation

vim-sandwich (by Masahiko Machida) is a modern alternative to vim-surround for manipulating surrounding characters — brackets, quotes, tags, and custom delimiters. It uses a consistent sa (add), sd (delete), sr (replace) mnemonic and supports dot-repeat, visual mode, and powerful recipe-based customization.

Core operations

Command Action Example
saiw( Add parens around inner word hello(hello)
sa2aw" Add quotes around 2 words foo bar"foo bar"
sd( Delete surrounding parens (hello)hello
sd" Delete surrounding quotes "hello"hello
sr(] Replace parens with brackets (hello)[hello]
sr"' Replace double quotes with single "hello"'hello'

Install with: Plug 'machakann/vim-sandwich'

Visual mode

Select text, then press sa + delimiter:

viw     " select word
sa(     " wrap in parens

What makes it different from vim-surround

  • Dot-repeatable out of the box (no vim-repeat dependency)
  • Uses recipes for complex surround patterns (e.g., function calls, HTML tags)
  • Operator-pending: sa is an operator that takes a motion, just like d or c
  • Can detect and operate on the nearest surrounding pair automatically

Tips

  • Load vim-surround-compatible keybindings with: runtime macros/sandwich/keymap/surround.vim — then cs, ds, ys work as expected
  • sdb deletes the nearest surrounding pair (auto-detected) — no need to specify which character
  • srb) replaces the nearest surrounding pair with parens
  • Recipes can be customized per-filetype for language-specific surroundings (e.g., LaTeX \begin{}/\end{})

Next

How do I run a search and replace only within a visually selected region?