Learn Practice Daily golf Reference

How to put quotes or brackets around a word in Vim

In plain Vim, put the cursor on the word, press ciw, type "", press Esc, then P to paste the word back between the quotes. With the vim-surround plugin, ysiw" does it in one command.

The ninja rolling one glowing word tile up in a sheet of foil like a burrito, the two twisted ends of the foil shaped like quote marks, a ysiw" keycap on the counter.

The menu page won't build. Someone filled in today's specials without quotes, so JavaScript reads Carnitas as the name of a variable and can't find one. Both values need a double quote at each end, and Vim has no single key for that.

Before
special: Carnitas,
ciw""EscP
special: "Carnitas",
ciw cuts the word and leaves you typing where it was. You type both quotes, Esc puts the cursor on the closing one, and P pastes the word in front of it
Try it here

Quote the specials

The cursor is on today's special.

Cut the word with ciw, type "", press Esc, then paste the word back with Pspecials.js
1export const today = {
2 special: Carnitas,
1 topping: Guacamole,
2};
NORMALLoading the editor…0.0sLn 2, Col :15
Open this text in the playground

This editor needs a keyboard. Read on, and play it at your computer.

Wrap a word in one command with vim-surround

Typing the quotes and pasting the word back gets old by the third time. Tim Pope's vim-surround plugin adds a verb for it. ys wraps text. After it comes what to wrap, named the way you'd name it after d or c, and then the character to wrap it in. So ysiw" reads as "wrap the inner word in double quotes", and the cursor can be anywhere in the word.

ys, cs and ds all come from that plugin, so plain Vim and Neovim only have them once you install it or something like it. VS Code's Vim extension and Zed's Vim mode have them built in, and IdeaVim needs one line in its config. The notes at the bottom of the page say how for each editor. The editor on this page runs them, so you can try them here whatever you use.

Try it here

Tomorrow's specials

Same mistake, next day's file. This time, use the plugin.

Wrap the special in double quotes with ysiw"specials.js
1export const tomorrow = {
2 special: Barbacoa,
1 topping: Elote,
2};
NORMALLoading the editor…0.0sLn 2, Col :16
Open this text in the playground

This editor needs a keyboard. Read on, and play it at your computer.

Change the quotes, or take them off

Two more verbs come with the plugin. cs changes the pair around the cursor. You name the pair that's there now, then the one you want, so cs"' turns "Horchata" into 'Horchata'. ds deletes a pair and keeps what was inside, so ds" turns "3.50" back into 3.50. As with ci", the cursor can be anywhere inside the quotes.

Try it here

The drinks list

The linter wants single quotes in this file, and a price in quotes is text, which won't add up at the till. The cursor is inside the drink's name.

Swap the double quotes for single ones with cs"'drinks.js
1export const drinks = {
2 label: "Horchata",
1 price: "3.50",
2};
NORMALLoading the editor…0.0sLn 2, Col :14
Open this text in the playground

This editor needs a keyboard. Read on, and play it at your computer.

Without the plugin, taking quotes off is a small trick of its own. di" cuts what's inside and leaves the cursor on the closing quote. v h selects that quote and the one before it, and p pastes the text over both.

Before
price: "3.50",
di"vhp
price: 3.50,
The price goes out with di", the two empty quotes get selected with v h, and p puts the price where they were

Brackets, tags and more than one word

The character at the end of a ys goes on both sides, and a bracket brings its partner. Which bracket you type decides the spacing. A closing one wraps tightly, so ysiw) gives (word). An opening one adds a space inside each end, so ysiw( gives ( word ). Braces and square brackets work the same way.

For HTML, type a whole tag as the pair. ysiw<em> gives <em>word</em>, and with cs and ds the letter t stands for the tags around the cursor, so dst strips them off. yss wraps the whole line. For more than one word, select the words in Visual mode first, then press S and the character or tag.

Try it here

New on the board

New items on the specials board go in italics. The cursor is on the first one.

Wrap *Mushroom* with ysiw<em>menu.html
1<ul class="specials">
2 <li>Mushroom tacos</li>
1 <li>Al Pastor tacos</li>
2</ul>
NORMALLoading the editor…0.0sLn 2, Col :11
Open this text in the playground

This editor needs a keyboard. Read on, and play it at your computer.

KeysWhat it does
ciw "" Esc Pquotes round the word under the cursor, in any Vim, no plugin (type () instead of "" for brackets)
di" v h ptakes the quotes off, no plugin
ysiw"wraps the word in double quotes
ysiw) / ysiw((word) / ( word ), and the same for ] [ and } {
ysiw<em><em>word</em>
yss)wraps the whole line, from its first character that isn't a space
S" (Visual mode)wraps the selection
cs"'changes the double quotes around the cursor to single quotes
ds" / ds)takes off the quotes or brackets around the cursor and keeps the text
dsttakes off the HTML tags around the cursor