Thursday 12 June 2014

A beginner's tutorial on Vim Editor-III

Part-III

Continuing from the previous lessons....
Replacement Commands:-
Sometimes it's need to replace a particular word with another text globally or specifically in a file.The terms 'globally' and 'specifically' is understood by the following example:
Suppose a text is written as follows:
Hello guys,this is Saptarshi Nag.
I want to say you "hello,world".
Now one wants to use 'hi' instead of 'hello's in global respect,i.e.,one wants to write
Hi guys,this is Saptarshi Nag.
I want to say you "hi,world".
This is global replacement
Again one may want to replace the only one of the 'hello's by a 'hi'.Suppose:
Hi guys,this is Saptarshi Nag.
I want to say you "hello,world".
Now both are possible through last-line mode.From previous lessons you know how to go into last-line mode.
For global replacement type in last -line method
%s/text/replacement/g (g stands for globally)
So here in the global replacement it would be
%s/hello/hi/g
But for specific replacement it'll be
%s/text/replacement/gc (gc stands for globally with confirmation)
Here for every replacement system wants to know if user wants to replace text.If yes press 'y' else 'n'.You'll see it by using it.
Therfore for the last example you should give
%s/hello/hi/gc
Then the answer to the confirmation of the first replacement will be 'y' then for the second one is 'n'.

No comments:

Post a Comment