Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

vi - converting multiple spaces into a single space

Status
Not open for further replies.

nappaji

Programmer
Mar 21, 2001
76
US
Hello,

How do I change multiple spaces into a single space on all lines in a file using vi.

I have done this before but dont seem to remember. I open the file using vi, and used to type in something like this

:%s/ \+/ /g

but this dont seem to work.

Please help.
 
:%s/ */ /g

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
s/[type a space & a tab here][type a tab & a space here/ /g

s/[ ][ ]/ /g

--
| Mike Nixon
| Unix Admin
|
----------------------------
 
Could use tr to squeeze spaces within vi

{!}tr -s ' '
 
nappaji: this syntax is used in awk and perl, not in vi or sed
nixon: you are very close to solution
append a star '*' after the first OR second ']'
to repeat NULL or INFINITE times the first OR second match
so this lines are equivalent:

:%s/[TabSpace]*[TabSpace]/Space/g
:%s/[TabSpace][TabSpace]*/Space/g

will give what you want (same syntax for sed).
note the difference:

:s/\([TabSpace]\)*[TabSpace]/\1/g

replace all occurances of TabSpace by the first
encountered Tab OR Space


:) guggach
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top