views:

177

answers:

5

It's very easy to set a text editor to use spaces or tab characters with each press of the tab key. However, I'm working with a grip of Python code maintained by a large team of developers in my company, and some use spaces and some use tabs. I cannot simply make them all conform with each other, because 1) it would break git blame, 2) it would muddle git diff, and 3) it would inevitably break the build the next time another editor hits their tab key in one of the files.

Instead, I'd like a text editor that automatically determines whether a file is indented by spaces or tabs and then conforms to the existing layout. Does anybody know if something like this exists?

+2  A: 

Sublime Text does this.

Michael Hackner
Giving it a try now, thanks.
Cory Petosky
View --> Indentation --> Guess settings from buffer
Michael Hackner
This is by far the nicest text editor I've used in Windows. Thanks for the advice!
Cory Petosky
My pleasure...I just recently discovered this program, via Lifehacker. http://lifehacker.com/5383161/sublime-text-is-a-serious-text-editor-with-no-bloat
Michael Hackner
+1  A: 

TextMate for the Mac does this.

I'm sure there are scripts for both vim and emacs.

Georg
+1  A: 

I think geany ( http://www.geany.org/ ) has that option in the preferences, if you use linux.

senicar
It does have that option: http://www.geany.org/manual/current/index.html#indentation Thank you. I've been looking for a decent text editor for my Linux box.
rtperson
Nice, now we've got for each platform one editor. :)
Georg
It also has a Windows installer: http://www.geany.org/Download/Releases
rtperson
A: 

You can do this in vim. This function from codeblog shows how to detect the prevalent formatting for a file, and set your defaults to match:

function Kees_settabs()
    if len(filter(getbufline(winbufnr(0), 1, "$"), 'v:val =~ "^\\t"')) > len(filter(getbufline(winbufnr(0), 1, "$"), 'v:val =~ "^ "'))
        set noet ts=8 sw=8
    endif
endfunction
autocmd BufReadPost * call Kees_settabs()

Personally, I would just go around with a two-by-four and give any developer using tabs a few wacks over the head.

brianegge
A: 

vim can do what you want. vim can do almost anything.

Here is a great recipe for customizing vim for Python: http://henry.precheur.org/2008/4/18/Indenting_Python_with_VIM.html

EDIT: actually, I just played around with this, and here is a simple way to do it. Add these two lines to your vim settings file:

filetype on
autocmd FileType python set smarttab expandtab
steveha