Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want my vim indentation to work as follows: if I have

▷   ▷   int function(int x,▒int y, int z) {

and I press enter, I want to get the following:

▷   ▷   int function(int x,
▷   ▷   _____________int y, int z) {

Where "▷" is a tab and "_" is a space.

In other words, I want to use tabs, but if I have a linebreak in a place that doesn't add an indentation level, then the non-indented line should be aligned with whitespace.

This allows me to use tabs for indentation, but preserve formatting on non-indent levels so that formatting is preserved even if the tab size is changed.

So far, I have not found a way that this is possible with Vim. Preserveindent and copyindent aren't what I'm looking for. They honor expandtab, and I want to only use expandtab for indentation, and spaces for alignment.

share|improve this question
I think this is accomplished with a syntax file. Don't quote me on that (hence why this is a comment and not an answer). – VxJasonxV Oct 4 '10 at 5:06
@VxJasonxV: A syntax file wouldn't do this but an indent file might. – garyjohn Oct 4 '10 at 7:29
An indent file will not do this. An indent file returns the number of indentation levels to be used, which is converted into tabs or spaces depending upon shiftwidth and expandtab. That doesn't let me use tabs for indentation and spaces for alignment. – So8res Oct 4 '10 at 13:45
I thought that was all in a syntax file too. Sorry to waste your time :). – VxJasonxV Oct 4 '10 at 16:17

According to the Vim Wiki, the Smart Tabs plugin will do this. I have not tried it myself.

share|improve this answer
This allows me to only use tabs at the beginning of lines, but doesn't auto change to spaces when, for example, parentheses are unclosed. – So8res May 16 '11 at 23:10
up vote 1 down vote accepted

Currently, this does not look doable in vim. You can either have tabs, or spaces, not both. Unsurprising, as the time for spaces over tabs is very language-sensitive. Consider: In python, how would you treat:

x = fn(abc, 'def', {
                       ghi: jkl,
                       mno: pqr,

Should you go back to tabs at some point? That would be ideal, but very context-sensitive, and wouldn't really work cross-language. Another fun example, in haskell:

data Something = Something { flag1 :: Bool
                           , flag2 :: Bool }
               deriving (Eq, Ord, Show)

Good luck with that one.

I've concluded that language-specific extensions would be necessary for this sort of functionality, unfortunately. If you know of a good way to do this, though, I'd love to hear it.

share|improve this answer

Open your _vimrc file, and add the following at the end:

set tabstop=4
set shiftwidth=4
set smartindent
set expandtab "converts the tabs into spaces
set autoindent
share|improve this answer
This isn't even close. This makes all tabs into spaces, I want to use tabs for indentation and spaces for alignment. – So8res Oct 4 '10 at 13:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .