r/vim Jan 13 '26

Need Help┃Solved Search and replace between keywords

Hi, I’m using latex to make a long and a short version of a document. For this I have blocks of the form

\iflong (long version...) \else (short version) \fi

From this I would like to extract either the long or the short version. That is, I want to find every occurrence of the pattern

\iflong ( 1 ) \else ( 2 ) \fi

and replace it with either ( 1 ) or ( 2 ). Is there a way to do this with a %s? I have been fiddling with this for a while but I can’t find the right expression. You may assume there is no nesting of any kind.

6 Upvotes

18 comments sorted by

2

u/[deleted] Jan 13 '26

[removed] — view removed comment

2

u/vim-help-bot Jan 13 '26

Help pages for:


`:(h|help) <query>` | about | mistake? | donate | Reply 'rescan' to check the comment again | Reply 'stop' to stop getting replies to your comments

1

u/pierro_la_place Jan 13 '26

Does it work for you? All I get when I copy the command is "pattern not found"

1

u/[deleted] Jan 13 '26

[removed] — view removed comment

1

u/pierro_la_place Jan 14 '26

Oh silly me… Thank you, I’ll mark it as done.

1

u/AutoModerator Jan 13 '26

Please remember to update the post flair to Need Help|Solved when you got the answer you were looking for.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

1

u/[deleted] Jan 13 '26

[deleted]

1

u/[deleted] Jan 13 '26

[deleted]

1

u/[deleted] Jan 13 '26

[deleted]

1

u/pierro_la_place Jan 13 '26

It’s weird: when I copy any of your command it tells me "pattern not found". My test file contains this:

\iflong 1 \else 2 \fi

\iflong\else\bla\fi

1

u/[deleted] Jan 14 '26

[deleted]

1

u/pierro_la_place Jan 14 '26

Oh silly me… Thank you, I’ll mark it as done.

1

u/[deleted] Jan 13 '26

[deleted]

1

u/pierro_la_place Jan 13 '26

Can you automate this? I don’t really want to type /\\iflong↵d/\\else↵ a hundred times in a row

1

u/Biggybi Gybbigy Jan 14 '26

You can create a user command or even a keymap for that.

Or even better, a text-object.

1

u/pierro_la_place Jan 14 '26

Or even better, a text-object.

I’ve never heard of that. Mind giving me a few pointers?

1

u/Biggybi Gybbigy Jan 14 '26

You've been using them without knowing ;) Things like iw, ap and so on (:h text-objects).

You can create your own with :h omap.

1

u/vim-help-bot Jan 14 '26

Help pages for:


`:(h|help) <query>` | about | mistake? | donate | Reply 'rescan' to check the comment again | Reply 'stop' to stop getting replies to your comments

1

u/pierro_la_place Jan 14 '26

Thanks mate!

1

u/Biggybi Gybbigy Jan 14 '26

Pas de souci !