tags:

views:

143

answers:

4

Is there a way to have hanging operators when indenting with tabs in Vim? For instance, if I have code:

class some_class
{
<tab>some_class();
<tab>~some_class();
};

I want it to look like this:

class some_class
{
        some_class();
       ~some_class();
};

The question is about the appearance of whitespace, not its composition. So, abbreviations and indentation rules would not do.

A: 

I'm not sure I understand the concept of "hanging operator" but it seems you would like to substitute your tabs with spaces.

You can set the expandtab and tabstop options:

:set expandtab
:set tabstop=8

That will expand any new tab characters you type into 8 spaces.

To apply the tab-expansion to existing tabs in your file, use the retab command:

:retab
pythonquick
No, I do not want to substitute tabs with spaces. Rather, I want a better visual representation of tabs. There are many instances when this would make code more readable. The term comes from typography: http://en.wikipedia.org/wiki/Hanging_punctuation
Don Reba
+3  A: 

I think he's asking if the ~ will move backwards one space if vim can detect that it's an operator, thereby "hanging" the ~ to the left of the some_class().

The answer as far as I know is it might be possible, but won't be easy. Insert-mode abbreviations are always patchy, and more complex solutions are...well more complex.

Unless you really need this behaviour, I don't think it's worth the effort.

EDIT:

I think I get what you're saying. In which case I can say with reasonable certainty that what you're asking is not possible. You cannot change how vim displays text that is indented by tabs and happens to start with an operator that you want hanging off the left margin. That simply will not work. Vim is set up to display text as-is, you can't change it to suit a typographical style without changing the source code, which I doubt you'll want to do.

sykora
Just to reiterate, the question is about indentation with tabs, not spaces. Besides, abbreviations can't condition on whitespace.
Don Reba
I think I understand a bit better, I'll update my answer.
sykora
Well. There is 'tabstop'. But, I strongly suspect you are right.
Don Reba
A: 

It seems like you would have to write your own indent file for this, examples of which are in $VIMRUNTIME/indent. But like sykora says, that probably is not worth the effort.

Peter van der Heijden
If I understand correctly, indenting rules determine which characters to use for indentation, but have nothing to do with their appearance.
Don Reba
That's correct. I either missed your remark about just wanting the text appear indented or it wasn't there when I replied.
Peter van der Heijden
A: 

I think what you want is called "autoindent". See :help ai

'autoindent' 'ai'   boolean (default off)
      local to buffer
    Copy indent from current line when starting a new line (typing <CR>
    in Insert mode or when using the "o" or "O" command).  If you do not
    type anything on the new line except <BS> or CTRL-D and then type
    <Esc>, CTRL-O or <CR>, the indent is deleted again.  Moving the cursor
    to another line has the same effect, unless the 'I' flag is included
    in 'cpoptions'.
    When autoindent is on, formatting (with the "gq" command or when you
    reach 'textwidth' in Insert mode) uses the indentation of the first
    line.
    When 'smartindent' or 'cindent' is on the indent is changed in
    a different way.
    The 'autoindent' option is reset when the 'paste' option is set.
    {small difference from Vi: After the indent is deleted when typing
    <Esc> or <CR>, the cursor position when moving up or down is after the
    deleted indent; Vi puts the cursor somewhere in the deleted indent}.
Brad Cox
What I am asking has more to do with things like 'tabstop', which affect appearance of indentation.
Don Reba