Tabs in Vim

Tabs seem to be out of fashion in the programming world, there are perfectly valid arguments on both sides or the tabs vs. spaces flame war, illustrated with examples from every programming language and text editor combination ever made. You can read any or all of these if you are interested, but that isn’t the issue here.

I use tabs – all the time. My tab stopping is set to 4 spaces but my intention is to use actual tab characters. Its actually surprisingly hard to do this with vim. I’ve added the following lines to my .vimrc which allow me to see whether I have tabs or spaces at the start of the line.


set list listchars=tab:\|_,trail:.
highlight SpecialKey ctermfg=DarkGray

I fiddled with some other settings too, to try to stop everything ending up as spaces (I’m driving my colleagues mad) but to no avail. With these lines:


set shiftwidth=4
set softtabstop=4
set tabstop=4
set noexpandtab
set autoindent
set smartindent

I think those are all the relevant lines … it kind of works but I still need to type the set noexpandtab when I’m editing a file to stop auto-indented new lines from getting space instead.

Its all very exasperating so if anyone has suggestions other than “use spaces” I’d be interested to hear them!

2 thoughts on “Tabs in Vim

  1. Try [code]:help modeline`[/code]

    If you really want to go for driving others super-crazy, try using a modeline in the first three or last three lines of the file, e.g.:
    [code]
    # vim: sw=4 softtabstop=4 ts=4 noet au smartindent
    [/code]
    That way, anyone else who edits the document with vim will be stuck with your conventions.

    But seriously, they will also see it in vim as you see it and leave it in a form that won't drive you crazy.

Leave a Reply

Please use [code] and [/code] around any source code you wish to share.

This site uses Akismet to reduce spam. Learn how your comment data is processed.