Is there a text editor that will automatically determine whether to indent with spaces or tabs? [closed]
开发者_C百科
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 7 years ago.
Improve this questionIt'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?
Sublime Text does this.
I think geany ( http://www.geany.org/ ) has that option in the preferences, if you use linux.
For Emacs, there is guess-style.
TextMate for the Mac does this.
I'm sure there are scripts for both vim and emacs.
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.
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
精彩评论