Lesson 5 — Search, Yank, Registers and Marks
Half of what goes into your next prompt is already on the screen. The path that failed, the URL the server printed, the identifier you are about to ask about, the paragraph worth quoting back — none of it needs typing, only fetching.
That matters because the prompts you write for a coding agent are full of things you did not invent: file paths, error strings, ids, names. Retyping them is slow and it is where typos come from, and a typo in a path sends an agent somewhere you did not mean. This lesson is how you find a thing you can see, take a clean copy of it, and drop it into what you are writing — without touching a pointer and without spelling anything twice.
This is the lesson that pays for the other four. Modes, motions, operators and text objects all exist so that this part is quick.
Searching for what you cannot see
Press /, type what you are looking for, press Enter. The cursor jumps to the match and the match is left highlighted.
| Press | What happens | Where it works |
|---|---|---|
/ | search forwards — type, then Enter | Both |
? | search backwards | Both |
n | next match | Both |
N | previous match | Both |
While you are typing a query, Backspace edits it and Esc abandons it. In a terminal the search runs over the output you have already scrolled through, so /error then n n walks you back through a long run of output without touching the scrollbar.
Where this works: both halves. In a code editor :noh clears the leftover highlight afterwards; a terminal has no : and needs no clearing.
Searching for the word you are already on
You will often want to find every other mention of something you can see right now — a filename, a request id, a variable. You do not have to retype it.
*— search forwards for the word under the cursor.#— the same, backwards.
Then n walks the matches. Put the cursor on the word, press *, and you are already searching.
Where this works: both halves. This is the single biggest saving on a phone, where typing a query is the expensive part of a search and tapping onto a word costs nothing.
Yank — copying with a verb
y is the copy operator, and it is an operator like d and c: it takes a motion or a text object, and a count if you want one.
| Press | Copies | Where it works |
|---|---|---|
yy | the whole line | Both |
3yy | three lines | Both |
yw ye y$ | to the next word / end of word / end of line | Both |
yiw yaw | the word under the cursor, without / with its trailing space | Both |
yiW | the whole run of non-space — one press for a URL | Both |
yi" | what is inside the quotes on this line | Both |
yip yap | the block you are in — in a terminal, one run of output rows — without / with the blank line after it | Both |
y} | from here to the end of the block | Both |
One terminal detail worth knowing: a yank that crosses a row — y} over a block, or 3yy — comes out linewise, whole lines at a time, which is what Vim does anyway. And unlike d and c, a yank may cross as many rows as you like.
What a bare y takes
Press y on its own and it copies the selection you just made — a visual selection, or the match a search has just landed on. So the shortest way to copy a word you went looking for is: / , type it, Enter, y.
It is fussy about which selection, and that works in your favour. A terminal can be holding an old highlight from a stray drag or an earlier Select All; that one does not count. Press yiw with a leftover highlight on screen and you get the word under the cursor, which is what you asked for.
yy still works for the same reason: a bare y only becomes the line operator when there is nothing fresh to take instead.
Where this works: terminals. In a code editor y in visual mode copies the selection in the ordinary Vim way.
Your copy also lands on the system clipboard
In a terminal, every yank goes to the system clipboard as well as to Vim's own register, and p reads that same clipboard. So it works in both directions: yiW on a URL then paste into any other app on the device, and equally, copy something in another app and p it straight into your prompt. Named registers are the exception — "ap means register a and never the clipboard.
What you copy comes out clean
A coding agent draws its output inside a box: a couple of spaces of padding down the left, sometimes line numbers, and its own word wrapping. Copy a paragraph out of one and all of that used to come with it, so pasting it back into the prompt gave you a stack of short indented lines that the prompt then wrapped again, breaking words in places nothing had broken them before.
Copying here undoes the drawing. The padding comes off, line numbers come off, and rows the program wrapped are put back into the sentence they were. yip on a paragraph of output gives you the paragraph.
Code is left exactly as it is, which is the more important half. Newlines and indentation are what code means, so a listing, a table or a list is never reflowed — only text that shows positive evidence of having been word-wrapped is touched. This applies to dragging with a pointer too, so both ways of copying give you the same thing.
Paste
| Press | What it does | Where it works |
|---|---|---|
p P | in a code editor, paste after / before the cursor | Both |
p in a terminal | pastes your system clipboard into what you are typing — so it reaches text copied in any other app, not just what you yanked here. P does the same | Terminals |
3p | sends it three times | Terminals |
p after v + motion | replaces the selection, on the line you are typing | Terminals |
p is a normal-mode command, so it comes BEFORE you go back to typing. Paste first, then press i. Press i first and you are in insert mode, where p is simply the letter p — which is the one ordering mistake everybody makes once.
Pasting into a terminal is bracketed wherever the program asks for it, which every current shell and CLI agent does. That means a multi-line yank arrives in the input as text and waits for you to press Enter, instead of running a line at a time as it arrives. Copying three lines of a command out of the scrollback and pasting them back is an ordinary thing to do here, not a trick.
Registers — collecting more than one thing
The plain y always writes to one unnamed register, so a second yank replaces the first. When you want three things off one screen, name them.
"abefore a yank stores it there:"ayiw."appastes that one back.- Letters and digits both work.
In a terminal the "a prefix applies to the next command only. The recipe:
"ayiwon the first word."byiwon the second."cyiwon the third."ap"bp"cpwherever you need them, thenito get back to typing.
Where this works: both halves. Code editors give you the full set of named registers "a to "z, and :reg lists what is in them.
Marks — keeping your place
You have found the error. Now you want to read further up, and then come back to exactly where you were.
mthen a letter drops a mark —ma.- Backtick then that letter returns you to the exact spot.
- Apostrophe then that letter returns you to the start of that line, which is what you want when the mark meant "that error, somewhere up there".
Marks are letters only. In a terminal a mark holds its place in the output even as new lines arrive and push the screen up, which is the whole point of dropping one before you go and look at something else.
Where this works: both halves. In code editors the backtick jump is the one to reach for.
Opening a link
gx opens the URL under the cursor, and so does Enter. If there is no URL exactly under the cursor it takes the next one on the line — so landing on the right line is enough, and you never have to aim at the "h" of "https".
Where this works: terminals.
What code editors add
A code editor is a buffer you own, so the copying half gets bigger:
- The full register set,
"ato"z, kept until you overwrite them. - Macros —
qthen a letter starts recording,qstops,@then that letter replays. Recording one fix and replaying it down a file is the thing people mean when they say Vim made them fast. - The command line for search work:
:%s/old/new/gto substitute,:nohto clear a highlight.
Terminals have none of these: no : commands and no macros. Both exist to repeat edits across a file, and a terminal only ever edits the one line you are typing.
Try it
Open a terminal, turn Vim keys on from its own menu, and run something that prints a lot — ls -la, or ask a CLI agent a question and let it answer.
- Press
Escto reach normal mode. TheNORMALbadge appears. - Press
/, type a word you can see, press Enter. The match is highlighted. - Press
yon its own. You just copied the match — and it is on your system clipboard. - Press
na few times to walk the other matches, thenNto come back. - Put the cursor on any word and press
*, thenn. Same search, nothing typed. - Press
mato drop a mark, pressggto jump to the top, then backtickato come straight back. - Move onto a line with a quoted path in it and press
yi". Thenpto drop it into what you are typing, andito carry on. Escback to normal — step 7 left you typing — then try the collection:"ayiwon one word,"byiwon another, then"ap"bpto place them, andiwhen you are done.
You now know: how to find anything on screen, copy it with a verb rather than a drag, keep several pieces at once, and drop them into the prompt you are writing — without spelling a path or an id twice.
Related
← Lesson 4 — Text Objects · Lesson 6 — Terminals and CLI Coding Agents →