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

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" operator
  • s (doubled) — targets the current line, similar to yy, 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 t to wrap in an HTML/XML tag: ysst then type div<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

  • yss respects 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)

Next

How do I run a shell command on a range of lines in normal mode and replace them with the output?