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

4

2

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...

Dark Templar

Posted 2011-12-20T05:45:32.023

Reputation: 2 329

write a python program that swaps out tabs for however many spaces... Or open the files in an IDE like Xcode with your tab preferences set to the new standard, select all text and re-indent... These are more elegant than manual. I like the first solution because you use a hammer to fix a hammer. – None – 2011-12-20T06:13:52.897

But not every line has the same number of tabs... some may just have 1, but there are some that even have 6! – Dark Templar – 2011-12-20T06:18:41.233

Try vimcasts.org. Episode 2 For tabs Episode 1 for showing hiddens

– Martin York – 2011-12-20T06:44:03.127

4Did you try a simple :retab! already? – sebastiangeiger – 2011-12-20T07:02:13.643

@sebastiangeiger - Thanks, I just tried that, since it was the simplest solution I've gotten so far - but it seems that what vim may be doing is chopping up the 6-length tabs into two (as denoted by the vertical markings :retab! has added; automatically adding in the vertical markings was part of my goal, so that is taken care of. The problem is that some lines where I was sure contained only one 6-length tab now shows up as containing TWO vertical marks) – Dark Templar – 2011-12-20T18:47:28.457

Hence that's why I suspect that command is just chopping up any tab that appears bigger. I originally left some comments when working on my own machine indicating that certain 6-length tabs were indeed only 1 tab :( – Dark Templar – 2011-12-20T18:51:58.107

Nevermind, I was tricked! The problem is that working on *my* machine had settings so that pressing the TAB button would insert TWO tabs (8 spaces). THAT's what the problem was! – Dark Templar – 2011-12-20T18:57:29.873

It's a shame that Python won't let you use *curly braces* to denote the start and end of a function or a block of code. The indentation rule just makes it confusing to debug files that are of at least a certain length!! – Dark Templar – 2011-12-20T19:06:35.867

Answers

4

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.

David Brown

Posted 2011-12-20T05:45:32.023

Reputation: 176

Having just messed up my file this way, beware of the simple find-replace option. If you look for 6-space indents, you may also find one-and-a-half 4-space indents. And how will you tell two 6-space indents from three 4-space indents. – Jacktose – 2018-10-23T21:07:03.690

1

HOW TO CONVET 6 -> 4

1. Your current setting(tab == 6-spaces-width && expand tab)

:set ts=6 et

#!/usr/bin/env python
def fun():
······print "hello, world"

2. convet 6-spaces to tab

:set noet
:ret!

#!/usr/bin/env python
def fun():
»·····print "hello, world"

3. reset tab to 4-space-width

:set ts=4

#!/usr/bin/env python
def fun():
»···print "hello, world"

4. convet tab to 4 spaces

:set et
:ret!

#!/usr/bin/env python
def fun():
····print "hello, world"

kev

Posted 2011-12-20T05:45:32.023

Reputation: 9 972

0

How I would to it. Set up the tabbing in your .vimrc on both machines as you prefer the most.

Number of spaces a tab counts for

set tabstop=4

Number of spaces used for (auto)indent

set shiftwidth=4

If you want to expand tabs to spaces

set expandtab

or not

set noexpandtab

Every time you open a file that has a messed up indentation. Go to the start of the page gg and autoindent to the end of the page =G

tidbeck

Posted 2011-12-20T05:45:32.023

Reputation: 1 365