How to find and replace all tabs with spaces in idle
Solution 1
IDLE doesn't let you search to literal tab characters. You can paste one into the search box (as suggested by will), but it will never match anything.
However, it does let you do regular expression searches, and the regular expression \t
will match a literal tab. So, turn on the Regular expression
checkbox, and put '\t
in the Find:
box, and 4 or 8 spaces (as appropriate) in the Replace:
box.
But, as will suggested, it's better to use IDLE's features instead of trying to do things manually: Select the block of code with tabbed (or inconsistent) indentation, go to the Format
menu, and select Untabify Region
. (Or just hit control-6.) If the tabs were inserted with an editor that uses 4-space tabs, you may need to first use New Indent Width
and change it to 4, then Untabify Region
.
IDLE doesn't have any code to guess what your tab size was when you wrote the inconsistent code. The only editor I know of that does is emacs. If you just open the file in emacs, it will try to guess your settings, and then you can select the whole buffer and untabify-region
. If it guessed right, you're golden; if it guessed wrong, don't save the buffer, because now it'll be even harder to fix. (If you're one of the 3 people in the world who knows how to read emacs lisp but doesn't like emacs, you could look through the python-mode.el
source and see how it does its magic.)
Solution 2
A generic way to do this is to just copy a tab character from the document (or just do one in any random text editor and copy it) and then put that into the field.
You could try putting \t
in there, but that only works in some editors.
Most IDEs have a function to automatically replace tabs with a predefined number of spaces. I suggest turning that on...
EDIT: doing a standard find and replace could be dangerous if you're using tabs somewhere else for any reason.
If you look here, there's an option called "tabify region". That might be more interesting for you.
Solution 3
There should be an app for that. ;)
If you enjoy overkill, what about running your code through a regular expression like:
re.sub('\t', '\s\s\s\s', yourCode)
Related videos on Youtube
The Unfun Cat
Get your piping hot GenomicRanges for Python here https://github.com/biocore-ntnu/pyranges
Updated on October 09, 2022Comments
-
The Unfun Cat over 1 year
I have an invisible
expected an indented block
error, which is likely caused by me using tabs instead of spaces.When I open the "find/replace" window and try to enter TAB in the
find
field IDLE unsurprisingly just skips to thereplace
field.How do I do this?
Final update:
Thank you for all answers, much appreciated. My problem was that python wants the function comments to be indented too, that is
def imdheladumb(): """ I'm dum as hel """
does not work, it needs to be
def imdheladumb(): """ I'm dum as hel """
-
abarnert over 11 yearsActually, IDLE won't let you search for literal tab characters (well, it will, but they won't match anything). But the tabify functionality in IDLE is probably what the OP actually wants, rather than making search/replace work, so +1.
-
The Unfun Cat over 11 yearsAll answers appreciated ,this one gets A (ccepted) for effort
-
tommy.carstensen over 10 yearsWhy does searching for line breaks using regex and \n not work for me? I'll create a separate question.