Learn Practice Daily golf Reference

How to go to the end of a line in Vim

In Normal mode, press $ to go to the end of the line and 0 to go to the beginning. ^ goes to the first character after the indent. A and I go to the end or start and let you type.

Try it here

Fix the line ends

Three small fixes, each at one end of a line: a doubled semicolon, a missing one, and a missing return at the start of the indented code.

1 of 3Delete the extra ; at the end: $, then x
1function total(items) {
2 const sum = items.reduce((a, b) => a + b, 0);;
1 const tax = sum * 0.2
2 sum + tax;
3}
NORMALLoading the editor…1/30.0s2:15

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

KeysGoes to
$the last character of the line
0the first column, before any indent
^the first non-blank character, after the indent
g_the last non-blank character (skips trailing spaces)
Athe end of the line, in Insert mode
Ithe first non-blank character, in Insert mode
gIthe first column, in Insert mode
3$the end of the line two below (a count moves down count - 1 lines)
D / Cdeletes / changes from the cursor to the end of the line (same as d$ / c$)
d0 / d^deletes from the cursor back to the start / to the first non-blank

0 or ^?

In code most lines are indented, and the two differ by exactly that indent. 0 goes to column one, before the spaces; ^ skips them and lands on the first real character, which is usually where you want to be. I works like ^ followed by i: it starts typing in front of the code, not in front of the indent. The Home and End keys also work in Vim, in Normal and Insert mode, but 0, ^ and $ are on the home row and combine with operators.

Motions, not just jumps

All three work after an operator: d$ deletes to the end of the line, y^ copies from the first character to the cursor, c0 changes everything before the cursor. $ in Visual mode takes the line break too, so v$d joins the next line onto this one; g_ stops on the last character, so vg_y copies the rest of the line without it.