What are some alternatives of vim-surround

39 views
Skip to first unread message

Devin Weaver

unread,
Apr 18, 2023, 11:16:39 AM4/18/23
to vim_use
I use vim-surround so much that it has become an essential part of my Vim experience. I will continue to use it with gusto. But it does garner a question and that the facilities it provides are rather common in editing source code in general which leads me to wonder what are the "vim --clean" ways to perform the same actions that vim-surround offers.

Say I run Vim on a device where getting a git clone of the plugin difficult. Maybe it only has access to its own config and doesn't have a user accessible file system or something. What kinds of vanilla commands could be done to simulate what vim-surround does?

And if there isn't a clean alternative shouldn't the facilities vim-surround offeres be built into the base executable to begin with? I find it hard to believe we never needed to surround text in Vim prior to Tpope's amazing plugin. There has to have been more basic ways to accomplish such.


CONFIDENTIALITY NOTICE: The information contained in this message may be privileged and/or confidential. It is the property of CrowdStrike.  If you are not the intended recipient, or responsible for delivering this message to the intended recipient, any review, forwarding, dissemination, distribution or copying of this communication or any attachment(s) is strictly prohibited. If you have received this message in error, please notify the sender immediately, and delete it and all attachments from your computer and network.

Tim Chase

unread,
Apr 18, 2023, 11:27:49 AM4/18/23
to 'Devin Weaver' via vim_use
On 2023-04-18 07:29, 'Devin Weaver' via vim_use wrote:
> leads me to wonder what are the "vim --clean" ways to perform the
> same actions that vim-surround offers.

vim-surround offers a number of features, some easier to replicate,
others more challenging.

I commonly use

c<motion/object>'<c-r>"'<esc>

to wrap <motion/object> in quotes, using control+r followed by
double-quote to enter the text of the most recent deletion/yank.
It's not repeatable with "." (well, it is, but it repeats the
original replacement, not with the second text). However you can
record a macro if you need to redo that. I can do the same sort
of thing if I'm using tags, things like

caw<a href="#target"><c-r>"</a><esc>

to wrap the current link in an HTML <a> element.

For large numbers of changes, I tend to switch to using :g or :s
commands

:g/pattern/-pu='<p>'|+pu='</p>'

:%s@pattern@<a href="#target">&</a>@g

rather than trying to do one and then "."-repeat subsequent ones.

-tim





Christian Brabandt

unread,
Apr 19, 2023, 8:35:34 AM4/19/23
to vim...@googlegroups.com

On Di, 18 Apr 2023, Tim Chase wrote:

> On 2023-04-18 07:29, 'Devin Weaver' via vim_use wrote:
> > leads me to wonder what are the "vim --clean" ways to perform the
> > same actions that vim-surround offers.
>
> vim-surround offers a number of features, some easier to replicate,
> others more challenging.
>
> I commonly use
>
> c<motion/object>'<c-r>"'<esc>
>
> to wrap <motion/object> in quotes, using control+r followed by
> double-quote to enter the text of the most recent deletion/yank.
> It's not repeatable with "." (well, it is, but it repeats the
> original replacement, not with the second text).

If you use the small delete register, it is dot-repeatable. So for e.g.
quoting a single word, cw'<c-r>-' works quite well, which for me is the
main surround use case.


Best,
Christian
--
Konsequent ist nur der, der sich selber mit den Umständen wandelt.
-- Winston Spencer Churchill

Tim Chase

unread,
Apr 19, 2023, 9:11:33 AM4/19/23
to vim...@googlegroups.com
On 2023-04-19 14:35, Christian Brabandt wrote:
> On Di, 18 Apr 2023, Tim Chase wrote:
> > c<motion/object>'<c-r>"'<esc>
[snip]
> > It's not repeatable with "." (well, it is, but it repeats the
> > original replacement, not with the second text).
>
> If you use the small delete register, it is dot-repeatable.

Strange in its inconsistency, that one would remember that part of
the source-text was from a register while the other doesn't.

Is there documentation on why this difference? I poked around at

:help "-
:help i_CTRL-R (and following on its variants)
:help .
:help simple-change

but didn't see anything noting this small-delete peculiarity.

-tkc





Christian Brabandt

unread,
Apr 19, 2023, 9:29:08 AM4/19/23
to vim...@googlegroups.com

On Mi, 19 Apr 2023, Tim Chase wrote:

> Strange in its inconsistency, that one would remember that part of
> the source-text was from a register while the other doesn't.
>
> Is there documentation on why this difference? I poked around at
>
> :help "-
> :help i_CTRL-R (and following on its variants)
> :help .
> :help simple-change
>
> but didn't see anything noting this small-delete peculiarity.

Well, it was a small annoyance of me in particular :)
https://github.com/vim/vim/pull/7527

You are right, it's not documented. Do you have an idea on how to
improve the documentation?


Best,
Christian
--
Die Jugend ist etwas Wundervolles. Es ist eine wahre Schande, daß man
sie an Kinder vergeudet.
-- George Bernard Shaw

Romain Lafourcade

unread,
Apr 24, 2023, 8:30:20 AM4/24/23
to vim_use
I have gathered a bunch of native alternatives to some of Surround's features in this gist: https://gist.github.com/romainl/ca742f241457b8609836202fe05ee5c0 for the fun of it.
Reply all
Reply to author
Forward
0 new messages