prompt-toolkit / pyvim

Pure Python Vim clone.
BSD 3-Clause "New" or "Revised" License
2.52k stars 161 forks source link

Substitute not working #55

Open lunemec opened 9 years ago

lunemec commented 9 years ago
4.2 Substitute                      *:substitute*
                            *:s* *:su*
:[range]s[ubstitute]/{pattern}/{string}/[flags] [count]
            For each line in [range] replace a match of {pattern}
            with {string}.
            For the {pattern} see |pattern|.
            {string} can be a literal string, or something
            special; see |sub-replace-special|.
            When [range] and [count] are omitted, replace in the
            current line only.
            When [count] is given, replace in [count] lines,
            starting with the last line in [range].  When [range]
            is omitted start in the current line.
            Also see |cmdline-ranges|.
            See |:s_flags| for [flags].

This is functionality for search and replace %s/search/replace/g

                            *:s_flags*
The flags that you can use for the substitute commands:

[&] Must be the first one: Keep the flags from the previous substitute
    command.  Examples: >
        :&&
        :s/this/that/&
<   Note that `:s` and `:&` don't keep the flags.
    {not in Vi}

[c] Confirm each substitution.  Vim highlights the matching string (with
    |hl-IncSearch|).  You can type:             *:s_c*
        'y'     to substitute this match
        'l'     to substitute this match and then quit ("last")
        'n'     to skip this match
        <Esc>   to quit substituting
        'a'     to substitute this and all remaining matches {not in Vi}
        'q'     to quit substituting {not in Vi}
        CTRL-E  to scroll the screen up {not in Vi, not available when
            compiled without the |+insert_expand| feature}
        CTRL-Y  to scroll the screen down {not in Vi, not available when
            compiled without the |+insert_expand| feature}
    If the 'edcompatible' option is on, Vim remembers the [c] flag and
    toggles it each time you use it, but resets it when you give a new
    search pattern.
    {not in Vi: highlighting of the match, other responses than 'y' or 'n'}

[e]     When the search pattern fails, do not issue an error message and, in
    particular, continue in maps as if no error occurred.  This is most
    useful to prevent the "No match" error from breaking a mapping.  Vim
    does not suppress the following error messages, however:
        Regular expressions can't be delimited by letters
        \ should be followed by /, ? or &
        No previous substitute regular expression
        Trailing characters
        Interrupted
    {not in Vi}

[g] Replace all occurrences in the line.  Without this argument,
    replacement occurs only for the first occurrence in each line.  If
    the 'edcompatible' option is on, Vim remembers this flag and toggles
    it each time you use it, but resets it when you give a new search
    pattern.  If the 'gdefault' option is on, this flag is on by default
    and the [g] argument switches it off.

[i] Ignore case for the pattern.  The 'ignorecase' and 'smartcase' options
    are not used.
    {not in Vi}

[I] Don't ignore case for the pattern.  The 'ignorecase' and 'smartcase'
    options are not used.
    {not in Vi}

[n] Report the number of matches, do not actually substitute.  The [c]
    flag is ignored.  The matches are reported as if 'report' is zero.
    Useful to |count-items|.
    If \= |sub-replace-expression| is used, the expression will be
    evaluated in the |sandbox| at every match.

[p] Print the line containing the last substitute.

