Vim: Spaces instead of tab characters.


Indentation is important in Python code. Period. (This is really a big, emphatic, bright period.)

Python programmers who move their code from machine to machine prefer to use “spaces as tab characters” over “normal tab characters” for indentation in their code. There is a good reason why they do that.

If you code in Vim, like I do, you might have noticed that the default behaviour of the tab character is to insert as many tab characters, when you press the tab key, as are defined in the .vimrc file. If you want to change Vim to insert spaces instead, add the following into your .vimrc file (I am assuming a tab width of 8 characters):


set tabstop=8
set shiftwidth=8
set expandtab

I picked this up from here.

Advertisements

11 thoughts on “Vim: Spaces instead of tab characters.

  1. Pingback: Space tabs are evil. « Libel

  2. Pingback: Bookmarks for March 11th through March 17th | Bieber Labs

    • the good reason: if there are actual spaces, you are assured of the same indentation wherever you view the file. tab characters depend on the specific editor you use! if you view a tabbed file in a repository like svn (through its web interface, for instance), you will not see your indentation usually.

      • Maybe the indentation would be consistent if you used spaces, but I always use tabs. People have different tab settings, and by using tabs everyone can configure their .vimrc to change their tab settings to what they prefer. Also if you just use tabs, you don’t get any merge conflicts due to whitespace; not sooo much of a problem anymore with todays tools, but it can still be tedious.

        Good post – I needed ‘expandtab’ to use spaces when writing a patch for Apache, as they use spaces instead of tabs.

  3. Thanks for the post, I am massively enthralled and was thinking if another person had any other related articles they might point me to. I love creating website posts too and would envy to gather as much information as I can.

  4. Pingback: Tabulaciones en vim | Tararea algunas notas

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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