How to apply the right indentation

764 views
Skip to first unread message

caruso_g

unread,
Oct 3, 2009, 8:47:33 AM10/3/09
to vim_use
Hi group,
I just started to use Vim (actually, MacVim) and I really love it. :)
I would like to ask you how to re-indent old files I edited in
TextMate. They use spaces instead of tabs, set to two spaces.
Now I have some rows rightly indented to the Vim default and some
stick to two spaces like in TextMate.
Which is the command to reformat them?
And, which are the settings to add to my .vimrc file?
Thanks you all.

Tim Chase

unread,
Oct 3, 2009, 11:35:24 AM10/3/09
to vim...@googlegroups.com
> I just started to use Vim (actually, MacVim) and I really love it. :)

Welcome aboard!

> I would like to ask you how to re-indent old files I edited in
> TextMate. They use spaces instead of tabs, set to two spaces.
> Now I have some rows rightly indented to the Vim default and some
> stick to two spaces like in TextMate.
> Which is the command to reformat them?

The settings are controlled by various correlated settings:

:set ts=2 sw=2 et

which you can learn about at

:help 'tabstop'
:help 'shiftwidth'
:help 'expandtab'
:help :retab

Setting the 'tabstop' to 2 changes your tab-stops to
2-visual-spaces-per-tab. I like to keep my 'shiftwidth' in sync
with my 'tabstop' setting just so things like the ">"/"<"
operators and ^D/^T work as expected in insert-mode.

If Vim is inserting real tabs instead of expanding spaces, the
'expandtab' setting will change future insertions. To change
past insertions of real-tabs, once you have the above ":set"
command, you can then use ":retab" to force vim to change
existing tabs to the spaces you defined.

> And, which are the settings to add to my .vimrc file?

You should be able to just add the above "set" (minus the colon)
to your vimrc to get this behavior by default. Alternatively, if
you allow modelines, you can include a modeline in your files
that specifies these settings:

:help modeline
:help 'modeline'

which would allow you to put something like

/* vim: set tw=2 sw=2 et */

at the top or bottom of your file, and vim will set them on a
per-file basis.

Hope this helps,

-tim

caruso_g

unread,
Oct 3, 2009, 5:56:28 PM10/3/09
to vim_use
Hi Tim,
thanks a lot, I tried the ":set ts=2 sw=2 et" and it worked perfectly
but only on the new tabs inserted by MacVim.
I tried to select all the text and to apply the command:

:'<, '>retab!4

or also without the number value (since accordingly to the manual it
should set it the ts value) but it didn't change.
I don't know if I invoked in the right way the command, did I?

Anyway, thanks a lot, I am going to set them in my .vimrc file for
next files. :)
Is it right in the following way?

set ts=2 sw=2 et

One last question. Can I set different values for different file
types? E.g. 4 spaces for html, 8 for php and 2 for Ruby?

Thanks a lot. :)

John Beckett

unread,
Oct 4, 2009, 7:15:15 AM10/4/09
to vim...@googlegroups.com
caruso_g wrote:
> One last question. Can I set different values for different
> file types? E.g. 4 spaces for html, 8 for php and 2 for Ruby?

I have put a new section ("Different settings for different file
types") in this tip:
http://vim.wikia.com/wiki/Indenting_source_code

Would Vimmers please have a look at the above and let me know if
any changes should be made because I find it incredibly
irritating that we do not have an easily-accessible and
"correct" set of answers to good questions such as the above.

BTW, for all new users:
Please bottom post on this list. Quote a small (relevant) part
of the message you are replying to, and put your text underneath.

See
http://groups.google.com/group/vim_use/web/vim-information

John

Robert H

unread,
Oct 4, 2009, 4:09:27 PM10/4/09
to vim...@googlegroups.com
On 10/3/09 5:56 PM, caruso_g wrote:
<snip>

> set ts=2 sw=2 et
>
> One last question. Can I set different values for different file
> types? E.g. 4 spaces for html, 8 for php and 2 for Ruby?
>

autocmd FileType perl :set ts=4 sw=4 et tw=78
autocmd FileType xml :set ts=2 sw=2
autocmd FileType html :set ts=2 sw=2 et tw=78

Those are just examples...you can do your own. :-)

Tony Mechelynck

unread,
Nov 1, 2009, 2:44:25 PM11/1/09
to vim...@googlegroups.com

Or rather, put them into vim-scripts in ~/.vim/after/ftplugin/ (on Unix)
or in ~/vimfiles/after/ftplugin/ (on Windows):

--- ftplugin/perl.vim
setlocal ts=4 sw=4 tw=78 et

--- ftplugin/xml.vim
setlocal ts=2 sw=2 tw=0 noet

etc. (Create the files and/or directories if they don't exist yet.)

It is important to use ":setlocal" rather than ":set", to avoid changing
the setting on other files being edited in parallel.


Best regards,
Tony.
--
hundred-and-one symptoms of being an internet addict:
171. You invent another person and chat with yourself in empty chat rooms.

Reply all
Reply to author
Forward
0 new messages