Solved

Vi Editor: how to replace/delete ?

Posted on 2003-12-09
10
1,600 Views
Last Modified: 2013-12-13
(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;
0
Comment
Question by:codenamecharlie
  • 3
  • 2
  • 2
  • +2
10 Comments
 
LVL 8

Expert Comment

by:ssnkumar
Comment Utility
(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!
0
 

Author Comment

by:codenamecharlie
Comment Utility

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

I thought Vi is powerful enough to do this... ?
0
 
LVL 8

Expert Comment

by:ssnkumar
Comment Utility
Can you please show me any other editor which can do this!?
0
 
LVL 3

Assisted Solution

by:scn
scn earned 20 total points
Comment Utility

(1)
1,2s/ABB_[a-z]*//

(2)
3,4s/^[ ]*//
(there is a space inside the brackets)
0
 
LVL 3

Expert Comment

by:scn
Comment Utility
I forgot the upper case letters:
(1)
1,2s/ABB_[a-zA-Z]*//
0
Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

 
LVL 6

Expert Comment

by:durindil
Comment Utility
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
0
 
LVL 51

Accepted Solution

by:
ahoffmann earned 30 total points
Comment Utility
:g/^ABB_[^ ]*/s///
:3,4 s/^ *//

(second one already suggested by scn:)
0
 

Author Comment

by:codenamecharlie
Comment Utility
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!
0
 
LVL 8

Expert Comment

by:ssnkumar
Comment Utility
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:-))
0
 
LVL 51

Expert Comment

by:ahoffmann
Comment Utility
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/
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

rdate is a Linux command and the network time protocol for immediate date and time setup from another machine. The clocks are synchronized by entering rdate with the -s switch (command without switch just checks the time but does not set anything). …
I am a long time windows user and for me it is normal to have spaces in directory and file names. Changing to Linux I found myself frustrated when I moved my windows data over to my new Linux computer. The problem occurs when at the command line.…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now