If you're working in Vim and frequently need to add, delete, or modify surrounding characters like parentheses, quotes, or tags, the vim-surround plugin is a must-have. Here's a quick guide to get you started.
What is vim-surround?
vim-surround, developed by Tim Pope, is a plugin that simplifies working with surrounding characters in Vim. Whether you're wrapping a word in quotes, removing parentheses, or changing tags, vim-surround makes it effortless.
- Adding Surroundings To add surrounding characters to text:
Install vim-surround using a plugin manager like vim-plug:
vim
Copy code
Plug 'tpope/vim-surround'
Use the command ys (yank surround) in normal mode:
ys wraps the specified motion with the given character.
Examples:
ysiw) → Wraps the inner word with parentheses: (word)
ysiw" → Wraps the inner word with double quotes: "word"
- Deleting Surroundings To remove surrounding characters:
Place the cursor inside the surrounding.
Use the ds command followed by the surrounding character:
ds" → Removes surrounding double quotes: "word" → word
ds) → Removes surrounding parentheses: (word) → word
- Changing Surroundings To change one type of surrounding to another:
Place the cursor inside the current surrounding.
Use the cs command followed by the current and desired surrounding characters:
cs"' → Changes double quotes to single quotes: "word" → 'word'
cs)( → Changes parentheses to curly braces: (word) → {word}
For Vim Without Plugins
If you don't have vim-surround installed, you can still manually add or remove surroundings:
Add: Use i (insert) or a (append) to type the surrounding characters manually.
Remove: Use x to delete characters or visual mode (v) to select and remove.
Why Use vim-surround?
This plugin saves time and keystrokes, especially for repetitive editing tasks. It's simple, powerful, and enhances productivity in Vim.
Try it out and watch your Vim workflow transform!
Top comments (0)