Vane
Vane

Reputation: 11

Vim alternatives file

:[range]s/{pattern}/{string}/[flags] [count]
            For each line in [range] replace a match of {pattern}
            with {string}.

The "or" operator in a pattern is "\|".  Example:
    /foo\|bar
This matches "foo" or "bar".  More alternatives can be concatenated:
    /one\|two\|three
Matches "one", "two" and "three".

Can we use a pattern/alternatives file with 3 lines?
one
two
three

Upvotes: 1

Views: 45

Answers (1)

builder-7000
builder-7000

Reputation: 7627

The following command works on my system:

let @a = system('cat repl.vim | tr "\n" "|"') | exe '%s/\v'.@a.'<bs>/x/g'

Here, I have a list of words in the file repl.vim. The first part of the command uses let to save the list of words in registry a replacing every newline \n with an or operator |. In the second part exe %s performs the substitution.

In practice, if repl.vim contains:

pattern1
pattern2
pattern3

Running the command will result in:

%s/\vpattern1|pattern2|pattern3/x/g

Upvotes: 1

Related Questions