sponsor Vim development Vim logo Vim Book Ad

SearchAlternatives : Add / subtract alternatives to / from the search pattern.

 script karma  Rating 9/7, Downloaded by 1655  Comments, bugs, improvements  Vim wiki

created by
Ingo Karkat
script type
This plugin provides mappings and commands to add and subtract alternative
branches to the current search pattern. Currently searching for "foo," but
also want to find matches for "bar"? You could type /foo\|bar<CR> or
/<C-R>/\|bar<CR>, but once you want to limit the search to whole \<words\>
(like the star command), and juggle several alternatives, adding and
dropping them as you search, this plugin is quicker than manually editing the
search command-line (which you can still do).

- Add to Word Search (vimscript #3955) can add the keyword under the cursor to
  the search pattern and search forward / backward like * / #.

<Leader>+               Add the current whole \<word\> as an alternative to the
                        search pattern, similar to the star command.

<Leader>g+              Add the current word as an alternative to the search
                        pattern, similar to the gstar command.

{Visual}<Leader>+       Add the current selection as an alternative to the
                        search pattern.
                        For a blockwise-visual selection, each line of the
                        block (minus leading and trailing whitespace) is added
                        as a separate alternative search pattern. When the
                        first selected line of the block is surrounded by non-
                        keyword characters, the match is done with \<...\>.
                        If the blockwise-visual selection is only comprised
                        of a single line, all WORDs of that line are added as
                        separate alternative whole \<word\> search patterns.

<Leader>-               Remove the current whole \<word\> from the alternatives
                        in the search pattern.

<Leader>g-              Remove the current word from the alternatives in the
                        search pattern.

{Visual}<Leader>-       Remove the current selection from the alternatives in
                        the search pattern.
                        For a blockwise-visual selection, this has the same
                        special behavior as {Visual}<Leader>+.

:SearchAdd {expr}       Add {expr} as an alternative to the search pattern.

:SearchRemove {expr}    Remove {expr} from the alternatives in the search
:[N]SearchRemove        Remove the [N]'th / last alternative from the
                        alternatives in the search pattern.
install details
The code is hosted in a Git repo at
You can use your favorite plugin manager, or "git clone" into a directory used
for Vim packages. Releases are on the "stable" branch, the latest unstable
development snapshot on "master".

This script is also packaged as a vimball. If you have the "gunzip"
decompressor in your PATH, simply edit the *.vmb.gz package in Vim; otherwise,
decompress the archive first, e.g. using WinZip. Inside Vim, install by
sourcing the vimball or via the :UseVimball command.
    vim SearchAlternatives*.vmb.gz
    :so %
To uninstall, use the :RmVimball command.

- Requires Vim 7.0 or higher.
- Requires the ingo-library.vim plugin (vimscript #4433), version 1.035 or

rate this script Life Changing Helpful Unfulfilling 
script versions (upload new version)

Click on the package to download.

package script version date Vim version user release notes
SearchAlternatives-1.11.vmb.gz 1.11 2020-01-31 7.0 Ingo Karkat - Abort :SearchRemove command on error.
- ENH: When adding a pattern (e.g. /foo/) when a shorter prefix (/fo/) already exists, the shorter one will eclipse the longer one (only "fo" will be matched in "foobar"). A new algorithm determines / estimates (in case of multis) the match length, and adds shorter ones after longer ones, to avoid the problem. In case of varying match lengths (e.g. with * and \+ multis), it cannot completely fix it, but it's always equal or better than the original simple addition at the end.
- ENH: When removing a branch, existing global regexp flags like for case sensitivity (/\c, /\C) and regexp engine type (/\%#=0) caused a mismatch, and the corresponding branch could not be found. Now, we extract those flags and segregate them from the comparisons. On addition via SearchAlternatives#AddPattern(), make global flags unique and put them to the front of the search pattern. *** You need to update to ingo-library (vimscript #4433) version 1.035! ***
SearchAlternatives-1.10.vmb.gz 1.10 2013-07-19 7.0 Ingo Karkat - ENH: Blockwise <Leader>+ / <Leader>- add / remove each partial selected trimmed line as a separate search alternative, or individual words when a single line is blockwise-selected.
- ENH: Implement command completion that offers existing alternatives (to remove or clone-and-modify them).
- Add dependency to ingo-library (vimscript #4433). *** You need to separately install ingo-library (vimscript #4433) version 1.009 (or higher)! ***
SearchAlternatives.vba.gz 1.00 2012-07-26 7.0 Ingo Karkat Initial upload
ip used for rating:

If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to the maillist. Help Bram help Uganda.