[#] Like [p] and prepend the line number.

[l] Like [p] but print the text like |:list|.

[r] Only useful in combination with `:&` or `:s` without arguments.  `:&r`
    works the same way as `:~`:  When the search pattern is empty, use the
    previously used search pattern instead of the search pattern from the
    last substitute or `:global`.  If the last command that did a search
    was a substitute or `:global`, there is no effect.  If the last
    command was a search command such as "/", use the pattern from that
    command.
    For `:s` with an argument this already happens: >
        :s/blue/red/
        /green
        :s//red/   or  :~   or  :&r
<   The last commands will replace "green" with "red". >
        :s/blue/red/
        /green
        :&
<   The last command will replace "blue" with "red".
    {not in Vi}

Note that there is no flag to change the "magicness" of the pattern.  A
different command is used instead, or you can use |/\v| and friends.  The
reason is that the flags can only be found by skipping the pattern, and in
order to skip the pattern the "magicness" must be known.  Catch 22!

If the {pattern} for the substitute command is empty, the command uses the
pattern from the last substitute or `:global` command.  If there is none, but
there is a previous search pattern, that one is used.  With the [r] flag, the
command uses the pattern from the last substitute, `:global`, or search
command.

If the {string} is omitted the substitute is done as if it's empty.  Thus the
matched pattern is deleted.  The separator after {pattern} can also be left
out then.  Example: >
    :%s/TESTING
This deletes "TESTING" from all lines, but only one per line.

For compatibility with Vi these two exceptions are allowed:
"\/{string}/" and "\?{string}?" do the same as "//{string}/r".
"\&{string}&" does the same as "//{string}/".
                            *E146*
Instead of the '/' which surrounds the pattern and replacement string, you
can use any other single-byte character, but not an alphanumeric character,
'\', '"' or '|'.  This is useful if you want to include a '/' in the search
pattern or replacement string.  Example: >
    :s+/+//+

For the definition of a pattern, see |pattern|.  In Visual block mode, use
|/\%V| in the pattern to have the substitute work in the block only.
Otherwise it works on whole lines anyway.

                    *sub-replace-special* *:s\=*
When the {string} starts with "\=" it is evaluated as an expression, see
|sub-replace-expression|.  You can use that for complex replacement or special
characters.

Otherwise these characters in {string} have a special meaning:
                                *:s%*
When {string} is equal to "%" and '/' is included with the 'cpoptions' option,
then the {string} of the previous substitute command is used, see |cpo-/|

magic   nomagic   action    ~
  &   \&      replaced with the whole matched pattern        *s/\&*
 \&    &      replaced with &
      \0      replaced with the whole matched pattern      *\0* *s/\0*
      \1      replaced with the matched pattern in the first
          pair of ()                         *s/\1*
      \2      replaced with the matched pattern in the second
          pair of ()                         *s/\2*
      ..      ..                             *s/\3*
      \9      replaced with the matched pattern in the ninth
          pair of ()                         *s/\9*
  ~   \~      replaced with the {string} of the previous
          substitute                         *s~*
 \~    ~      replaced with ~                    *s/\~*
      \u      next character made uppercase              *s/\u*
      \U      following characters made uppercase, until \E      *s/\U*
      \l      next character made lowercase              *s/\l*
      \L      following characters made lowercase, until \E      *s/\L*
      \e      end of \u, \U, \l and \L (NOTE: not <Esc>!)        *s/\e*
      \E      end of \u, \U, \l and \L               *s/\E*
      <CR>    split line in two at this point
          (Type the <CR> as CTRL-V <Enter>)          *s<CR>*
      \r      idem                           *s/\r*
      \<CR>   insert a carriage-return (CTRL-M)
          (Type the <CR> as CTRL-V <Enter>)          *s/\<CR>*
      \n      insert a <NL> (<NUL> in the file)
          (does NOT break the line)              *s/\n*
      \b      insert a <BS>                      *s/\b*
      \t      insert a <Tab>                     *s/\t*
      \\      insert a single backslash              *s/\\*
      \x      where x is any character not mentioned above:
          Reserved for future expansion

The special meaning is also used inside the third argument {sub} of
the |substitute()| function with the following exceptions:
  - A % inserts a percent literally without regard to 'cpoptions'.
  - magic is always set without regard to 'magic'.
  - A ~ inserts a tilde literally.
  - <CR> and \r inserts a carriage-return (CTRL-M).
  - \<CR> does not have a special meaning. it's just one of \x.

Examples: >
  :s/a\|b/xxx\0xxx/g         modifies "a b"      to "xxxaxxx xxxbxxx"
  :s/\([abc]\)\([efg]\)/\2\1/g   modifies "af fa bg" to "fa fa gb"
  :s/abcde/abc^Mde/      modifies "abcde"    to "abc", "de" (two lines)
  :s/$/\^M/          modifies "abcde"    to "abcde^M"
  :s/\w\+/\u\0/g         modifies "bla bla"  to "Bla Bla"
  :s/\w\+/\L\u/g         modifies "BLA bla"  to "Bla Bla"

Note: "\L\u" can be used to capitalize the first letter of a word.  This is
not compatible with Vi and older versions of Vim, where the "\u" would cancel
out the "\L". Same for "\U\l".

Note: In previous versions CTRL-V was handled in a special way.  Since this is
not Vi compatible, this was removed.  Use a backslash instead.

command     text    result ~
:s/aa/a^Ma/ aa  a<line-break>a
:s/aa/a\^Ma/    aa  a^Ma
:s/aa/a\\^Ma/   aa  a\<line-break>a

(you need to type CTRL-V <CR> to get a ^M here)

The numbering of "\1", "\2" etc. is done based on which "\(" comes first in
the pattern (going left to right).  When a parentheses group matches several
times, the last one will be used for "\1", "\2", etc.  Example: >
  :s/\(\(a[a-d] \)*\)/\2/      modifies "aa ab x" to "ab x"

When using parentheses in combination with '|', like in \([ab]\)\|\([cd]\),
either the first or second pattern in parentheses did not match, so either
\1 or \2 is empty.  Example: >
  :s/\([ab]\)\|\([cd]\)/\1x/g   modifies "a b c d"  to "ax bx x x"
<

Substitute with an expression           *sub-replace-expression*
                        *sub-replace-\=* *s/\=*
When the substitute string starts with "\=" the remainder is interpreted as an
expression.  This does not work recursively: a |substitute()| function inside
the expression cannot use "\=" for the substitute string.

The special meaning for characters as mentioned at |sub-replace-special| does
not apply except for "<CR>".  A <NL> character is used as a line break, you
can get one with a double-quote string: "\n".  Prepend a backslash to get a
real <NL> character (which will be a NUL in the file).

The "\=" notation can also be used inside the third argument {sub} of
|substitute()| function.  In this case, the special meaning for characters as
mentioned at |sub-replace-special| does not apply at all. Especially, <CR> and
<NL> are interpreted not as a line break but as a carriage-return and a
new-line respectively.

When the result is a |List| then the items are joined with separating line
breaks.  Thus each item becomes a line, except that they can contain line
breaks themselves.

The whole matched text can be accessed with "submatch(0)".  The text matched
with the first pair of () with "submatch(1)".  Likewise for further
sub-matches in ().

Be careful: The separation character must not appear in the expression!
Consider using a character like "@" or ":".  There is no problem if the result
of the expression contains the separation character.

Examples: >
    :s@\n@\="\r" . expand("$HOME") . "\r"@
This replaces an end-of-line with a new line containing the value of $HOME. >

    s/E/\="\<Char-0x20ac>"/g
This replaces each 'E' character with a euro sign.  Read more in |<Char->|.
lunemec commented 9 years ago

Ok, this is not exactly search and replace, this behaves differently:

4.3 Search and replace                  *search-replace*

                            *:pro* *:promptfind*
:promptf[ind] [string]
            Put up a Search dialog.  When [string] is given, it is
            used as the initial search string.
            {only for Win32, Motif and GTK GUI}

                        *:promptr* *:promptrepl*
:promptr[epl] [string]
            Put up a Search/Replace dialog.  When [string] is
            given, it is used as the initial search string.
            {only for Win32, Motif and GTK GUI}

But everyone uses substitute :)