Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim indents C/C++ functions badly when the type and name are in different lines

The following code snippets explain my problem well.

What I want:

template<class T>
ostream& operator<<(ostream& out, const vector<T>& v)
{
    ...
}

int
main()
{
    ...
}

What I get (notice the over-indentation before the function name):

    template<class T>
ostream& operator<<(ostream& out, const vector<T>& v)
{
    ...
}

    int
main()
{
    ...
}

I have set filetype plugin indent on in my ~/.vimrc.

I have looked at this post but the answer in that looks like learning a new programming language. I am a vim fan, but not a vim expert. Isn't there a simpler solution?

like image 995
munikarmanish Avatar asked Jan 29 '15 14:01

munikarmanish


People also ask

How do I turn off auto indent in Vim?

vim" in 'runtimepath'. This disables auto-indenting for files you will open. It will keep working in already opened files. Reset 'autoindent', 'cindent', 'smartindent' and/or 'indentexpr' to disable indenting in an opened file.

How do I indent multiple lines in Vim?

To tab or add the indentation at multiple lines, try “shift+dot” i.e., “.” Shortcut once. You will see it will add an indentation of one character at each selected line from the start. If you want to add indentation without stopping, then you have to try the “.” Key from the keyword after using “shift+.”.

Does Vim have auto indentation?

How to Turn On Auto Indent in Vim. To automatically indent when editing a file in Vim, enable the auto indenting feature using the :set autoindent flag in command mode: Press Enter, and this will auto-indent the file you are currently editing.

How do I indent in Vim?

I like having auto on, but smart does funny things based on keywords. To indent the current line, or a visual block: ctrl-t, ctrl-d - indent current line forward, backwards (insert mode) visual > or < - indent block by sw (repeat with . ) then try hitting the F5 key while in insert mode (or just :set paste ).


2 Answers

What you are seeing is the effect of cino-t (cinoptions setting t). You need to make sure conniptions contains t0 to make the parameter flush with the left margin

From :h cino-t

                                            cino-t
    tN    Indent a function return type declaration N characters from the
          margin.  (default 'shiftwidth').

            cino=               cino=t0             cino=t7
                  int             int                        int
              func()              func()              func()

To do this you need to make sure that it is set for the cpp filetype. (cindent is turned on by the default cpp indent file)

I think just adding set cinoptions+=t0 to your vimrc should be sufficient.

like image 65
FDinoff Avatar answered Oct 11 '22 17:10

FDinoff


Just as I guessed, this had a pretty simple solution! After motivating myself to read the :help 'cinoptions-values', the following configuration was all that's needed to solve this particular problem.

:set cino+=t0

From the help text:

tN    Indent a function return type declaration N characters from the
      margin.  (default 'shiftwidth').

        cino=               cino=t0             cino=t7
              int             int                        int
          func()              func()              func()
like image 27
munikarmanish Avatar answered Oct 11 '22 17:10

munikarmanish