VIM Editor-Part III

Tagged: 

This topic was published by and viewed 824 times since "". The last page revision was "".

Viewing 1 post (of 1 total)
  • Author
    Posts
  • Saptarshi Nag
    Saptarshi Nag
    Participant
    • Topics - 6
    • @saptarshi

    Continuing from previous lessons....

    Replacement Commands:-

    Sometimes it's need to replace a particular word with another text in a file.It can be made "globally" or "specifically",the respective terms are better understood in the following example:

    Suppose there is a text:

    Hello guys,this is Saptarshi Nag

    I want to say you "hello world"

    Now one wants to replace "hello" with "hi" globally,that is it will be-

    Hi guys,this is Saptarshi Nag

    I want to say you "hi world"

    Again one wants to replace the only one "hello" with "hi" like this

    Hi guys,this is Saptarshi Nag

    I want to say you "hello world"

    Now both are possible through last line mode.

    For global replacement type the following in last-line mode:

    %s/text/replacement/g [g stands for global]

    So in the first example it will be:

    %s/hello/hi/g

    But for specific replacement,we have to use the following:

    %s/text/replacement/gc [gc stands for global with confirmation]

    Here for every replacement system wants to know if user wants to replace text or not.If yes,press 'y' else 'n'.

    Therefore the second example can be done with:

    %s/hello/hi/gc

    Now apply it yourself

    Check out DCJTech's official Facebook page - http://dcjtech.info/members/saptarshi/

Viewing 1 post (of 1 total)