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?

Cory Petosky
  • 12,458
  • 3
  • 39
  • 44
  • 4
    Seriously consider setting a company-wide standard (of spaces only). You are setting yourself up for problems. Like code not working, that is very difficult to troubleshoot because suddenly some random IF block is outdented.... but looks correct to eye and parser. – Joe Koberg Nov 09 '09 at 21:47
  • I could not agree with you more, and it's something I'm pursuing. I just need a workaround in the meantime. – Cory Petosky Nov 09 '09 at 21:50
  • 1
    I've never learned Python, but I would think this would be a huge issue. Haskell shares Python's concept of meaningful whitespace, and its compiler thrashes like a beached whale if you have tabs in your code. – rtperson Nov 09 '09 at 22:04
  • Python correctly handles a file using spaces or a file using tabs, but does not handle a file using both spaces and tabs (which is completely understandable). – Cory Petosky Nov 09 '09 at 22:06
  • Python (2.x at least) can manage mixed spaces and tabs; tabs are interpreted as the "standard" "move to the next multiple-of-8 column". If your editor is set to use 8-column tabs, you won't notice a difference and your code will still work. However, if you have 4-column tabs and the file looks ok to your eye, it won't look ok for python. – tzot Nov 09 '09 at 22:40
  • Python can handle it and it all works great as long as EVERYONE's editor AND ALL the python runtimes ALL agree that tab = 8 spaces (or 4 or whatever). However, this is NEVER the case. A lot of editors come with tabs every 4 spaces. The python runtime defaults to 8, so there's already a "gotcha". – Joe Koberg Nov 10 '09 at 17:24

6 Answers6

6

Sublime Text does this.

Michael Hackner
  • 8,605
  • 2
  • 27
  • 29
1

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

senicar
  • 61
  • 3
1

For Emacs, there is guess-style.

0

TextMate for the Mac does this.

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

Georg Schölly
  • 124,188
  • 49
  • 220
  • 267
  • Is this still valid with the latest 2 versions? If so, how is it done? – kainjow Dec 06 '16 at 23:08
  • I don't know unfortunately. I've written this answer 7 years ago and a quick google search showed me some results that might even show that my answer was wrong, i.e. that TextMate does _not_ automatically detect soft tabs. – Georg Schölly Dec 09 '16 at 21:49
  • 1
    I did contact TextMate support and it is not possible with the current 2 version. – kainjow Dec 10 '16 at 00:29
0

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
  • 29,240
  • 13
  • 74
  • 99
0

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
  • 74,789
  • 21
  • 92
  • 117