Tabs in Vim: When the alignment is all wrong how do you fix…

pythonvim

So my situation is that I have a file, myfile.py (written in python) that I started developing on my own machine. I used vim to do this, but at that time I did not know that Vim could let you define the length of your tabs, and also to show how many times the line had been tabbed by using a marker.

So before I SCP'ed myfile.py to a remote unix server and began working on it there, all my tabs were about 6 spaces in length each, and there were no markings defining how many indentations were present in a particular line. But since 6 spaces is a lot, there really was no need…

But when I began developing on the remote server, I noticed that their vim configurations had the tab so that it would be about 4 spaces in length each, and so that there would be vertical markings to show that a tab is present. However, it did not automatically change the lines of code that were already there in myfile.py to fit their tab-settings, and the settings ended up only applying to any new lines of code I wrote using vim on their server.

Being the newb that I was, I didn't bother to fix anything in order to make the file look consistent. Now I have some weird, interleaved-mixture where some lines have tabs that are 4 in length, others with 6, and the ones with shorter length have a vertical marking for each tab and the longer ones have nothing.

How do I efficiently fix this mess to revert back to one standard? (hopefully the one that has the shorter tab length with the vertical markings). I'm looking for anyone who has even remote experience regarding this predicament – whether this causes me to learn more vim on an advanced level, or start with some roundabout way first, I do not care. I just prefer anything over manually fixing each line – you know how wrong that could make your code when you're dealing with Python!

[EDIT]-> Also thought I should note that each line does not have the same number of tabs… some lines have 0, 1, 4, 5, etc etc…

Best Answer

Indentation in vim can be a little hairy.

You can configure vim to either insert a tab character or insert some number of spaces when you press the tab key using the expandtab option. You can also configure how wide tabs (how many spaces they take up) are with the tabstop option. Finally you can also configure vim to display tabs with a special character using the listchars option (▸ is a common choice). However that character won't be shown where spaces are used in place of tabs. That is why some of your tabs have a special character and some do not, some of them are actual tab characters, and others are just a lot of spaces acting like a tab.

It sounds like the remote unix server was configured with expandtab on and tabstop set to 6, so when you enter a tab, it outputs 6 spaces, while you're development server seems to be configured with noexpandtab and tabstop set to 4.

To fix this you can use vim's find/replace functionality to replace all occurrences of six spaces with a single tab using the command :%s/ \{6}/\t/g. To understand that command you should read up on vim's search and replace capabilities here. To keep this from happening in the future you should make your own .vimrc file that sets things up the way you like it and either place it in your home directory so it is automatically loaded or :source it whenever you edit any files.

Note: There are also other tab options such as shiftwidth and softtabstop but they aren't as relevant to your problem. However you should read about them so you can configure vim to act exactly how you want.

Related Question