nvim-surround
Add/change/delete surrounding delimiter pairs with ease. Written with :heart: in Lua.
Top Related Projects
surround.vim: Delete/change/add parentheses/quotes/XML-tags/much more with ease
Set of operators and textobjects to search/select/edit sandwiched texts.
Quick Overview
nvim-surround is a Neovim plugin that provides functionality to add, delete, and change surrounding characters (like parentheses, brackets, quotes) around text objects. It's designed to be a modern, configurable alternative to the popular vim-surround plugin, written in Lua for better performance and extensibility.
Pros
- Highly configurable and customizable
- Written in Lua, offering better performance and integration with Neovim
- Supports both normal mode and visual mode operations
- Provides dot-repeat functionality for surround operations
Cons
- Requires Neovim 0.7.0 or higher
- May have a steeper learning curve compared to vim-surround for some users
- Limited compatibility with older Vim configurations
- Some users may experience conflicts with other plugins or custom keybindings
Code Examples
- Adding surrounds:
-- Surround the current word with parentheses
vim.cmd('normal ysiw)')
-- Surround the current line with curly braces
vim.cmd('normal yss{')
- Changing surrounds:
-- Change surrounding quotes from single to double
vim.cmd('normal cs"\'')
-- Change surrounding parentheses to square brackets
vim.cmd('normal cs(]')
- Deleting surrounds:
-- Delete surrounding parentheses
vim.cmd('normal ds)')
-- Delete surrounding HTML tags
vim.cmd('normal dst')
Getting Started
To install nvim-surround using a plugin manager like packer.nvim, add the following to your Neovim configuration:
use({
"kylechui/nvim-surround",
tag = "*", -- Use for stability; omit to use `main` branch for the latest features
config = function()
require("nvim-surround").setup({
-- Configuration here, or leave empty to use defaults
})
end
})
After installation, you can use the default keybindings or customize them to your liking. The plugin provides various operations like ys
(add surround), cs
(change surround), and ds
(delete surround) in normal mode, as well as S
in visual mode to surround selected text.
Competitor Comparisons
surround.vim: Delete/change/add parentheses/quotes/XML-tags/much more with ease
Pros of vim-surround
- Well-established and battle-tested plugin with a large user base
- Works in both Vim and Neovim environments
- Simpler setup and configuration process
Cons of vim-surround
- Written in Vimscript, which may have performance limitations compared to Lua
- Less customizable and extensible than its Lua-based counterpart
- Lacks some advanced features present in nvim-surround
Code Comparison
vim-surround:
" Add surrounding quotes to a word
ysiw"
" Change surrounding parentheses to square brackets
cs)]
nvim-surround:
-- Add surrounding quotes to a word
vim.keymap.set("n", "<leader>s", require("nvim-surround").surround_word)
-- Change surrounding parentheses to square brackets
vim.keymap.set("n", "<leader>c", require("nvim-surround").change_surround)
nvim-surround offers more granular control over keybindings and functionality, while vim-surround relies on its established command structure. The Lua-based plugin allows for easier integration with other Neovim plugins and configurations, potentially leading to a more cohesive setup for Neovim users.
Set of operators and textobjects to search/select/edit sandwiched texts.
Pros of vim-sandwich
- More mature and stable, with a longer development history
- Supports both Vim and Neovim, offering wider compatibility
- Provides additional features like text object handling and dot-repeat functionality
Cons of vim-sandwich
- Written in Vimscript, which may be slower compared to Lua-based nvim-surround
- Configuration can be more complex, especially for users less familiar with Vimscript
- May have a steeper learning curve due to its extensive feature set
Code Comparison
vim-sandwich:
nmap sa <Plug>(sandwich-add)
xmap sa <Plug>(sandwich-add)
omap sa <Plug>(sandwich-add)
nmap sd <Plug>(sandwich-delete)
xmap sd <Plug>(sandwich-delete)
nmap sr <Plug>(sandwich-replace)
xmap sr <Plug>(sandwich-replace)
nvim-surround:
require("nvim-surround").setup({
keymaps = {
insert = "<C-g>s",
insert_line = "<C-g>S",
normal = "ys",
normal_cur = "yss",
normal_line = "yS",
normal_cur_line = "ySS",
visual = "S",
visual_line = "gS",
delete = "ds",
change = "cs",
},
})
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
nvim-surround
Surround selections, stylishly :sunglasses:
:sparkles: Features
- Add/delete/change surrounding pairs
- Function calls and HTML tags out-of-the-box
- Dot-repeat previous actions
- Set buffer-local mappings and surrounds
- Jump to the nearest surrounding pair for modification
- Use a single character as an alias for several text-objects
- E.g.
q
is aliased to`,',"
, socsqb
replaces the nearest set of quotes with parentheses
- E.g.
- Surround using powerful pairs that depend on user input
- Modify custom surrounds
- First-class support for Vim motions, Lua patterns, and Tree-sitter nodes
- Highlight selections for visual feedback
:lock: Requirements
- Neovim 0.8+
- [Recommended] If nvim-treesitter is installed, then Tree-sitter nodes may be surrounded and modified, in addition to just Vim motions and Lua patterns
- [Recommended] If nvim-treesitter-textobjects is installed, then Tree-sitter text-objects can be used to define surrounds, simplifying configuration
:package: Installation
Install this plugin using your favorite plugin manager, and then call
require("nvim-surround").setup()
.
lazy.nvim
{
"kylechui/nvim-surround",
version = "^3.0.0", -- Use for stability; omit to use `main` branch for the latest features
event = "VeryLazy",
config = function()
require("nvim-surround").setup({
-- Configuration here, or leave empty to use defaults
})
end
}
packer.nvim
use({
"kylechui/nvim-surround",
tag = "*", -- Use for stability; omit to use `main` branch for the latest features
config = function()
require("nvim-surround").setup({
-- Configuration here, or leave empty to use defaults
})
end
})
:rocket: Usage
The three "core" operations of add
/delete
/change
can be done with the
keymaps ys{motion}{char}
, ds{char}
, and cs{target}{replacement}
,
respectively. For the following examples, *
will denote the cursor position:
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
Detailed information on how to use this plugin can be found in
:h nvim-surround.usage
.
:gear: Configuration
The default configuration is found
here.
Simply call require("nvim-surround").setup
or
require("nvim-surround").buffer_setup
with the desired options.
More information on how to configure this plugin can be found in
:h nvim-surround.configuration
.
Contributing
Shoutouts
- vim-surround
- mini.surround
- vim-sandwich
- Like this project? Give it a :star: to show your support!
Top Related Projects
surround.vim: Delete/change/add parentheses/quotes/XML-tags/much more with ease
Set of operators and textobjects to search/select/edit sandwiched texts.
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot