sponsor Vim development Vim logo Vim Book Ad

PatternsOnText : Advanced commands to apply regular expressions.

 script karma  Rating 15/9, Downloaded by 474    Comments, bugs, improvements  Vim wiki

created by
Ingo Karkat
 
script type
utility
 
description
DESCRIPTION
This plugin provides a toolbox of commands to delete, substitute, or
de-duplicate text based on a passed regular expression (or the last used
search pattern), something which would otherwise require multiple steps, a
complex sub-replace-expression or a macro. One way or the other, all
commands here are extensions of the :substitute command. Use them whenever
the general built-in doesn't suffice.

SOURCE
:DeleteExcept
    http://stackoverflow.com/questions/6249172/vim-delete-anything-other-than-pattern
:SubstituteInSearch inspired by
    http://stackoverflow.com/questions/13763880/in-vim-how-can-i-search-and-replace-every-other-match
:DeleteDuplicateLinesOf inspired by
    http://stackoverflow.com/questions/14236226/hanging-only-to-the-first-instance-of-the-line-and-deleting-all-further-copies
:DeleteDuplicateLinesIgnoring inspired by
    http://stackoverflow.com/questions/1896793/remove-duplicate-line-in-vim
:DeleteAllDuplicateLinesIgnoring inspired by
    http://stackoverflow.com/questions/22193596/in-vim-how-to-remove-all-lines-that-are-duplicate-somewhere

