www-gem words

[Plugin] You're surrounded and that's good: nvim-surround

Published on

vim Writing in a text editor would be cumbersome if it was just about typing one letter after the other, then removing one letter after the other to correct your text, and start over again and again. We are lucky to have smart people in our community who build tools to make the writing process more enjoyable and help us improve our productivity. This is the case of the kylechui/nvim-surround neovim plugin . Taking advantage of brackets, parentheses, and quotes, this plugin simplifies text manipulation.
It provides a set of commands for quickly adding, removing, and changing pairs of characters around a selected piece of text. It’s particularly useful for tasks like wrapping text in HTML tags, creating function calls, or even just balancing your parentheses.

╭── How to use it

Here are some quick examples on how to use this plugin. Pairs of delimiters usually refer to <, >; {, }; [, ]; <, >; ‘, ‘; “, “. That means if you type one of them in your keybinding, its matching pair will automatically be used in your selection surrounding.

Note that the following aliases can be used in your keybinding:
  a = >
  b = )
  B = }
  r = ]
  q = “, ‘, `
Any other character will be used as is. For example, using “e” in the keybinding to add surrounding characters will append and prepend the selected text with “e”. The only exception being “t” which will be detailed below.

Add surroundings
Quickly add pairs of delimiters around a selection with ys

For example,
ysiw{ or ysiw}to wrap a word in curly braces
ys3wr to wrap 3 words in square braces

There’s also a selection mode which will let you add delimiters to a selected text. Simply select your text and use the keybinding S followed by a delimiter.

Remove surroundings

For example,
ds( or ds)to remove braces
dsq to remove quotes

Change surroundings

For example,
cs([, cs(], cs)[, or cs)] to swap braces to square braces
csBr to swap curly braces to sqaure braces

Work with html tags
Using “t” at the final argument in any of the above keybindings will deal with html tags.

For example:
ysiwt will ask for the tag delimiter. Entering “p” will surround the selection with <p> and </p>
dst will delete the first occurrence of surrounding html tags
cst will ask for the new html tag

Define your own delimiters
This option only support adding delimiters for now. Typing the ys keybinding followed by “i” will ask for your left and right delimiter respectively.

For example
ysiwi will ask for the left delimiter, let’s enter */
then it’ll ask for the right delimiter, let’s enter /*
the end result will be */YOUR_SELECTION/*

╭── That’s all?

You never get enough, do you? So there’s even more keybindings to work on functions, add delimiters on new lines… All is detailed in :h nvim-surround.usage

By now, you should get an idea of how this plugin can speed up your text editing/coding workflow. Give it a try today and watch your productivity soar as you effortlessly balance your brackets and wrap your text!


Thanks for your read. Hope it's been useful to you.


Interact with this post using Mastodon or

Comment on wwwgem's post

Copy and paste this URL into the search field of your favourite Fediverse app or the web interface of your Mastodon server.

✄ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈

More food for thoughts? Check other posts about: #(Neo)vim #(Neo)vim-Plugins