How to Surround Text in Vim, Without Plugins
Surrounding text in Vim/Neovim is not strictly complicated, but finding a good workflow is not immediately obvious either.
Recipe
- select text (your pick),
c
""
,ESC
P
Step by step below.
Step 1: “changing” text
Select text, and press c
(change mode).
Alternatively, it’s that more convenient, you can use a “text object
selection” like ciw
to “change” a specific something.
select some text
press c
(text is gone)
2 birds with 1 scone:
- ready to type, in insert mode
- the deleted text was automatically copied (as a side-effect)
Step 2: typing wrapper text
Type the wrapping characters yourself. (here: ""
)
Go back to normal mode: press ESC
Step 3: pasting
With the cursor directly on the closing character, press P
(capital p)
Comments
I have used vim-surround for a long time. But when I switched from vim to neovim, I wasn’t sure if I really needed to plugin for this.