05. Tabs to Spaces & Spaces to Tabs expand, unexpand

There may be times when you have finished up writing code, but need to change tabs to spaces for a file to display consistently among different computers. The expand and unexpand commands help you convert from spaces to tabs and tabs to spaces.

Expand filter

In a pipeline, expand converts tabs to spaces. Using specific options, you can specify a number of parameters.

-i
Skip the tab conversions after non-blanks.
-t
Set the number of spaces to replace each tab. By default, it's set to 8.
$ expand -t 4 sample.xml
# Convert every tab into four spaces
<?xml version="1.0" encoding="UTF-8"?>
<note>    
    <to>Tove</to>
    <from>Jani</from>
    <heading>Reminder</heading>
    <body>Please do not forget me this weekend!</body>
</note>

Unexpand filter

The unexpand filter does the opposite - it converts spaces to tabs.

-a
Convert all blanks.
--first-only
Convert only leading sequences.
-t
Set tabs to be a number of spaces apart. Default is 8.

In this example, assume that each "tab" instance is four spaces.

$ unexpand -t 2 sample.xml
# Convert every two spaces to a tab
<?xml version="1.0" encoding="UTF-8"?>
<note>    
  <to>Tove</to>
  <from>Jani</from>
  <heading>Reminder</heading>
  <body>Please do not forget me this weekend!</body>
</note>

Aching back from coding all day?

Inversion Therapy Table

Aching back from coding all day? Try Back Problems

Stretch out your back and relieve your back muscles with inversion therapy. This device counteracts the forces of gravity on the body by decompressing and elongating the spine. By using this product just ten minutes a day, you can be well on your way to improved circulation and posture while relieving muscle aches, back pain and stress.

$$ Check price
119.98119.98Amazon 4.5 logo(1,700+ reviews)

More Back Problems resources

Take your Linux skills to the next level!

Command Line Kung Fu

Take your Linux skills to the next level! Try Linux & UNIX

Command Line Kung Fu is packed with dozens of tips and practical real-world examples. You won't find theoretical examples in this book. The examples demonstrate how to solve actual problems. The tactics are easy to find, too. Each chapter covers a specific topic and groups related tips and examples together.

$ Check price
14.9914.99Amazon 4.5 logo(27+ reviews)

More Linux & UNIX resources

Ad