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:
sais an operator that takes a motion, just likedorc - Can detect and operate on the nearest surrounding pair automatically
Tips
- Load vim-surround-compatible keybindings with:
runtime macros/sandwich/keymap/surround.vim— thencs,ds,yswork as expected sdbdeletes the nearest surrounding pair (auto-detected) — no need to specify which charactersrb)replaces the nearest surrounding pair with parens- Recipes can be customized per-filetype for language-specific surroundings (e.g., LaTeX
\begin{}/\end{})