Software

Quickly replace multiple space characters with a tab character

Replacing multiple spaces with a tab can be a nightmare, unless you know this simple Find and Replace trick.
Word processing has been around for a long time, and the techniques seem ingrained in our psyche. Yet every once in a while, I run across a document that uses multiple spaces instead of tabs to align text. The result is often a big mess. There are three ways to fix this problem:
  • Manually replace the multiple space characters with tabs.
  • Use several Find and Replace tasks, which is a bit faster than doing it manually.
  • Use one Find and Replace task to replace each instance of multiple space characters with a single tab character.
I know which one I’d choose! To replace multiple and consecutive spaces with a single tab character, do the following:

  1. Choose Replace from the Edit menu (or press [Ctrl]+H) to open the Find And Replace dialog box.
  2. Click the More button.
  3. In the Find What text box, enter one space character and the following characters, exactly as shown: {2,}.
  4. In the Replace With control, enter ^t.
  5. Check the Use Wildcards option.
  6. Click Replace All.
  7. Click Close.

The {2,} component tells Word to find two or more of the literal character, which in this case is a space character. You could use this component to find other multiple characters. The ^t component represents a single tab. To replace the spaces with more than one tab, simply add one ^t component for each additional tab. Keep in mind that this technique will replace every occurrence of multiple and consecutive space characters, including some you might not want to replace with a tab. If you want to retain a legitimate occurrence of multiple spaces, select only the text that you want to run the Find and Replace task against before executing it. Or click Find Next so you can review the occurrence to decide whether you want to replace it or not. This replace trick will work in any Office application, not just Word.

About

Susan Sales Harkins is an IT consultant, specializing in desktop solutions. Previously, she was editor in chief for The Cobb Group, the world's largest publisher of technical journals.

30 comments
mfa
mfa

I've got a document they used multiple tabs in to cross wide swaths of blank space. The document is basically a table, which I'd like to import into Excel. I can't use tabs as the delimiter, beacuse there may be multiple and varying numbers of tab characters in what should be white space. And I can't used fixed column widths, either. I want to save the document somehow preserving all the white space as blanks.

jon_saxon
jon_saxon

Is it just me or is there an Excel dialog box embedded on this post between steps 5 and 6? Otherwise, thanks for a higher-tech way to do what I have done before which is to search and replace on multiple spaces. You can also just search on " " (without the quotes).

Jacky Howe
Jacky Howe

I've been manipulating text in notepad for years but I have never used this principal. I have always used the keyboard to make a space or tab and then copy it into the fields depending on what was being replaced. You learn something new every day. Thanks Susan. ;)

Marshwiggle
Marshwiggle

"This replace trick will work in any Office application, not just Word." ... Ctrl+H in Outlook?

Rick_from_BC
Rick_from_BC

This is just one of several tricks for cleaning up those pesky reports. In the expression {2,}you can add a second figure after the comma to specify how many spaces to replace with the tab character. E.G., {3,7} will replace any string of 3 through 7 of the specified characters with the designated character. In the example above, you would replace the space character with a tab for every string of 3-7 spaces. Take a good look at drop-down menu at the bottom centre marked "Special." The items here can greatly increase your search and replace functionality. One example: you can switch from a listing of "Doe, John" to "John Doe" using the 'expression' search function. To get started, open Word, and in the Help menu, search for "wild cards" then In the resulting list, look at "Advanced Search Methods" and in the text, click on the "Wildcard characters you can use when searching." (Wording on the Mac - may vary slightly on the PC.) Have fun.

miarond
miarond

This has to be one of the most helpful tidbits of knowledge I've found on TechRepublic so far! I work with an enterprise tape backup software that originated in Unix and was ported to Windows (poorly, I might add). The company that bought out this software ( EMC) didn't do much to update it and, hence, all the administration has to be done via the command line. Well, all the reports that you get from the command line are text based and, not surprisingly, the format is delimited by a random number of spaces! I tried this method out on one of my reports and voila!, it worked perfectly! No longer do I have to bend over backwards to make my data readable and usable! Thank you Susan Harkins, you are an IT Goddess!

HAL 9000
HAL 9000

Try reposting this in the 'Q&A' forum. The 'Discussion' forum is for matters of general discussion, not specific problems in search of a solution. The 'Water Cooler' is for non-technical discussions. You can submit a question to 'Q&A' here: http://www.techrepublic.com/forum/questions/post?tag=mantle_skin;content There are TR members who specifically seek out problems in need of a solution. Although there is some overlap between the forums, you'll find more of those members in 'Q&A' than in 'Discussions' or 'Water Cooler'. Be sure to use the voting buttons to provide your feedback. Voting a '+' does not necessarily mean that a given response contained the complete solution to your problem, but that it served to guide you toward it. This is intended to serve as an aid to those who may in the future have a problem similar to yours. If they have a ready source of reference available, perhaps won't need to repeat questions previously asked and answered. If a post did contain the solution to your problem, you can also close the question by marking the helpful post as "The Answer". .