SEE ALSO
- The ExtractMatches.vim plugin (vimscript #4795) provides commands to yank
  matches into a register, optionally only unique matches without duplicates.
- The EditSimilar.vim plugin (vimscript #2544) uses the same
  {wildcard}={string} replacement pairs of :SubstituteWildcard, but for
  editing a similar file, e.g. via :EditSubstitute.

USAGE
:[range]SubstituteExcept/{pattern}/{string}/[flags] [count]
                        Replace instances of text in the current line /
                        [range] that do not match {pattern} / the last used
                        search pattern with {string}; "g" is implicit. Empty
                        lines (without any whitespace) are kept empty.
                        In {string}, & refers to the text that does not match,
                        whereas \1 etc. refer to the previous match of a
                        sub-expression in {pattern}.
:[range]SubstituteExcept [flags] [count]
                        Repeat the last substitution with the last used search
                        pattern, last used replacement string, and last used
                        :s_flags; "g" is implicit.

:[range]DeleteExcept [{pattern}]
:[range]DeleteExcept /{pattern}/[flags]
                        Delete all text in the current line / [range] except
                        for the text matching {pattern} / the last used search
                        pattern. For [flags], see :s_flags; "g" is implicit.

:[range]SubstituteSelected/{pattern}/{string}/[flags] {answers}
                        Replace matches of {pattern} in the current line /
                        [range] with {string}, determining whether a
                        particular match should be replaced on the sequence of
                        "y" or "n" (a count can be prefixed, e.g. "3y4n2y") or
                        numeric positions "2,5", or ranges "3-5" in {answers}.
                        If no numeric positions follow, the last "y" / "n"
                        sequence repeats for further matches. For example with
                        "ynn", the first match is replaced, the second and
                        third are not, the fourth is again replaced, ...
                        Handles & and \0, \1 .. \9 in {string}, also \=
                        sub-replace-expression and \r, \n, \t, \b.
:[range]SubstituteSelected [flags] [answers]
                        Repeat the last substitution with the last used search
                        pattern, last used replacement string, last used
                        :s_flags, and last used answers.

:[range]SubstituteSubsequent/{pattern}/{string}/[flags]
                        Replace all matches of {pattern} on or after the current
                        cursor position (and in all entire following lines) in
                        [range] with {string}.
                        For a "blockwise to end" replacement that is
                        restricted to positions on and after the current
                        column in all lines, pass the special flag [b].
:[range]SubstituteSubsequent/{pattern}/{string}/[flags] {answers}
                        Replace all matches of {pattern} on or after the current
                        cursor position in [range] with {string}, determining
                        whether a particular match should be replaced on the
                        sequence of "y" or "n" (a count can be prefixed, e.g.
                        "3y4n2y") or numeric positions "2,5", or ranges "3-5" in
                        {answers}, as with :SubstituteSelected.
:[range]SubstituteSubsequent [flags] [answers]
                        Repeat the last substitution with the last used search
                        pattern, last used replacement string, last used
                        :s_flags, and last used answers.

:[range]SubstituteInSearch/{pattern}/{string}/[flags] [count]
                        Within the current search pattern matches, replace all
                        matches of {pattern} with {string}. Shortcut for
                        :%s//\=substitute(submatch(0), "foo", "bar", "g")/gc
                       ~ is replaced with the {string} of the previous
                        :SubstituteInSearch.
                        Handles \= sub-replace-expression in {string}, which
                        can't be used recursively in the above built-in
                        :%s//\=...
                       [flags] and [count] apply to the outer matching of the
                        last search pattern. The [e] from :s_flags also
                        suppresses the error message when {pattern} never
                        matches.
                        To replace only the first occurrence of {pattern},
                        pass the special flag [f].
                        For a case-insensitive substitution, prepend '\c' to
                        {pattern}.
                        Does not modify the last search pattern quote/.
:[range]SubstituteInSearch/{search}/{pattern}/{string}/[flags] [count]
                        Like above, but additionally specifies the {search}
                        pattern instead of using quote/ (which again isn't
                        modified).
:[range]SubstituteInSearch [flags] [count]
                        Repeat last :SubstituteInSearch with same search
                        {pattern} and substitute {string}, but without the
                        same flags or given {search} pattern.

:[range]SubstituteMultiple /{pattern}/{string}/ [...] [flags] [count]
                        Change any of the {pattern} given to the corresponding
                        {string}. As this is done in a single :substitute,
                        this can have different (better) semantics than
                        sequential :s/new/old/ | s/older/newer/ commands. For
                        example, you can swap texts with this.
                        Note: You cannot use capturing groups /\( here!
                        Handles & in {string}. Whitespace in the substitution
                        pairs must be escaped; for a literal backslash search
                        / replace, use \\\\:
                            /C:\\\\/my\ drive\\\\/

:[range]SubstituteWildcard {wildcard}={string} [...] [flags] [count]
                        Change (first in the line, with "g" flag all) matches
                        of wildcard to {string}. Modeled after the Korn
                        shell's "cd {old} {new}" command.
                        Handles & in {string}. Whitespace in the substitution
                        pairs must be escaped; for a literal backslash search
                        / replace, use \\:
                            C:\\=my\ drive

                        For all following commands, the [!] suppresses the
                        error when there are no duplicates.

:[range]PrintDuplicateLinesOf[!] [{pattern}]
:[range]PrintDuplicateLinesOf[!] /{pattern}/
                        Print all occurrences of lines matching {pattern} (or
                        the current line, if omitted).
                        All matching lines are added to the jumplist, so you
                        can use CTRL-O to revisit the locations.

:[range]DeleteDuplicateLinesOf[!] [{pattern}]
:[range]DeleteDuplicateLinesOf[!] /{pattern}/
                        Delete all subsequent occurrences of lines matching
                        {pattern} (with . the current line; if omitted: the
                        last search pattern).

:[range]PrintDuplicateLinesIgnoring[!] [{pattern}]
:[range]PrintDuplicateLinesIgnoring[!] /{pattern}/
                        Print all occurrences of a line whose text (ignoring
                        any text matched by {pattern}) appears multiple times.
                        To ignore empty lines, use a {pattern} of ^$ (strict)
                        or ^\s*$ (lenient).
                        All matching lines are added to the jumplist, so you
                        can use CTRL-O to revisit the locations.

:[range]DeleteDuplicateLinesIgnoring[!] [{pattern}]
:[range]DeleteDuplicateLinesIgnoring[!] /{pattern}/
                        Delete all subsequent occurrences of a line (ignoring
                        any text matched by {pattern}).

:[range]DeleteAllDuplicateLinesIgnoring[!] [{pattern}]
:[range]DeleteAllDuplicateLinesIgnoring[!] /{pattern}/
                        Delete all (including the very first) occurrences of a
                        duplicate line (ignoring any text matched by
                        {pattern}).

:[range]PrintDuplicates[!] [{pattern}]
:[range]PrintDuplicates[!] /{pattern}/
                        Print all matches of {pattern} (if omitted: the last
                        search pattern) that appear multiple times in the
                        current line / [range].
                        To print all duplicate matches of {pattern} within a
                        single line, processing a [range] or the entire
                        buffer, use:
                        :global/^/PrintDuplicates! [{pattern}]
                        :global/^/PrintDuplicates! /{pattern}/
                       The [!] suppresses the error when there are no
                        duplicates in a particular line.

:[range]DeleteDuplicates[!] [{pattern}]
:[range]DeleteDuplicates[!] /{pattern}/
                        Delete all subsequent matches of {pattern} (or the
                        last search pattern, if omitted) except the first.

:[range]DeleteRanges[!] {range} [range] [...] [x]
:[range]YankRanges[!] {range} [range] [...] [x]
                        Within the entire buffer / passed [range], delete /
                        yank all lines that fall within {range} (with [!]: do
                        not fall within) into register [x] (or the unnamed
                        register). When multiple [range]s are given, each line
                        is yanked only once (in ascending order).

:[range]PrintRanges[!] {range} [range] [...]
                        Within the entire buffer / passed [range], print all
                        lines that fall within {range} (with [!]: do not fall
                        within).
                        Each start of a block is added to the jumplist, so
                        you can use CTRL-O to revisit the locations.

:[range]RangeDo[!] {range} [range] [...] {command}
                        Within the entire buffer / passed [range], execute the
                        Ex command {cmd} on each line that falls within
                        {range} (with [!]: does not fall within). When
                        multiple [range]s are given, each line is processed
                        only once (in ascending order).
                        Note: {command} must be separated by whitespace from
                        the preceding ranges!
                        Works like
                            :global/apples/,/peaches/ {cmd}
                       but:
                        - processes each line only once when the ranges are
                          overlapping
                        - supports multiple ranges (which are joined) and
                          inversion with [!]
 
install details
INSTALLATION
This script is 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 PatternsOnText*.vmb.gz
    :so %
To uninstall, use the :RmVimball command.

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

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
PatternsOnText-1.35.vmb.gz 1.35 2014-04-25 7.0 Ingo Karkat - ENH: Allow to pass multiple ranges to the :*Ranges commands.
- FIX: The *Ranges commands only handled /{pattern}/,... ranges, not line numbers or marks. Only use :global for patterns; for everything else, there's only a single range, so we can just prepend it to :call directly.
- Don't clobber the search history with the :*Ranges commands (through using :global).
- Add :RangeDo command.
- ENH: Also support passing the search pattern inline with :SubstituteInSearch/{search}/{pattern}/{string}/[flags] instead of using the last search pattern.
PatternsOnText-1.30.vmb.gz 1.30 2014-03-13 7.0 Ingo Karkat - Add :DeleteAllDuplicateLinesIgnoring command.
- CHG: The :[Print|Delete]DuplicateLines[ Of|Ignoring] commands do not automatically ignore empty lines any more. Use a {pattern} like ^$ to ignore them explicitly.
- FIX: Wrong use of ingo#escape#Unescape().
- ENH: Enable use of \= sub-replace-expression in :SubstituteInSearch through emulation, as the command implementation itself uses \=, Vim doesn't allow recursive use inside it. This has been inspired by http://stackoverflow.com/questions/21588649/increment-numbers-between-delimiters-in-vim
- Add :DeleteRanges, :YankRanges, :PrintRanges commands.
- Handle \r, \n, \t, \b in {string}, too. *** You need to update to ingo-library (vimscript #4433) version 1.017! ***
PatternsOnText-1.20.vmb.gz 1.20 2014-02-18 7.0 Ingo Karkat - ENH: Add :SubstituteWildcard {wildcard}={string} and :SubstituteMultiple /{pattern}/{string}/ commands. *** You need to update to ingo-library (vimscript #4433) version 1.016! ***
PatternsOnText-1.12.vmb.gz 1.12 2013-11-21 7.0 Ingo Karkat - FIX: Use of \v and \V magicness atoms in the pattern for :DeleteExcept and :SubstituteExcept cause errors like "E54: Unmatched (" and "E486: Pattern not found". Revert to the default 'magic' mode after each pattern insertion to the workhorse regular expression.
- FIX: Abort :DeleteExcept / :SubstituteExcept commands when the pattern contains the set start / end match patterns \zs / \ze, as these interfere with the internal implemenation.
- Minor: Make substitute() and matchlist() robust against 'ignorecase'.
PatternsOnText-1.11.vmb.gz 1.11 2013-06-13 7.0 Ingo Karkat - FIX: Remove -bar from all commands to correctly handle patterns like foo\|bar without escaping as foo\\|bar.
- :SubstituteSelected now positions the cursor on the line where the last selected replacement happened, to behave like :substitute.
PatternsOnText-1.10.vmb.gz 1.10 2013-06-06 7.0 Ingo Karkat - ENH: :SubstituteSelected handles count before "y" and "n" answers, numeric positions "2,5", and ranges "3-5".
- Also recall previous answers in a bare :SubstituteSelected command.
- The commands that take a {pattern}, i.e. :SubstituteExcept, :DeleteExcept, :SubstituteSelected now consistently set that as the last search pattern.
- Refactoring: Move functions from ingo/cmdargs.vim to ingo/cmdargs/pattern.vim and ingo/cmdargs/substitute.vim. *** You need to update to ingo-library (vimscript #4433) version 1.007! ***
PatternsOnText-1.01.vmb.gz 1.01 2013-05-30 7.0 Ingo Karkat Implement abort on error for :SubstituteExcept, :DeleteExcept, :SubstituteSelected, and :SubstituteInSearch, too.
PatternsOnText-1.00.vmb.gz 1.00 2013-05-29 7.0 Ingo Karkat Initial upload
ip used for rating: 54.81.187.157

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.
   
SourceForge.net Logo