Learn Practice Daily golf Reference

How to add text to the start or end of every line in Vim

Type :%s/$/,/ and press ↵ to add a comma to the end of every line. :%s/^/- / puts - at the start instead. In the pattern, $ means the end of a line and ^ its start.

The ninja walking down a stack of clay line strips of different lengths with a piping bag, piping a glowing comma onto the end of each one, under a sign reading :%s/$/,/.

Four orders were charged twice at Byte Burrito last night. The till's report gives you their numbers, one per line, and they're going into the refund script as an array, so each one needs a comma after it.

:%s/$/,/ does every line at once. It's Vim's find and replace, with % for every line and s for substitute, and $ as the thing to find. In a pattern, $ means the end of the line, the spot just after its last character, so replacing it puts a comma there and takes nothing away. One u takes them all off again.

Before
1041
1042
1045
1050
:%s/$/,/↵
1041,
1042,
1045,
1050,
A comma lands at the end of every line, the last one included
Try it here

Commas for the refund script

The cursor is on the first order number.

Type :%s/$/,/ and press ↵refunds.txt
11041
11042
21045
31050
NORMALLoading the editor…0.0sLn 1, Col :1
Open this text in the playground

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

Add text to the start of every line

^ is the start of the line, so :%s/^/- / puts - in front of every line. That turns the kitchen's list of fillings into a bulleted list for the menu page, which is written in Markdown.

The start here is column one, in front of any indent. For indented lines, :normal further down does better.

Try it here

A bulleted list of fillings

Make the kitchen's list into a bulleted list.

Type :%s/^/- / with a space after the dash, and press ↵fillings.md
1carnitas
1al pastor
2black beans
3grilled peppers
NORMALLoading the editor…0.0sLn 1, Col :1
Open this text in the playground

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

Wrap every line in quotes

The supplier's order form takes a CSV, and their system wants every name in double quotes. That's text at both ends, and one command still does it. In the pattern, .* matches the whole line, whatever is on it. In the replacement, & stands for whatever matched. So :%s/.*/"&"/ swaps each line for itself with a quote on either side.

Anything can go around the &. :%s/.*/('&'),/ turns carnitas into ('carnitas'),, ready for a SQL insert.

Try it here

Quotes for the supplier

Wrap the fillings for the supplier, then change your mind.

Type :%s/.*/"&"/ and press ↵fillings.csv
1carnitas
1al pastor
2black beans
3grilled peppers
NORMALLoading the editor…0.0sLn 1, Col :1
Open this text in the playground

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

Add text after the indent, on the lines you select

The shop's settings file lists the fillings under a heading, indented two spaces, and YAML wants - in front of each one. This time only three lines need it, and the dash has to go after the indent, where ^ won't put it.

Select the lines first. V selects the line the cursor is on, and j stretches the selection down. Press : and Vim types :'<,'> for you, which means the selected lines. Every command above works on them, :'<,'>s/$/,/ included.

For the indent, type normal and some keys after it. :normal presses those keys on each line in turn, in Normal mode, starting from the beginning of the line. I starts typing after the indent, so :'<,'>normal I- types the dash where it belongs. A starts typing at the end of the line, so :'<,'>normal A, does what $ did. Vim leaves Insert mode by itself at the end of each line.

Try it here

Fillings in the settings file

The cursor is on carnitas.

Select the three fillings with V and j j. Press :, type normal I- with a space after the dash, and press ↵shop.yml
2shop: Byte Burrito
1fillings:
3 carnitas
1 al pastor
2 black beans
NORMALLoading the editor…0.0sLn 3, Col :3
Open this text in the playground

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

Add text to the end of a few lines with Visual Block

If you'd rather watch it happen, select a column. Press Ctrl-v (hold Ctrl, press v) and move down with j, then press $ to stretch the block to the end of every line, however long each one is. A starts typing after the block. Only the first line shows your text while you type, and the rest fill in when you press Esc.

For the front of the lines, start the block on the first character, leave out the $, and press I instead of A. A block needs the lines to sit together, so it suits a short run like the three lines of this CSS rule that lost their semicolons. Visual Block mode has the rest.

Try it here

Semicolons for the menu styles

The cursor is on color, the first line without its semicolon.

Start the block with Ctrl-v and take it down with j j. Press $, then A, type ; and press Escmenu.css
1.menu {
2 color: tomato
1 font-weight: bold
2 padding: 1rem
3}
NORMALLoading the editor…0.0sLn 2, Col :3
Open this text in the playground

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

TypeAdds
:%s/$/,/ ↵A comma to the end of every line.
:%s/^/- / ↵- to the start of every line, in front of any indent.
:%s/.*/"&"/ ↵A double quote at both ends of every line.
:%s/.*/('&'),/ ↵Brackets, quotes and a comma: carnitas becomes ('carnitas'),.
:%s/.$/&,/ ↵A comma to the end of every line that isn't blank.
:5,12s/$/;/ ↵A semicolon to the end of lines 5 to 12 only.
:'<,'>s/$/,/ ↵A comma to the end of each selected line (press : in Visual mode and Vim types the '<,'>).
:'<,'>normal A, ↵The same, by pressing A and typing a comma on each line.
:'<,'>normal I- ↵- after the indent of each selected line.
:%norm I# ↵# after the indent of every line (:norm is short for :normal).
:g/./normal A, ↵A comma to every line with something on it (:g/./ picks those lines).
Ctrl-v, select down, $ A, text, EscThe text at the end of each line in the block, however long the line.
Ctrl-v, select down, I, text, EscThe text in front of the block, on each line.
uUndoes the whole lot at once.