0

In a block of test like this

cron.allow
cron.deny

I want to substitute cron with at,only in the range selcted by visual mode(ctrl+v) So i press ctrl+v : and appear the ! prompt ok I do

sed s:cron:at:gc

but i only see this error

:'<,'>! sed s:cron:at:gc
shell end return code 1
7 lines filteerd

and this after return

sed: expression -e #1, caracther 12: option `s' unknown

Why?

3 Answers 3

3

Once in visual block mode with your lines selected try this:

:'<,'>s/cron/at/g

Vim has search and replace capability without the need to call an external command.

2

From the command line, observe that this succeeds:

$ echo cron | sed s:cron:at:
at

But this doesn't:

$ echo cron | sed s:cron:at:c
sed: -e expression #1, char 11: unknown option to `s'

c is not a sed option. The following, though, will work:

:'<,'>! sed s:cron:at:g

But, of course,there is no need to use sed to do such simple substitutions in vim. Furthermore, if you want c (confirm), use vim's built-in substitution command:

:'<,'>s/cron/at/gc
1

It seems you don't need to use sed or substitute at all. Just use the change (c) command. From the documentation:

Visual-block change                       *v_b_c*
All selected text in the block will be replaced by the same text string.  When
using "c" the selected text is deleted and Insert mode started.  You can then
enter text (without a line break).  When you hit <Esc>, the same string is
inserted in all previously selected lines.

The key here is, "When you hit <Esc>, the same string is inserted in all previously selected lines." So, all you have to do is block select the cron text, press c, type at, exit insert mode and you will get your desired result of replacing each cron in the block with at.

If you really want to run a substitution command on a block, I would cut and paste the block to some blank lines, run your substitution on those lines, and then cut and paste the resulting block back to its original location.

The problem with the command you have in your question is you are trying to use the external sed, and the s command in sed doesn't accept a c flag.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .