💾 Archived View for thrig.me › blog › 2023 › 09 › 09 › vi-to-find-character.gmi captured on 2024-07-09 at 01:09:45. Gemini links have been rewritten to link to archived content

View Raw

More Information

⬅️ Previous capture (2023-11-14)

-=-=-=-=-=-=-

Some #forth folks did not know the f F t T commands in vi(1), so on the odds that other vi users likewise do not know them, what follows is a short primer on these commands. Hopefully your version of vi hasn't mangled these commands too much; I'm using a somewhat modified version of the vi(1) that ships with OpenBSD.

Examples may help.

    .i le mlatu cu cadzu le djacu

Assuming the cursor is at the beginning of the above line, one may want to jump to the "djacu". The command "/dj^M" would get you there, but that's four characters, and you might want to reserve that search buffer for something else. The command "fd" or "find dee" will take the cursor to the "d" in "cadzu", and then the command ";" will move to the next "d". If you notice the "d" in cadzu you maybe could use "2fd" to advance directly to the second "d" found from where the cursor is, but here "fd;" would also work, among other command patterns.

With "^" representing the cursor position followed by vi commands, a clumsy illustration of this may run something like:

    .i le mlatu cu cadzu le djacu
    ^ 2fd
    .i le mlatu cu cadzu le djacu
                            ^ cwdargu^[
    .i le mlatu cu cadzu le dargu

So that's "2fdcwdargu^[" to replace "djacu" (water) with "dargu" (road). I've memorized all this, a lot, so it pretty much happens automatically. Probably like piano scales it needs to be practiced.

Even fancier is to combine the "f" or "t" with the "c" (change) or "d" (delete) commands. With the cursor at the end of the following line of C code, a "T(" and then a "2;" will move the cursor to the "p" in promptbuf. (An "upto" search needs a "2;" to jump over the already adjacent match). Then, "c2t)" will allow you to rewrite the entire argument list to sprintf: change, 2nd match, upto, the letter "t". You could also use "Fp;;" instead of "T(2;" to move back to the "p" in promptbuf, or maybe "0f(l" to get there (beginning of line, find "(", one right).

    sprintf(promptbuf, "Which %c do you want? (0-f)", obj->o_type);

Or, "df." will delete to the end of a sentence, while "ct." will change upto the end of said (assuming no inner "." within the sentence, like this one has). Whether these commands make sense for how you edit things will require practice.

Even more complicated is "yt)", which would yank upto the next ")" character. With the command "ayf) and then "ap (the " is part of the commands here) you can yank and then put what got placed into the "a" buffer.

On an unrelated note, gewöhnungsbedürftig is a funny word I learned about today, being not much exposed to German. On an even more unrelated note, I did once play a bit of a Beethoven piano sonata in West Berlin (this was some years ago).

tags #vi