Skip to content Skip to sidebar Skip to footer

How To Find And Replace All Tabs With Spaces In Idle

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 f

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)

Solution 4:

For those people having the problem, the new version of VSCode can solve this easily.

  1. Click on Tab Sizes at the bottom of the page.
  2. Select Convert Indentation to Spaces from the "Select Action" menu that pops up.

Post a Comment for "How To Find And Replace All Tabs With Spaces In Idle"