Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

vi and vim/insert column of STRING

1,509 views
Skip to first unread message

Harry Putnam

unread,
Jun 2, 1999, 3:00:00 AM6/2/99
to
Wanting to insert a colummn containing a specific string into a a text
file like so:

Original text file:

text line text line text line text line
text line text line text line text line
text line text line text line text line
text line text line text line text line

After insertion:

STRING text line text line text line text line
STRING text line text line text line text line
STRING text line text line text line text line
STRING text line text line text line text line

Or
text line text STRING line text line text line
text line text STRING line text line text line
text line text STRING line text line text line
text line text STRING line text line text line


Insert a column of STRING at any location.
Something like emacs `string-rectangle'

With vim, but also wonder if it can be done with regular vi?


Thomas Köhler

unread,
Jun 2, 1999, 3:00:00 AM6/2/99
to
Harry Putnam <rea...@newsguy.com> wrote:
>Wanting to insert a colummn containing a specific string into a a text
>file like so:
>
>Original text file:
>
>text line text line text line text line
>text line text line text line text line
>text line text line text line text line
>text line text line text line text line
>
>After insertion:
>
>STRING text line text line text line text line
>STRING text line text line text line text line
>STRING text line text line text line text line
>STRING text line text line text line text line

vanilla vi:
:%s/^/STRING /

>Or
>text line text STRING line text line text line
>text line text STRING line text line text line
>text line text STRING line text line text line
>text line text STRING line text line text line

vanilla vi:
:%s/^text line text/& STRING/

Or, if you have different text (and thus want to add something after the
10th character):
:%s/^........../&STRING/

Or, if you are using vim:
:%s/^.\{10}/&STRING/

Or, in all the above cases, if you want to do the search&replace only on
lines 12-14:
:12,14s/.../

Or, if you have marks a and b set:
:'a,'bs/.../

In vim again, use visual mode (hit V, select the line you want to apply
a substitution to and hit : which automatically inserts the marks '< and
'> for which the substitution is to be done).

Then again, if you have

STRING text line
STRING text line
STRING text line

and want to change this to

text STRING line
text STRING line
text STRING line

try visual block mode:
ctrl-v starts visual block mode, move around, hit d (to delete the
text), move around, hit p to paste the whole block. In the above case,
asuming you are on the first line, the following commands will do
(explained)
0 " go to the first column
ctrl-v " start visual block mode
2j " go two lines down
f<space> " go to the first space in the line
d " delete the selected block
f<space> " go to the next space (any other movement command can be used)
p " paste the formerly deleted block

>Insert a column of STRING at any location.
>Something like emacs `string-rectangle'

I don't know what string-rectangle does :)

>With vim, but also wonder if it can be done with regular vi?

Hope this helps in all cases - vim and vanilla vi :-)

CU,
Thomas

--
Thomas Köhler Email: jean...@picard.franken.de
<>< WWW: http://home.pages.de/~jeanluc/
IRC: jeanluc
LCARS --- Linux for Computers on All Real Starships

Peppe

unread,
Jun 3, 1999, 3:00:00 AM6/3/99
to
Thus wrote Harry Putnam <rea...@newsguy.com>

