Discussion:
How to represent an empty string in a substitute command
Jean Johner
2010-03-12 14:05:01 UTC
Permalink
Hello,
I would like to add a "c" (c character) at the beginning of a line (or
of a range) for any character already present at column 1. This means
that if "c" is the first character of the line, I want to get "cc". If
it is "a", I want to get "ca".
Using
:s/a*/c/
works because * can be zero so that *a can be an empty string.
Unfortunately, if "a" is the first character of the line, the command
results in c replacing a (not inserting before a).

Is there a way to solve this problem, for example by representing an
empty string in an absolute manner.
I tried /.*/ but it replaces the whole line.
I tried // but it represents the last search string.

Best regards.

Jean Johner
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
François Ingelrest
2010-03-12 14:34:14 UTC
Permalink
Post by Jean Johner
I would like to add a "c" (c character) at the beginning of a line (or
of a range) for any character already present at column 1. This means
that if "c" is the first character of the line, I want to get "cc". If
it is "a", I want to get "ca".
Using
:s/a*/c/
works because * can be zero so that *a can be an empty string.
Unfortunately, if "a" is the first character of the line, the command
results in c replacing a (not inserting before a).
Is there a way to solve this problem, for example by representing an
empty string in an absolute manner.
I tried /.*/ but it replaces the whole line.
I tried // but it represents the last search string.
Not sure I really understood what you need, but here's a try:

%s/^\(.*\)$/c\1/
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
James Beck
2010-03-12 14:35:48 UTC
Permalink
Post by Jean Johner
Hello,
I would like to add a "c" (c character) at the beginning of a line (or
of a range) for any character already present at column 1. This means
that if "c" is the first character of the line, I want to get "cc". If
it is "a", I want to get "ca".
Using
:s/a*/c/
works because * can be zero so that *a can be an empty string.
Unfortunately, if "a" is the first character of the line, the command
results in c replacing a (not inserting before a).
Is there a way to solve this problem, for example by representing an
empty string in an absolute manner.
I tried /.*/ but it replaces the whole line.
I tried // but it represents the last search string.
I'm not exactly sure what you're looking for, but perhaps this does the
trick?

:s/^\(\a\)/c\1/

Search for the start of the line followed by anything that matches a
character. Then substitute that for 'c' followed by whatever character
matched the search.

James
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
Tony Mechelynck
2010-03-12 14:50:01 UTC
Permalink
Post by Jean Johner
Hello,
I would like to add a "c" (c character) at the beginning of a line (or
of a range) for any character already present at column 1. This means
that if "c" is the first character of the line, I want to get "cc". If
it is "a", I want to get "ca".
Using
:s/a*/c/
works because * can be zero so that *a can be an empty string.
Unfortunately, if "a" is the first character of the line, the command
results in c replacing a (not inserting before a).
Is there a way to solve this problem, for example by representing an
empty string in an absolute manner.
I tried /.*/ but it replaces the whole line.
I tried // but it represents the last search string.
Best regards.
Jean Johner
There are various ways to go about this. For instance the following:

:'<,'>s/^\ze\a/c/

which means

: start of ex-command
'<,'> this range is automagically inserted if you hit :
in Visual mode; it means "on all selected lines".
s s[ubstitute]
/ start of "replace what" regex
^ begin of line
\ze "replace what by" ends here, but continue matching
\a alphabetic character
/ start of "replace by what" string
c c
/ end of "replace by", there are no flags

see :help pattern-overview

To replace any character (not only alphabetic), but not empty lines,
replace \a by a dot.

Best regards,
Tony.
--
The human animal differs from the lesser primates in his passion for
lists of "Ten Best".
-- H. Allen Smith
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
Jean Johner
2010-03-12 22:17:48 UTC
Permalink
        :'<,'>s/^\ze\a/c/
Bonjour Antoine,
The absolute way to represent an empty string in a substitute command
I was looking for is simply:
\ze
In effect

[range]s/\ze/c

does the job (including empty lines)
No need to add ^ which is implicit and . which restrict the substitute
to non empty lines

The solution of François
[range]s/^\(.*\)$/c\1/
also works. It can be simplified to
[range]s/\(.*\)/c\1/
as ^ and $ are implicit in .*

Best regards

Jean Johner
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
Tony Mechelynck
2010-03-12 23:03:52 UTC
Permalink
Post by Jean Johner
Post by Tony Mechelynck
:'<,'>s/^\ze\a/c/
Bonjour Antoine,
The absolute way to represent an empty string in a substitute command
\ze
In effect
[range]s/\ze/c
does the job (including empty lines)
No need to add ^ which is implicit and . which restrict the substitute
to non empty lines
The solution of François
[range]s/^\(.*\)$/c\1/
also works. It can be simplified to
[range]s/\(.*\)/c\1/
as ^ and $ are implicit in .*
Best regards
Jean Johner
To add the letter c at the start of a range of lines, there are
different ways:

1) :12,18s/^/c

(replace the start-of-line by "c"). \ze matches anywhere, so in an
otherwise empty pattern and without the g flag, the first place it can
match is at start-of-line.

2) select the first column blockwise, then

Ic<Esc>

(see :help v_b_I ) to insert c before the block on every line.

If you wanted to match the empty string in the middle of the line, let's
say insert a space if there is none between well-defined patterns "foo"
and "bar", you would use

:s/foo\zs\zebar/ /
or
:s/\(foo\)\(bar\)/\1 \2

with flag g if it were "everywhere in the line".


Best regards,
Tony.
--
"One Saturday afternoon, during the campaign to decide whether or not
there should be a Coastal Commission, I took a helicopter ride from Los
Angeles to San Diego. We passed several state beaches, some crowded
and some virtually empty. They had the same facilities, and in some
cases the crowded and the empty beach were within a quarter mile of
each other. Obviously many beach-goers prefer to be crowded together.
Buying more beaches that people won't go to because they prefer to be
crowded together on one beach is a ridiculous waste of our natural
resources and our taxes."
-- Ronald Reagan
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
Jean Johner
2010-03-13 09:19:22 UTC
Permalink
:s/^/c is even better than s/\ze/c.
Ic<Esc> is good to know.

Thanks

Jean Johner
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
Continue reading on narkive:
Loading...