Skip to content
Oct 7 / PK Shiu

Fix Python source code to use spaces instead of tabs

What if someone gave you a Python source file that is indented using tabs? If you are using emacs, the following commands will let you convert it back to using spaces:

# first set the buffer tab width to 4 (or whatever you like)
M-x set-variable <return> tab-width <return> 4

# then mark the entire file
C-x h

# do untabify to convert:
M-x untabify <return>

That’s it!

Share and Enjoy:
  • Print
  • Digg
  • StumbleUpon
  • Facebook
  • Twitter
  • Google Bookmarks
  • Add to favorites
  • email
  • FriendFeed
  • Google Buzz
  • LinkedIn
  • Tumblr
  • Reddit


Leave a comment
  1. Trever Shick / Jan 4 2011

    I had to do this on my own sources 🙁 a while back but used find and sed 🙂

  2. Trever Shick / Jan 4 2011

    for vi users – put in your ~/.vimrc file

    set ts=4
    set expandtab

    then to get rid of existing tabs in a file use an inplace sed script
    :1,$s/^I/ /g

    the “^I” is actually but will output as ^I

Leave a comment