Surrounds the current selection with brackets or quotes. It can also remove bracket pairs from the end of the selected region and it provides some convenience functions for selecting and expanding the selection region.
(straight-use-package
'(surround :type git :host github :repo "edvin/surround.el"))
(surround-mode 1)
Surround is not hosted in MELPA so you have to pull it from GitHub. If you use Straight.el you can update the dependency
with M-x straight-pull-package
and selecting surround
.
-
C-c C-s s
(surround-region
) Prompts for abracket-char
to wrap around the currently selected region. If thebracket-char
has a counterpart insurround-bracket-alist
, the corresponding end bracket char is added to the end of the region. Selecting quotes like'
or"
will add the same char to the end of the region, while(
,[
and{
will add the corresponding end bracket instead. -
C-C C-s r
(surround-replace-wrap
) will replace thebracket-char
at the ends of the currently selected region with the newly selectedbracket-char
. Counterparts are looked up the same way as forsurround-region
. -
C-c C-s e
(surround-expand-region
) will expand the selection region to the next matchingbracket-char
. Repeat to expand further. You can passC-u
first to exclude thebracket-char
pair from the selected area. (Passing the optionalexclusive
parameter to the function). After expanding the region you can keep pressing 'e' to further expand the region using the same 'bracket-char' or chose one of the other transient menu options available. TypingC-u
after expanding works the same way as typing it before the original command, removing thebracket-chars
from the selected region. -
C-c C-s x
(surround-auto-expand-region
) will automatically expand the selection region to the next matching registered bracket-chars. It will consultsurround-auto-expand-alist
to look for matches and find the opposing bracket by looking upsurround-bracket-alist
. You can passC-u
first to exclude the matched bracket pair from the selected area. (Passing the optionalexclusive
parameter to the function). After auto-expanding the region you can keep pressing 'x' to further expand the region or chose one of the other transient menu options available.C-u
can be typed before or after the command to remove the surrounding bracket-chars from the selected region. -
C-c C-s u
(surround-unwrap
) will unwrap/remove the brackets at the end of the currently selected region. Typically you'd select the region withsurround-expand-region
first, then either unwrap or replace thebracket-char
pair. -
C-c C-s l
(surround-select-line
) is a convenience function to select the whole line. It does the same asC-a C-SPC C-e
, but additionally allow you to pressd
to duplicate the line by way of the transient menu that appears after selecting the line. Keep hitting d to add more copies of the same line.