10
\$\begingroup\$

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?

\$\endgroup\$
  • \$\begingroup\$ meta.codereview.stackexchange.com/q/1376/18427 \$\endgroup\$ – Malachi May 20 '14 at 17:16
  • \$\begingroup\$ @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. \$\endgroup\$ – Jamal May 20 '14 at 17:18
6
\$\begingroup\$

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).

\$\endgroup\$
  • 3
    \$\begingroup\$ Where has this been all my life?!? \$\endgroup\$ – Jamal May 15 '14 at 18:14
2
\$\begingroup\$

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

enter image description here

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

\$\endgroup\$
  • 8
    \$\begingroup\$ Word!!! ???? My Word! \$\endgroup\$ – rolfl May 15 '14 at 0:40
  • \$\begingroup\$ @rolfl: It works for me. ;-) \$\endgroup\$ – Jamal May 15 '14 at 0:41
  • 4
    \$\begingroup\$ +1 for red free hand circle! Keep up the good work @Jamal! \$\endgroup\$ – Marc-Andre May 16 '14 at 17:31
  • \$\begingroup\$ 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.) \$\endgroup\$ – TRiG Apr 27 '17 at 11:00
2
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

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[@]}"
\$\endgroup\$
0
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

Using Notepad++

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.

  • Using Notepad++

    1. CtrlA
    2. Edit->Blank Operations->Tab to Spaces
    3. CtrlC
    4. CtrlW to close file
    5. AltTab back to Stack Overflow
    6. CtrlV to paste fixed code back again

I stole a lot of Rolf's answer for this

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .