Skip to content

nvim-surround

What is nvim-surround

  • Add/change/delete surrounding delimiter pairs with ease
  • A plugin for Neovim written in Lua
  • kylechui/nvim-surround

Text objects

In Vim, text objects allow you to select pieces of text based on their structure or syntax. Here is a list of common text objects in ascending order by their typical size:

  • a character (v or V): v
  • a word (w and iw): iw
  • a WORD (an entire string of non-blank characters, W and iW): iW
  • a sentence (as and is): is
  • a paragraph (ap and ip): ip
  • a block of text enclosed in delimiters (a(, a), ab, aB, a{, a}, i(, i), ib, iB, i{, i}): i(, a)
  • a block of text enclosed in quotes (a', a", i', i"): i', a"

These text objects are invaluable when working with the nvim-surround plugin for efficient text editing within Vim.

Use examples

Old text                    Command         New text

surr*ound_words             ysiw)           (surround_words)
*make strings               ys$"            "make strings"
[delete ar*ound me!]        ds]             delete around me!
remove <b>HTML t*ags</b>    dst             remove HTML tags
'change quot*es'            cs'"            "change quotes"
<b>or tag* types</b>        csth1<CR>       <h1>or tag types</h1>
delete(functi*on calls)     dsf             function calls

Normal mode

Mnemonics

  • ys (you surround/add): ys<motion><delimiter>
  • cs (change surrounding): cs<old delimter><new delimiter>
  • ds (delete surrounding): ds<delimiter>

examples

  • ysiw" to add double quote around a word
  • ysa") to add parenthesis around quoted content
  • ysl'
  • yst;}
  • ys8jt + HTML tag, add tags around content
  • yss: Add a surrounding pair around the current line (normal mode)
  • yS: Add a surrounding pair around a motion, on new lines (normal mode)
  • ySS: Add a surrounding pair around the current line, on new lines (normal mode)
  • ds" to delete double quote around a word
  • dst to delete the surrounding tags
  • cs"' to replace double quote by single quote
  • cst + HTML tag, change surrounding tags with new tags
  • yswf
  • csf
  • dsf

This is a paragraph.

Insert mode

  • ??

Visual mode

  • S'
  • S>

Use :h nvim-surround.usage to learn more