How do I quickly delete surrounding quotes, brackets, or tags in Vim?
Answer
ds"
Explanation
The vim-surround plugin provides the ds command to instantly delete any surrounding delimiter pair. Rather than hunting for the opening and closing characters separately, ds removes both in one keystroke sequence.
How it works
dstands for deletesstands for surrounding- The next character specifies which surrounding to remove
So ds" means: delete surrounding double quotes.
Example
With your cursor anywhere inside the quotes:
return "Hello world";
Pressing ds" results in:
return Hello world;
Common uses
ds" " remove surrounding double quotes
ds' " remove surrounding single quotes
ds) " remove surrounding parentheses
ds] " remove surrounding square brackets
ds} " remove surrounding curly braces
dst " remove surrounding HTML/XML tag
Nested delimiters
The plugin finds the nearest matching pair outward from your cursor. Given deeply nested structures, you can repeat the command to peel off layers:
" Start:
[["hello"]]
" ds" removes the quotes:
[[hello]]
" ds] removes the inner brackets:
[hello]
" ds] again removes the outer brackets:
hello
Deleting HTML tags with dst
The t target is especially powerful for HTML editing:
" Start:
<div class="wrapper">content</div>
" dst removes the entire tag pair:
content
Tips
- Pair with vim-repeat so
.repeats the lastdscommand - Use
dstrepeatedly to strip nested HTML tags layer by layer - After deleting surroundings, use
csto immediately add different ones if you need a replacement