Vim from zero: a global command repeats one change
:g runs a command on every line that matches. :g/^item /normal cwadd changes each order line that starts with item and a space. A note line stays.
Zoe is one of GoByte's characters. This post was drafted by AI agents in Zoe's voice, then fact checked, run and edited by the GoByte team.
The dot command repeats a change where you point. :g points for you. It finds every line that matches a pattern and runs a command there. normal means the command is a sequence of keys, the same keys you would type. An order line changes. A note line does not.
The pattern chooses the lines. The keys are the change#
:g finds every line matching a pattern and runs a command there. normal means the command is keys, as if you had typed them, with the cursor starting on the first character of the match. ^item requires item and a space at the start of the line, so itemized does not match and an item in the middle does not match. cwadd changes that first word to add. The order becomes add latte = "oat". A line that says note keep is not an order, and it stays. u undoes the whole :g. Try the keys on one line before you hand them to every match.
- ^itemcwaddadd latte = "oat"
- notekeptnote keep
:g/^item /normal cwadd
add latte = "oat"
note keep
The pattern chooses the lines#
Start with two lines:
item latte = "oat"
note keep
:g/^item / matches a line whose first characters are item and a space. normal cwadd runs cwadd and an implicit Escape: change the word under the cursor to add. The cursor on a :g match starts at the first character, which is the i, so the word is item. The order line becomes add latte = "oat". note keep does not match, so it is still note keep.
A line that says item in the middle does not match ^item , because ^ means the start. A line that says itemized does not match, because of the space in the pattern. Tighten the pattern before you widen it. :g will apply the keys to every match, and a loose pattern edits lines you only meant to read.
Read the pattern, then the keys#
The command is one line: global, pattern, normal, keys. If the keys are wrong, u undoes the whole :g. Try the keys on one line first, the way lesson 4 did, and only then hand them to :g. The pattern is the list of orders. The keys are the one change. That split is the same idea as an object and a method. The line is the data. The command is the behavior you apply to each match.
Your product here? Partner with us
Back to topDiscussion
No comments yet. Signed in GoByte members with a verified e-mail can join. Community guidelines
Reading is open to everyone. Commenting and voting need a GoByte account with a verified e-mail.