[wellylug] vi(m) question.

Enkidu enkidu at cliffp.com
Sat May 8 17:47:15 NZST 2004

Thanks for that Sam. I know the basics but the details always escape

I'm still confused (sorry!) about the "\+\" bit.

Starting from the left most delimter you first have a "^" which says
"start of string". Then you escape the "(", followed by a space,
followed by an escaped "+" which means one or more spaces required at
the start of the string. Then you escape the ")" and then the second

So the whole string says "replace any spaces at the start of the
string with "nextext" followed by the spaces that were originally at
the start of the string (with the rest of the string following
untouched. Have I got it? That was what I was after!



:%s/^\( \+\)/newtext\1/

On Sat, 8 May 2004 12:16:53 +1200, you wrote:

>The first thing to note is that regular expressions in Vim seem to need
>more backslashes than pretty much everything else.  I'm not sure if this
>is just a difference between Posix and Perl expressions, or if it's
>something different again.
>The parentheses mark stuff you want to keep in the new string.  The
>first set of parens can be retrieved with \1, the second with \2, etc
>Adding a + after a character means 'one or more of the previous
>character'.  So 'a+' would match 'a', 'aaaa', 'aaaaaaaaaaa', etc.
>Adding a * means '0 or more of the character', so 'a*' matches 'aaaaa',
>'aaaaaaaaa', and ''.
>Other special characters are:
>^ - the beginning of a string
>$ - the end of a string
>. - any character at all
>You can use square brackets to match any one of a set of characters.
>For example, '[aeiou]' would match any lowercase vowel.
>If you wanted to remove every lowercase vowel from a file, you would do
>something like:
>The g at the end allows the expression to match more than once on any
>given line.  The following:
>Would only match the first on each line.
>To match everything *except* lowercase vowels, you add a ^ as the first
>character inside the square brackets.  When it's given as the first
>character in the [] set, it doesn't mean the beginning of the line as
>mentioned above.  The following:
>Would remove everything that is not a lowercase vowel from the file.
>There are heaps of other far more complex things you can do.  For
>example, the following expression will match any valid date in
>mm/dd/yyyy or dd/mm/yyyy format, including testing for leap years:
>(stolen from http://regexlib.com/REDetails.aspx?regexp_id=279)
>The regex(7) manpage has a lot more information, but is very hard to
>understand. :)
>-----Original Message-----
>From: wellylug-admin at lists.naos.co.nz
>[mailto:wellylug-admin at lists.naos.co.nz] On Behalf Of Enkidu
>Sent: Saturday, 8 May 2004 11:22 a.m.
>To: wellylug at lists.naos.co.nz
>Subject: Re: [wellylug] vi(m) question.
>On Fri, 7 May 2004 23:57:20 +1200, you wrote:
>I guess that "\1" in the second half means "everything in the ( ) from
>the first part". 
>The first bit means "at the start of the string remember everything
>including the white space but what does "\+\" mean?

More information about the wellylug mailing list