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?
-
\$\begingroup\$ meta.codereview.stackexchange.com/q/1376/18427 \$\endgroup\$Malachi– Malachi2014年05月20日 17:16:48 +00:00Commented May 20, 2014 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– Jamal Mod2014年05月20日 17:18:10 +00:00Commented May 20, 2014 at 17:18
6 Answers 6
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).
-
3\$\begingroup\$ Where has this been all my life?!? \$\endgroup\$2014年05月15日 18:14:49 +00:00Commented May 15, 2014 at 18:14
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 Microsoft Word
- Paste the code (extra text is okay) and place the cursor at the beginning
- Open Find and Replace
- Enter
^t
into the Find field - Enter the desired number of spaces into the Replace field
- Enter
- Click on Replace All
- 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.
-
8\$\begingroup\$ Word!!! ???? My Word! \$\endgroup\$2014年05月15日 00:40:00 +00:00Commented May 15, 2014 at 0:40
-
\$\begingroup\$ @rolfl: It works for me. ;-) \$\endgroup\$2014年05月15日 00:41:03 +00:00Commented May 15, 2014 at 0:41
-
4\$\begingroup\$ +1 for red free hand circle! Keep up the good work @Jamal! \$\endgroup\$Marc-Andre– Marc-Andre2014年05月16日 17:31:31 +00:00Commented May 16, 2014 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– TRiG2017年04月27日 11:00:55 +00:00Commented Apr 27, 2017 at 11:00
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 intabify.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
andEND
, rather than by the position of point and mark. The variabletab-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.
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++
- CtrlA
- Edit->Blank Operations->Tab to Spaces
- CtrlC
- CtrlW to close file
- AltTab back to Stack Overflow
- CtrlV to paste fixed code back again
I stole a lot of Rolf's answer for this