# How can I replace tabs with spaces in code?

The code in my post appears to have improper indentation, but the indentation in the editor differs. There is usually a different amount of indentation between the two, which must be so because there are tabs used in the code. I'm afraid others will assume that I'm unaware of proper indentation, thus this will become part of a review.

What are some methods for removing these tabs and replacing them with spaces?

• meta.codereview.stackexchange.com/q/1376/18427 – Malachi May 20 '14 at 17:16
• @Malachi: It's not quite the same. That question states: I've replaced tabs with spaces in my editor. This question is for those who haven't done that yet. – Jamal May 20 '14 at 17:18

# Tabs to Spaces Online Converter

There's a (web)app for everything. In this case thanks to Anders Åberg, who was thoughtful enough to even include a feature for adding four additional spaces to the beginning (to generate a markdown code block).

• Where has this been all my life?!? – Jamal May 15 '14 at 18:14

# Using Microsoft Word

1. Paste the code (extra text is okay) and place the cursor at the beginning
2. Open Find and Replace
• Enter ^t into the Find field
• Enter the desired number of spaces into the Replace field
3. Click on Replace All
4. Replace the old text in the editor with this fixed text

This image is from Word 2010 and may differ from other versions.

• Word!!! ???? My Word! – rolfl May 15 '14 at 0:40
• @rolfl: It works for me. ;-) – Jamal May 15 '14 at 0:41
• +1 for red free hand circle! Keep up the good work @Jamal! – Marc-Andre May 16 '14 at 17:31
• If you want to do the same in a sensible code editor, the escape for a tab may well be \t, not ^t. (It is on GEdit, anyway.) – TRiG Apr 27 '17 at 11:00

# Using vim

Select the code, and paste it in to an empty vim terminal/screen.

Enter the following sequence:

:set ts=4
:set expandtab
:retab


Select the code out again.

# Using single-purpose GNU programs

### GNU Expand

The expand utility works (only) as a pipeline program:

for f in "${sources[@]}" do cp "$f" "$f.bak" && expand <"$f.bak" >"$f" done  ### GNU Indent The indent utility can completely re-layout your code. The option to expand tabs is -nut: indent -nut "${sources[@]}"


# Using Emacs

Emacs comes with an untabify function:

untabify is an interactive autoloaded compiled Lisp function in tabify.el.

(untabify START END &optional ARG)

Convert all tabs in region to multiple spaces, preserving columns. If called interactively with prefix ARG, convert for the entire buffer.

Called non-interactively, the region is specified by arguments START and END, rather than by the position of point and mark. The variable tab-width controls the spacing of tab stops.

This can be used interactively in the normal way with M-x, or bound to a key sequence.

Rolf Actually posted an answer Here that shows how to format code for posting here from Eclipse and NotePad++

I use NotePad++ which is why I remember that we had discussed this elsewhere.