Tabs to Whitespaces

Recently I needed to change a couple of files in a python project to make them PEP 8 conform. Usually I’m using tabs instead of spaces. For me this makes much more sense. People who advocate spaces are using the tab key anyway, so why redefining this key to write two, four or even eight whitespaces instead and get confused when working in a team with other people?

However, the mighty Python Style Guide has spoken, now I need to use four tabs and want to redefine the tab key and convert my present code.

Converting Python Code from Tabs to Whitespaces

This can be easily done via sed: sed -i 's:\t: :g' *.py
Note the four whitespaces between \t: and :g!

Changing the VIM Configuration

For editing python scripts I usually use vim (or gvim). In this case I want to change my settings for python files only, other file types should still contain tab characters. So I defined a new file type plugin by creating a file named ~/.vim/ftplugin/python.vim with the following content:

setlocal tabstop=4
setlocal softtabstop=4
setlocal shiftwidth=4
setlocal expandtab
setlocal smarttab
setlocal smartindent

This will redefine the tab key to write four space characters and allows me to press backspace just once to delete one full indentation level. Thanks to Kai for the help.

Changing the SciTE Configuration

Sometimes I use SciTE for editing scripts, it’s a simple and fast graphical text editor with good drag’n’drop support. To edit the python settings go to “Options” -> “Edit Properties” -> “Open” and add these lines to the end of the configuration file:


0 Responses to “Tabs to Whitespaces”

  1. Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: