VIM and tab to space migration

Robert Montante bobmon at bloomu.edu
Fri May 7 18:57:56 EDT 2004


>> 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. 
> 
>> [snipped]
> 
> But I haven't. I did what you suggested, opened a file using ONLY tabs for indentation and VIM stilled inserted spaces when it pressed the tab yet.
> 

Umm, are you aware of vim configuration files?  On Unix the per-user file is ".vimrc" and the global file is $VIM/vimrc ($VIM being vim's installation directory); on Windows the file is %VIM/_vimrc (vim installed in %VIM folder).

Choose the appropriate config. file, and add commands to force the tab behavior you want.  For example:

  :set tabstop=4        " Force tabs to be displayed/expanded to 4 spaces (instead of default 8).
  :set softtabstop=4    " Make Vim treat <Tab> key as 4 spaces, but respect hard Tabs.
  :                     "   I don't think this one will do what you want.
  :set expandtab        " Turn Tab keypresses into spaces.  Sounds like this is happening to you.
                        "    You can still insert real Tabs as [Ctrl]-V [Tab].
  :set noexpandtab      " Leave Tab keys as real tabs (ASCII 9 character).
  :1,$retab!            " Convert all tabs to space or ASCII-9 (per "expandtab"),
                        "   on lines 1_to_end-of-file.
  :set shiftwidth=4     " When auto-indenting, indent by this much.
                        "   (Use spaces/tabs per "expandtab".)
  :help tabstop         " Find out more about this stuff.
  :help vimrc           " Find out more about .vimrc/_vimrc :-)

I think it would be really hard to get Vim to guess whether tabs or spaces are being used in each file (unless you're willing to add a "vim" line to the end of each file), but it's really fairly easy to get vim to force each file into whichever convention you want, as soon as it's edited --- just add the "retab" command to the .vimrc file, and it'll convert automatically.

(Oh yes, you can also do all the above commands "manually" from the command line, but once you get the ones you want, it's most convenient to put them in .vimrc)

HTH, -bob,mon.




More information about the Python-list mailing list