2

I know this question has been asked many times, but I'm not finding a solution to my problem in particular, mostly because I can't even really identify the problem.

Here's the timeline of events:

  • open old python file (written with python2.7) - everything is fine
  • change key bindings to execute .py files with python3 instead of python2.7 - as expected, lot's of IndentErrors, but file is still the same structure/layout/number of spaces/tabs
  • added expandtab to my .vimrc - no more IndentErrors do to inconsistency anymore, but new IndentErrors all over the place because the structure of my source code is now all out of whack.
  • I remove expandtab from my .vimrc and I still get the same out of whack source, even if I kill-session, quite iTerm, pull the source fresh from github...WHY!?

Just to be clear, there are only TWO things that have been altered in my vimrc in like the past few months and both alterations were made this morning. The first was remaping <F9> to issue a shell command for python3 instead of 2.7, which DID NOT affect the structure of my source code. The second change was adding expandtab which messed everything up...removing expandtab doesn't return my vim experience to the way it was prior to adding it.

The structural alterations to my source are semi-uniform, so let me try and explain what's actually happening.

What used to look like:

class GUI():
    def __init__(self):
        suite of code

now looks like:

class GUI():
    def __init__(self):
    suite of code

and it's like this across the board for what seems like all nested suites of code...in other words, the first expansion of tabs into 4 spaces works fine, but instead of expanding tabs nested further within the first tab, it looks like it just deletes the tab instead.

There are other weird things going on, but I can't really see a pattern.

Any ideas what's going on?

  • It looks like your first indent was four spaces, and your second indent was a tab, with the code being written for a tabstop of 8. Vim is either displaying or expanding with the assumption that tabstop=4. Do you have the original source from before you edited the files with the bad vim settings? – Random832 Mar 6 '15 at 21:23
  • Here's the source: github.com/Aweeeezy/Simulator/blob/master/GUI.py It's not possible that the code was written for a tabstop=8 because I've had the same .vimrc (with tabstop=4) since before I even knew python. – aweeeezy Mar 6 '15 at 21:35
  • The code is definitely tabstop=8, I can select the single tabs on the 8-space indents. And python in mixed indent mode treats tabs as eight spaces. I don't know how your vim configuration was working to show it before. – Random832 Mar 6 '15 at 21:37
  • Interesting...I must have added tabstop at sometime in between, though I don't recall it. Anyways, thanks for isolating the problem. Any advice on retroactively fitting source code as if it was written with tabstop=4 instead? edit: or should I just change tabstop back to 8? – aweeeezy Mar 6 '15 at 21:41
  • %s/\t/(eight spaces)/g. You should not have tabs in python code. – Random832 Mar 6 '15 at 21:42
4

This appearance, with every other indent level dropped, is usually (and seems to be the case in your code) a symptom of looking at code which has 4 spaces per indent, but 8-space tabs tabs on every other line (e.g. 4s, 1t, 1t4s, 2t), with a tabstop setting of 4.

For Python code, you should simply replace all tabs in the code with eight spaces, since that was how the interpreter was treating it. In general you could deal with situations like this by using the :retab command after setting tabstop to the value the source code was originally written with, so e.g.

:set ts=8
:retab 4

If expandtab is set, retab will replace tabs with spaces.

  • Perfect! Before your edit, I tried :set tabstop=8 followed by :retab! and that did the magic. Now onto the next issue of trying to merge my packages from my old version of python into the new one :/ I should have heeded the "always install with a virtual env" advice a long time ago. I still don't really understand how that works. – aweeeezy Mar 6 '15 at 21:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.