> Insert a column of STRING at any location.
> Something like emacs `string-rectangle'

Seems like Thomas answered this in quite some detail.

Still, in the upcoming vim 5.4 there is some extra commands in visual block
mode. Eg.:

I Insert a string before the selected are (repeated on each line)
s Substitute the highlighted text with the same string on each line
r Replace all highlighted characters by some character

Plus a few others.

Just thought you might be interested.

Peppe
--
Il brutto [reading note]: __/-\__ Preben Guldberg
"See you soon, Idi.. I..di..ots" (o o) c92...@student.dtu.dk
Il buono: "Idiots! -- It is for you." -oOOo (_) oOOo---------------------
--_Il buono, il brutto, il cattivo_ http://www.student.dtu.dk/~c928400/

Harry Putnam

unread,
Jun 4, 1999, 3:00:00 AM6/4/99
to
jean...@picard.franken.de (Thomas Köhler) writes:

> Harry Putnam <rea...@newsguy.com> wrote:
> >Wanting to insert a colummn containing a specific string into a a text

> >After insertion:
> >

> >STRING text line text line text line text line
>

> vanilla vi:
> :%s/^/STRING /
>
> >Or
> >text line text STRING line text line text line
>

> vanilla vi:
> :%s/^text line text/& STRING/
>
> Or, if you have different text (and thus want to add something after the
> 10th character):
> :%s/^........../&STRING/

> Or, if you are using vim:
> :%s/^.\{10}/&STRING/
>

> Hope this helps in all cases - vim and vanilla vi :-)

Yes indeed, but am finding some unexpected behaviour:

Using your examples for both vanilla vi and vim in this (edited)
.mailrc file:

xxxxx DJ-xxxx "Dx Axxx <Dxx...@xxxxx.xxxx.xxx>"
xxxxx Kxx "Kxx Gxxxxxxxxxx <xxxxxxxxxxx.xxxxxxx.xx.xxx-xxxxxxxx.xx>"
xxxxx DJ-xxxx "Dx Axxx <xxxx...@xxxxxx.xxx>"
xxxxx HxxxxIII "Hxxxx Pxxxxx <xxx...@xxxxxxx.xxx>"
xxxxx HxxxxIV "Hxxxx IV <3x...@xxxxxxxxx.xxx>"
xxxxx Mxx "Fxxxxx710 <Fxxx...@xxx.xxx>"
xxxxx Cxxxx "C.T.Nxxxxxxx <xx...@xxxx.xxx>"
xxxxx Bxxx "Bxxxxxx <xxxx...@xxx.xxx>"
xxxxx xxxxxx "xxxxx-xx...@xx-x.xxxxx.xx.xx"
xxxxx xxx "Hxxxx Pxxxxx <xxx...@xxxxxxxx.xxx.xxx>"
xxxxx Jxxx S "Jxxx <xxxxx...@xxx.xxx>"
xxxxx xxxx "xxx...@xxxxxxx.xxxxxx.xxx"
xxxxx xxxxxxx "xxx...@xxxxxxxx.xxx.xxx"
xxxxx xxxxxxxxxx "xxx...@xxxx.xxx"
xxxxx Gxxxxx "Gxxxxx <xxx...@xxx.xxx>"
xxxxx Bxxxxx "Bxxxxx <xxx...@xxxxx.xxx>"

Using the command: %s/................/& / (or the vim equiv) To add
two spaces just before the first (") quote mark, produces:

xxxxx DJ-xxxx " Dx Axxx <Dxx...@xxxxx.xxxx.xxx>"
xxxxx Kxx "Kxx Gxxxxxxxxxx <xxxxxxxxxxx.xxxxxxx.xx.xxx-xxxxxxxx.xx>"
xxxxx DJ-xxxx " Dx Axxx <xxxx...@xxxxxx.xxx>"
xxxxx HxxxxIII "Hxxxx Pxxxxx <xxx...@xxxxxxx.xxx>"
xxxxx HxxxxIV " Hxxxx IV <3x...@xxxxxxxxx.xxx>"
xxxxx Mxx "Fxxx xx710 <Fxxx...@xxx.xxx>"
xxxxx Cxxxx "C.T.Nxxxxxxx <xx...@xxxx.xxx>"
xxxxx Bxxx "Bxxxxxx <xxxx...@xxx.xxx>"
xxxxx xxxxxx "xxxxx-xx...@xx-x.xxxxx.xx.xx"
xxxxx xxx "Hxxxx Pxxxxx <xxx...@xxxxxxxx.xxx.xxx>"
xxxxx Jxxx S "Jxxx <xxxxx...@xxx.xxx>"
xxxxx xxxx "xxx...@xxxxxxx.xxxxxx.xxx"
xxxxx xxxxxxx "xxx...@xxxxxxxx.xxx.xxx"
xxxxx xxxxxxxxxx "xxx...@xxxx.xxx"
xxxxx Gxxxxx "Gxxxxx <xxx...@xxx.xxx>"
xxxxx Bxxxxx "Bxxxxx <xxx...@xxxxx.xxx>"

Instead of the desired effect of a uniform insertion of two spaces.
What is happening here?


c92...@student.dtu.dk (Peppe) writes:

>
> Seems like Thomas answered this in quite some detail.
>
> Still, in the upcoming vim 5.4 there is some extra commands in visual block
> mode. Eg.:
>
> I Insert a string before the selected are (repeated on each line)
> s Substitute the highlighted text with the same string on each line
> r Replace all highlighted characters by some character
>
> Plus a few others.
>
> Just thought you might be interested.

Yes, these look usefull... thanks

Thomas Köhler

unread,
Jun 5, 1999, 3:00:00 AM6/5/99
to

Huh? "s/................/& /" on this region worked for me. Perhaps
there were some tabs in there? These would kill the expected behaviour.
Just replace all tabs with the appropriate number of spaces (in vim, do
this: ":set expandtab" followed by ":retab", vanilla vi doesn't have the
retab command). Then try again. Does it work now?

Richard Slobod

unread,
Jun 5, 1999, 3:00:00 AM6/5/99
to
Harry Putnam <rea...@newsguy.com> wrote:
>jean...@picard.franken.de (Thomas Köhler) writes:

>xxxxx DJ-xxxx "Dx Axxx <Dxx...@xxxxx.xxxx.xxx>"
>xxxxx Kxx "Kxx Gxxxxxxxxxx <xxxxxxxxxxx.xxxxxxx.xx.xxx-xxxxxxxx.xx>"
>xxxxx DJ-xxxx "Dx Axxx <xxxx...@xxxxxx.xxx>"
>xxxxx HxxxxIII "Hxxxx Pxxxxx <xxx...@xxxxxxx.xxx>"

[rest of example snipped]

>Using the command: %s/................/& / (or the vim equiv) To add
>two spaces just before the first (") quote mark, produces:
>
>xxxxx DJ-xxxx " Dx Axxx <Dxx...@xxxxx.xxxx.xxx>"
>xxxxx Kxx "Kxx Gxxxxxxxxxx <xxxxxxxxxxx.xxxxxxx.xx.xxx-xxxxxxxx.xx>"
>xxxxx DJ-xxxx " Dx Axxx <xxxx...@xxxxxx.xxx>"
>xxxxx HxxxxIII "Hxxxx Pxxxxx <xxx...@xxxxxxx.xxx>"

[rest of results snipped]

>Instead of the desired effect of a uniform insertion of two spaces.
>What is happening here?

It looks like the quote is not the tenth character on every line.
Perhaps you have some tabs in there?

Walter Prager

unread,
Jun 7, 1999, 3:00:00 AM6/7/99
to
Harry Putnam wrote:
>
> Using the command: %s/................/& / (or the vim equiv) To add
> two spaces just before the first (") quote mark, produces:

snip

> Instead of the desired effect of a uniform insertion of two spaces.
> What is happening here?
>

Try "%s/^................/& /"

The command you used will add a space after every 16 characters. I believe you
want this only for the start of line.
--
Walter Prager, S/W Designer | ||| "I don't have all the answers. In
TEL: (613) 599-3600 ext 6460 | |||||||| life, to be honest, I've failed as
EMAIL: wpr...@newbridge.com | ||||\||| much as I've succeeded. But I love
| ||||\\|| my wife, I love my life, and I wish
NEWBRIDGE NETWORKS CORP | ||||\\\| you *my* kind of success."
Kanata, Ontario, Canada | ||| Dicky Fox

Geoff Clare

unread,
Jun 10, 1999, 3:00:00 AM6/10/99
to
Walter Prager <wpr...@newbridge.com> writes:

>> Using the command: %s/................/& / (or the vim equiv)

>Try "%s/^................/& /"

>The command you used will add a space after every 16 characters. I believe you
>want this only for the start of line.

Did you try it with and without the "^"?

No, I thought not. When there is no "g" after the trailing "/", only
the first match on the line is substituted. Anchoring with "^" makes
no difference in that case.
--
Geoff Clare g...@unisoft.com
UniSoft Limited, London, England. g...@root.co.uk

0 new messages