How do I wrap an entire line in surrounding characters using vim-surround?
Answer
yss"
Explanation
The yss{char} mapping from the vim-surround plugin surrounds the entire current line (ignoring leading whitespace) with the chosen delimiter. It is the line-wise shortcut for ys, analogous to how yy is the line-wise shortcut for y.
How it works
ys— the vim-surround "add surrounding" operators(doubled) — targets the current line, similar toyy,dd,cc"(or any delimiter) — the surrounding character to wrap with
Supported delimiters follow the same rules as other vim-surround mappings:
- Use the opening delimiter (
(,[,{) to add surrounding with a space inside - Use the closing delimiter (
),],}) for tight surrounding without spaces - Use
tto wrap in an HTML/XML tag:ysstthen typediv<CR>
Example
Given the line (cursor anywhere on it):
hello world
Press yss" to get:
"hello world"
Press yss( to get (with spaces):
( hello world )
Press yss) to get (without spaces):
(hello world)
Tips
yssrespects indentation — leading whitespace is preserved outside the surrounding- Combine with
.(via the vim-repeat plugin) to repeat the last surround operation - To change existing surrounding, use
cs(e.g.cs"'changes"to') - To delete surrounding, use
ds(e.g.ds"removes surrounding double quotes)