Seach and follow with keystrokes or a macro

21 views
Skip to first unread message

Gua Chung Lim

unread,
Jul 10, 2021, 9:24:12 AM7/10/21
to vim...@googlegroups.com
Hi,

I have been using vim for years, I usually use @ macros. I have a question hard to explain. Assuming I have a text file ...

keyword: toggle CASE Retentive Case
keyword: TOGGLE case Retentive Case
keyword: togGLE CASE Retentive Case
...

And I want to toggle the case of some letters after ^keyword:.
Things I usually do is ...
/^keyword
then do the following keystrokes ...
9lv10l~0
And for the rest of file I can simply do ...
n.
to find the next match (n), and repeat the keystrokes (.)

Certainly, I can assign 9lv10l~0 to a macro. But what if I have 1,000+ lines like this, I would never want to use the macro for every line containing "^keyword". Is there anyway to search and replace in a single colon command? Like ...

:%s/^keyword/do_keystrokes/g
or
:%s/^keyword/do_a_macro/g

Thanks in advance,

--
Gua Chung Lim

Nothing ever exists entirely alone without dualities.

Suresh Govindachar

unread,
Jul 10, 2021, 11:56:54 AM7/10/21
to vim...@googlegroups.com
On 7/10/2021 6:23 AM, Gua Chung Lim wrote:
> Hi,
>
> I have been using vim for years, I usually use @ macros. I have a question hard to explain. Assuming I have a text file ...
>
...
>
> Certainly, I can assign 9lv10l~0 to a macro. But what if I have 1,000+ lines like this, I would never want to [manually] use the macro for every line containing "^keyword". Is there anyway to search and replace in a single colon command? Like ...
>
> :%s/^keyword/do_keystrokes/g
> or
> :%s/^keyword/do_a_macro/g
>

Yes -- see :help :g or :help multi-repeat

--Suresh

c.willis111

unread,
Jul 10, 2021, 12:15:46 PM7/10/21
to vim...@googlegroups.com
Hi

as you say, the task is not terribly clear.

Once you've done a

/^keyword

you can find further instances with

n

It is not clear if you have some rule for the case required for various words. I notice that case occurs with different capitalisation where it hasn't obviously been mangled. If there's no rule, you're stuck with going through them all. The usual swap case is just

~

So you can go through finding the next keyword with n and then right arrowing or tilding to get that line right.

If on the other hand you can devise a rule for the capitalization, you can devise a set of commands such as

:g/^keyword/s/toggle\c/TOGGLE/
:g/^keyword/s/CASE\c/CASE/g
:g/^keyword/s/CASE$/Case/

which wd change all the toggles and cases to upper case, and then change all CASEs at the end of line to mixed case. The g command limits the change to lines starting keyword (lower case).

regards - Chris Willis

Nothing ever exists entirely alone without dualities.

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsub...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/YOmfbxPeLlbqmz2M%40gmail.com.

Gua Chung Lim

unread,
Jul 11, 2021, 5:34:38 AM7/11/21
to vim_use
* 'c.willis111' via vim_use <vim...@googlegroups.com> wrote:
> as you say, the task is not terribly clear.
> Once you've done a
> /^keyword
> you can find further instances with
> n
That's what I said.

> > Is there anyway to search and replace in a single colon command? Like ...
> >
> > :%s/^keyword/do_keystrokes/g
> > or
> > :%s/^keyword/do_a_macro/g
> It is not clear if you have some rule for the case required for various
> words. I notice that case occurs with different capitalisation where it
> hasn't obviously been mangled. If there's no rule, you're stuck with
> going through them all. The usual swap case is just
> ~
I know ~ and I have said that. Practically, there are many more keystrokes in different situations other than those of the letter case. Probably, my example is not good enough. My real question is that how to apply keystrokes in a single colon (:) command.

> So you can go through finding the next keyword with n and then right
> arrowing or tilding to get that line right.
> If on the other hand you can devise a rule for the capitalization, you
> can devise a set of commands such as
> :g/^keyword/s/toggle\c/TOGGLE/
> :g/^keyword/s/CASE\c/CASE/g
> :g/^keyword/s/CASE$/Case/
Probably, :g is what I am looking for. I previously thought of :%s. Thanks,

* 'Suresh Govindachar' via vim_use <vim...@googlegroups.com> wrote:
> Yes -- see :help :g or :help multi-repeat
I shall try that.

Thank you very much.

--
Gua Chung Lim

lgc

unread,
Jul 11, 2021, 6:51:30 PM7/11/21
to vim_use

   > Is there anyway to search and replace in a single colon command?

Yes, use `:normal`, possibly in combination with `:global`.  Record your key presses (`    9lv10l~0`) into the macro `q`.  Then, execute:

    :%norm! @q

To execute the macro on all the lines in the file.

Or:

    :*norm! @q

To execute the macro on all the lines which were in the last visual selection.

Or:

    :g/^keyword:/norm! @q

To execute the macro on all the line starting with the text `keyword:`.

Gua Chung Lim

unread,
Jul 12, 2021, 8:42:15 AM7/12/21
to vim...@googlegroups.com
* lgc <jdupo...@gmail.com> wrote:
> > Is there anyway to search and replace in a single colon command?
> Yes, use `:normal`, possibly in combination with `:global`. Record your
> key presses (` 9lv10l~0`) into the macro `q`. Then, execute:
>
> :%norm! @q
>
> To execute the macro on all the lines in the file.
>
> Or:
>
> :*norm! @q
>
> To execute the macro on all the lines which were in the last visual
> selection.
>
> Or:
>
> :g/^keyword:/norm! @q
>
> To execute the macro on all the line starting with the text `keyword:`.
Yes, thanks, I also found ...

:g[!]/pattern/exec 'norm[!]' "/pattern\<CR><cmd>"

It is very interesting.
Reply all
Reply to author
Forward
0 new messages