tags:

views:

645

answers:

4

I used to have 8-space tabs in Vim. Then I changed to 4 spaces, but now whenever I add a line to some code I had written before changing to 4 spaces, it gives me an indentation mismatch error even though everything is lining up nicely. Is there any way to avoid this problem?

+3  A: 

Have you done a :%retab ...?

Alex Martelli
+5  A: 

Have you changed just the tabstop option?

I use 4 spaces (fill with spaces when I hit tab, to insert actual tab hit ctrl-v tab). Here are the tab related settings from .vimrc:

" tabs
set tabstop=4
set shiftwidth=4
set expandtab

When you fill tab with spaces you will always insert spaces instead of tab and your code will always look the same.

When you use tabs each tool displays tab differently and you end up spending your time setting up how many spaces should be displayed for tab (8,4,3.5) instead of doing productive work.

Or choose one of these (from vim 7.1 help tabstop):

    Note: Setting 'tabstop' to any other value than 8 can make your file
    appear wrong in many places (e.g., when printing it).


    There are four main ways to use tabs in Vim:
    1. Always keep 'tabstop' at 8, set 'softtabstop' and 'shiftwidth' to 4
       (or 3 or whatever you prefer) and use 'noexpandtab'.  Then Vim
       will use a mix of tabs and spaces, but typing <Tab> and <BS> will
       behave like a tab appears every 4 (or 3) characters.
    2. Set 'tabstop' and 'shiftwidth' to whatever you prefer and use
       'expandtab'.  This way you will always insert spaces.  The
       formatting will never be messed up when 'tabstop' is changed.
    3. Set 'tabstop' and 'shiftwidth' to whatever you prefer and use a
       |modeline| to set these values when editing the file again.  Only
       works when using Vim to edit the file.
    4. Always set 'tabstop' and 'shiftwidth' to the same value, and
       'noexpandtab'.  This should then work (for initial indents only)
       for any tabstop setting that people use.  It might be nice to have
       tabs after the first non-blank inserted as spaces if you do this
       though.  Otherwise aligned comments will be wrong when 'tabstop' is
       changed.
stefanB
+3  A: 

For python code, you are probably best off with the following:

:set tabstop=8
:set shiftwidth=4
:set expandtab

That way you are still using the 'industry standard' 8 space tabs, but you won't be putting any of them into your file. That should keep your old code clean as well, although you'll have to go back through and manually move everything left over time. You'll definitely want to :retab everything too.

If you want to replace everything with 4 space indents do

:set tabstop=4
:retab
:set tabstop=8

This will re-indent everything using spaces at 4 spaces per tab, and set you back to sane defaults.

Obvously, this is subject to opinion, but in my book using tabs set to anything other than what you get when you cat the file is asking for trouble.

easel
Or don't use real tabs but fill with spaces instead. No matter how many 8 or 4 as far as you are consistent.
stefanB
Btw, why would you use different indentation for python?
stefanB
by the way, I recommend "set smarttab"
kcwu
Ugh, I hate code indented with spaces. If Python would just ignore spaces, problems like the OP's wouldn't happen.
John Millikin
+2  A: 

The best way to visualise a mismatch is to :set list which will show whitespace issues.

set listchars=tab:>-,trail:-,nbsp:+ "This is terminal friendly but you can make fancy
set list

I'd say this is an essential setting for python editing when spaced indents are the norm. Especially when a file is edited by a co worker.

I also double checked the style guideunder "Code lay-out". theres a python -tt option you might want to use as specified in http://www.python.org/dev/peps/pep-0008/. That will throw warnings and errors if you mix tabs with spaces.

You could incorporate this into your unit testing. It seems the pep is recommending 4 spaces for newer code. You might want to consider changing if you intend on contributing to open source projects.

also to be extra tidy I have for deleting whitespace at eol

nnoremap <leader>wd :%s/\s\+$//<cr>
michael
Small tip: The offending characters - whether it be spaces or tabs - could be made to stand out nicely when using syntax highlighting by having a special highlighting rule for them.