Link to home
Start Free TrialLog in
Avatar of codenamecharlie
codenamecharlie

asked on

Vi Editor: how to replace/delete ?

(1) Using VIM (Vi improved), how to delete "ABB_Hello" and "ABB_World" with just one command?

(2) How to remove those spaces in the beginning of Line 3 and 4 with just one command?


ABB_Hello Line 1;
ABB_World Line 2;
           Line 3;
           Line 4;
Avatar of Narendra Kumar S S
Narendra Kumar S S
Flag of India image

(1) With a single command you cannot delete both the words! You will have to use delete word command twice.
(2) Goto the first column on each line and (in escape mode) enter "dw" (without quotes), the spaces will get deleted.
     If you know the number of spaces in the begining of those lines and if they are same, then you can remove them with a single command like this(editor prompt): Goto the ":" prompt inside vim. And give the following command there :g/           /s///g
This will work only if the number of spaces is exactly same!
Avatar of codenamecharlie
codenamecharlie

ASKER


VI is not smart enough to delete those words with just one single command? Disappointing.....

I thought Vi is powerful enough to do this... ?
Can you please show me any other editor which can do this!?
SOLUTION
Avatar of scn
scn

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I forgot the upper case letters:
(1)
1,2s/ABB_[a-zA-Z]*//
To delete all occurrences of ABB_Hello:

:%s/ABB_Hello//g

To delete all occurrences of ABB_World:

:%s/ABB_World//g

To delete the spaces, you have to know how many spaces there are:

:%s/^     //g
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
OK, scn and ahoffmann's solutions work, other guys either misunderstand my question or just gimme wrong answers.

BTW, ahoffmann, may you pls explain to me the meaning of the command:

 :g/^ABB_[^ ]*/s///   ???

does 'g' mean "global" ???
what is [^ ]* ? is there a space after ^   ???
also, what is s///     ???   looks weird


Many thx!
Hi codenamecharlie,
The solution I gave for the 2nd Q works fine! Also, it is simple to understand than others!:-)

But I accept that the answers provided by scn and ahoffmann are better than the solution I gave:-))
g is different from :g
:g means "process hole file"
s/// used in :g means that the matched pattern is replaxed by nothing:
  s//  -- matches pattern
    // -- replace by nothing
or more syntacticaly: s/pattern/replace/