views:

279

answers:

2

Using vim, I would like to effectively have expandtabs off if I'm to the left of any text on the line and on if I'm to the right of any non-whitespace character. (I would like to use tabs for indentation and spaces for alignment.)

Can this be done?

+4  A: 

Try something like this:

function! TabMaybeIndent()
    if strpart(getline('.'), 0, col('.') - 1) =~ '^\s*$'
        return "\<Tab>"
    else
        return "    "
    endif
endfunction

set noexpandtab
imap <Tab> <C-r>=TabMaybeIndent()<CR>
Brian Carper
This looks like a reasonable approach. With minor adjustments, you could even follow the current `softtabstop` setting instead of blindly inserting 4 spaces.
ephemient
+5  A: 

Yes. Use the Smart Tabs plugin.

This script allows you to use your normal tab settings for the beginning of the line, and have tabs expanded as spaces anywhere else. This effectively distinguishes 'indent' from 'alignment'.

<tab> Uses editor tab settings to insert a tab at the beginning of the line (before the first non-space character), and inserts spaces otherwise.

<BS> Uses editor tab settings to delete tabs or 'expanded' tabs ala smarttab

To make Vim line up function arguments, add

set cindent
set cinoptions=(0,u0,U0

to .vimrc. The plugin will encode the whitespace as such:

int f(int x,
......int y) {
--->return g(x,
--->.........y);
}

This makes the alignment of "x" and "y" independent of the tab size (tabstop).

Marius Andersen