Operating System - HP-UX
1847123 Members
5495 Online
110263 Solutions
New Discussion

Re: repeat a same command-set in vi

 
SOLVED
Go to solution
discoverer
Frequent Advisor

repeat a same command-set in vi

Hi friends,

For example,
In vi, I need to insert a same word before each word.
As we know, we can insert the word before the first word, then ESC -> w -> . -> w -> . -> .....

Is there any simple ways to repeat these ' w -> . ' at one time?

Thanks in advance !

/Listener
Listen, then discover, then succeed!
8 REPLIES 8
Michael Tully
Honored Contributor

Re: repeat a same command-set in vi

Hi,

Once your do an insert of a word, or any string, you can repeat the function by going to the next place where you wish to do it again and press . This is like the repeat key.

-Michael
Anyone for a Mutiny ?
discoverer
Frequent Advisor

Re: repeat a same command-set in vi

Hi Tully,

What I mean is to repeat a 'command-set', not (use . to repeat) a insert/append/delete/... command.

Such as, is there something like " 10(w.) " to finish my above requirement, just insert the same word before each following words for 10 times.

Sorry to assign a low point which I didn't assign before , but you just repeat what I have mentioned.
:-(

/Discoverer
Listen, then discover, then succeed!
Michael Tully
Honored Contributor
Solution

Re: repeat a same command-set in vi

Hi,

Sorry I didn't understand your question properly, must be as I'm going on holidays....

Okay, if you have a string called word1 and wanted to replace each occurance of word1 with word23 try
this in 'vi'

:1,$s/word1/word23/g (global all occurance)
:1,$s/word1/word23/ (one on each line)

If you want to insert a string at the beginning
of each line:
:1,$s/^/string//

If this is not what you after exactly have a look at the link, it provides some good working examples of 'vi'

http://www.epcc.ed.ac.uk/tracs/vi.html#intermed4
or
http://www.networkcomputing.com/unixworld/tutorial/009/009.html

HTH
-Michael
Anyone for a Mutiny ?
Deepak Extross
Honored Contributor

Re: repeat a same command-set in vi

I'm not quite sure if I understand what you're after, but does a "10." help?
This will repeat the last command 10 times.
Steven Sim Kok Leong
Honored Contributor

Re: repeat a same command-set in vi

Hi,

You want the the same word to be repeated before every word.

Does this satisfy your requirement:

:1,$s/ / insertedword /g
:1,$s/^/^insertedword /g

Example:

Before inserting:
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.

After inserting:
insertedword The insertedword quick insertedword brown insertedword fox insertedword jumps insertedword over insertedword the insertedword lazy insertedword dog.
insertedword The insertedword quick insertedword brown insertedword fox insertedword jumps insertedword over insertedword the insertedword lazy insertedword dog.

Hope this helps. Regards.

Steven Sim Kok Leong
Brainbench MVP for Unix Admin
http://www.brainbench.com
Wodisch
Honored Contributor

Re: repeat a same command-set in vi

Hello discoverer,

what you want is called a "macro" in vi.
It works like this:
- you put the keys to be "executed" into a buffer
- you tell vi to execute that buffer with "@".

E.g:
Insert a line like
cwbye"mdd
where the stuff in angle brackets is actually keys (your hold down "Ctrl", then press "V", release both of them, and then press the "Esc" key, twice, then the double quote, an "m" and twice a "d").
Now you have the characters "cwbye" in buffer "m".
Move your cursor to some word you would like to change to "bye" and press "@m".
Te repeat the last macro use "@@".

HTH,
Wodisc
Jeff Machols
Esteemed Contributor

Re: repeat a same command-set in vi

you could always map a key.

:map t wwcw

this would map the character t to two word commands and a change word command. This can be done with any command.
to get return do , m. to get esc do , esc
discoverer
Frequent Advisor

Re: repeat a same command-set in vi

HeHe....

I love this forum!

Though Tully (& Steven)'s method is not so universal, but I'd like to assigned 10 pts for his total help.

Jeff's is just as what I had though out.
Wodisc's is the same but more expert.

And thanks for Deepak's attendant.

/discoverer
Listen, then discover, then succeed!