ssharkins
ssharkins

I really don't know what happened. I suppose it was the wrong figure all along, but I really don't think so -- some figures don't really matter, but this one does, and I think if it had been the wrong figure, someone would've complained. But, I admit, it's possible I could've made that mistake. At any rate, it's the right figure now, so thanks for letting me know!

ftemplin1465
ftemplin1465

This is why I like browsing through TechRepublic. There is great information in both the articles and in the comments that follow. So thank you Susan and 'comment contributors'. And thank you TechRepublic.

jim.beattie
jim.beattie

This doesn't work for me in either version of Word, I get a message saying invalid pattern search. Are there any other settings that need to be adjusted as this would be a helpful tool.

ssharkins
ssharkins

As far as I know, it works in all of the Office applicatins.

Saurondor
Saurondor

You might want to look into a text editor that supports regular expressions. To overcome the limitation of replacing any space sequence you can use other characters. For example ^ indicates beginning of line. So: Find : ^[\s]{2,2} Replace with : \t Says look for and replace any two spaces in the beginning of the line with one tab. The following: Find : ^[\s]{4,4}([\S]) Replace with : \t\t\1 Says to look for and replace any four spaces between the start of line and any non-space character with two tabs. These expressions can become as complex as you require them for your reports and then just saved and rerun on every report.

ssharkins
ssharkins

Thank you for letting me know you found this tip helpful. I'm so glad, because it sounds like you've got quite a task with those reports!

ssharkins
ssharkins

Thank you -- I'm glad you found this one useful. I use it all the time!

DuhGreek
DuhGreek

Are you certain that you entered a single space before the brace notation? The brace notation applies to the character/pattern that immediately precedes it. If there is nothing before it, you will get the error you mentioned.

ssharkins
ssharkins

Did you check the Use Wildcards option, as instructed?

Marshwiggle
Marshwiggle

... not only does Ctrl+H not bring up the Find/Replace dialog, Replace isn't even on the Edit menu. But the main thing is, this is going to save me a lot of work in Word, which I use to convert text-doc song sheets, where guitar chords have been placed over lyrics using spaces, to Word docs, where using tabs produces more accurate results. Thanks.

rvanderhoof
rvanderhoof

I can't get this or Susan's suggestion to work with Office 2003. Does it work only in Office 2007?

ssharkins
ssharkins

This is a great lesson -- thank you for sharing this information!

ssharkins
ssharkins

I'm so glad you figured this out and thanks to everyone who helped!

jim.beattie
jim.beattie

You are correct, thanks for the tip, it works well. Guess I should have read the original script a little closer

jim.beattie
jim.beattie

Yes, I typed them in myself. This is the message I get: "The Find What text contains a Pattern Match expression which is not valid"

ssharkins
ssharkins

Are you absolutely certain that you have multple spaces in this particular document?

jim.beattie
jim.beattie

Yes I checked the wildcard option. In fact I did it both ways I tried all sorts of other combinations and characters. Without wildcards checked it searches the document and finds nothing.

mdhealy
mdhealy

Yep, regexes are extremely useful. I myself have used vim for many years, but vim does have a challenging learning curve because it is based on the ancient vi editor. For those who've never learned vi wikipedia lists a number of editors worth considering: http://en.wikipedia.org/wiki/Source_code_editor One of the best known open-source editors around is http://notepad-plus.sourceforge.net/uk/site.htm (I've not used it, since I can do everything I need with a combination of vim and Perl, but it does have an excellent reputation).

NickNielsen
NickNielsen

Microsoft uses ^p for the paragraph marker and ^l for line breaks. One of these may work to search from the beginning of a line.

Saurondor
Saurondor

Apparently Word doesn't have a way to detect the start of line with their regular expression syntax. If what you're working with is a text document you might want to download and install PSPad or similar tool. Otherwise for word documents Open Office supports a better range of regular expressions and can open .doc files.

DuhGreek
DuhGreek

that Saurondor refers to were refined in Unix for its many utilities that use pattern matching. They have been since been applied for use by Perl and other programs and are extremely flexible and powerful. Microsoft products did not implement "regular expressions" as I know them. There are some similarities but for the most part IMHO, MS is in its own world regarding pattern matching. While useful, theirs pale when compared to "regular expressions".

rvanderhoof
rvanderhoof

Thanks dperley. That worked for Susan's suggestion. But I get a warning " ^ is not a valid special character" if I try Saurondor's suggestion in Word 2003.

dperley
dperley

Did you check the "Use wildcards" option on the expanded ("More") screen? It didn't work for me until I did.

Editor's Picks