Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting a file's indentation from tabs to spaces

What's the best way to convert a tab-indented file into a space-indented one?

I'd like to be able to specify a variable number of spaces per tab replacement.

In other words, in every line that begins with a consecutive sequence of tabs, I'd like replace each tab of the sequence with X spaces, where I get to set X.

I don't mind what tool is used, whether it is AWK, sed, or even Vim.

Anyone know what the best way to do this is?

Update/paraphrase/clarification:

In other words, here is the process that I need to run the file through:

"on each line, replace every tab character that precedes the first non-tab character on that line with 3 spaces"

like image 304
user3025492 Avatar asked Oct 29 '25 05:10

user3025492


1 Answers

The easiest solution on Linux or many Unix systems is to use the expand command.

If you use Vi or Vim to edit your files, you can add the following to your .vimrc file to ensure tabs are always expanded to spaces when editing files:

set expandtab

For more information on Vim and tabs, see Converting Tabs To Spaces.

like image 143
Ned Avatar answered Oct 31 '25 00:10

Ned



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!