VIM and tab to space migration

Alejandro López-Valencia me at privacy.net
Thu May 6 08:56:01 EDT 2004


On 2004-05-06, Brian Quinlan wrote in <mailman.302.1083833114.25742.python-list at python.org>:
>> I use these settings myself, but just now after posting, I realized that
>> it is far better to set the preferences on a per-buffer basis, so I
>> change my advice to the following:
>
> [snipped]
>
>> And this is *really* all there is to it.
>
> I don't know what this is supposed to do, but it doesn't seem to do what 
> I want. Here is the test I performed:
>
> 1. create a new file using tab indentation
> 2. open it in VIM
> 3. hit the tab key
>
> VIM inserts 4 spaces for the tab so I now have a file containing mixed 
> tabs and spaces.

If you are starting with a file that already contains tabs *and* spaces,
I'd say you have already called upon you the gods of disaster. Python
source code files should use *either* tabs or spaces and the recommended
length for a tab is the equivalent of 4 spaces. Unfortunately most
editors can't be told the size of a hardware tab and assume it is 8
spaces always or if having some built-in smarts read the definition from
the current terminal terminfo/termcap driver and that is a fixed value.
Unless you use 8-space tabs, you can't be sure someone somewhere will
not use an editor that you haven't ever heard of, or the same editor
with different settings, destroy the indentation by simply opening it
and then, finding you write lousy code, curse you and your children for
ten generations :-).

That is one reason why GvR recommends strongly to use tabs *or* spaces,
but not both. In the case of Vim, there exists an often overlooked
command, 'retab' that allows you to fix such files. A judicious use of
'retab' and tabnanny.py (included in python's standard library) can
fix such disasters. I myself don't care if a python source code file has
tabs *or* spaces, I do care it is properly formatted and indented,
therefore it runs in *my* interpreter. Using spaces only is a warranty
it will happen so.

> Ideally, I would like VIM to be smart enough (like 
> every other programmer's editor in the universe) to detect the current 
> tab settings in the file and use those.
>

Then you experience of the world is very limited :-) I am aware of only
some very expensive commercial editors/IDEs that can do such thing in a
general way. X/Emacs, can insofar as you write the E-Lisp/CL/Python code
to do it. Vim can, insofar as you write the VimL, TCL, Python, Ruby,
Perl or MzScheme code to do it (probably someone has already written it,
but I haven't had the motivation to look for it.  Search the community
sites; Vim's is at http://www.vim.org/).

If you have a vim binary linked to a python interpreter, you can write a
python/vim script that sniffs the file and sets options the way you like
by seting up a hook on the FileType autocmd event for python files.
(You could write it in VimL so that is as portable as possible, but I'm
trying to keep the post on topic ;-).

-- 
Alejandro López-Valencia
qenqhy ng rgo qbg arg qbg pb
http://dradul.tripod.com/
The limits of my language are the limits of my world.
                                    (L. Wittgenstein)



More information about the Python-list mailing list