2

I don't know what vim cannot do, so far, I feel it is so powerful!

I need indent in Normal Mode for c++ code. I found vim is using "=" to that, just visual/mouse select the code and hit "=" key, or doing "gg=G" for all.

The following style is what I need (no indentation after a keyword). It might be ugly, but let's just don't discuss that. I'm using it as an example.

template< class T > class Stack
{
    public:
    Stack(int = default_value);
    Stack()//destructor
    {delete [] values;}
    bool push( T );
    T pop();
    bool isEmpty();
    bool isFull();
    private:
    int size;
    T *values;
    int index;
};

After "=" indenting, I got the following code

template< class T > class Stack
{
    public:
        Stack(int = default_value);
        Stack()//destructor
        {delete [] values;}
        bool push( T );
        T pop();
        bool isEmpty();
        bool isFull();
    private:
        int size;
        T *values;
        int index;

};

Question: Is there a way to configure/change the indentation style/schema behind the "=" operator or Is there a mechanism to load some sort of indentation schema into the "=" in vim?

Thanks

5

For your specific query (indentation of statements following a C++ scope declaration), you can use Vim's C-indenting and add the h setting to cinoptions:

" Use C-indenting
:set cindent

" Don't indent further after scope declaration
:set cinoptions+=h0

More generally, you can also use either an external program or Vimscript to configure the behaviour of the = command.

Use the 'equalprg' option to specify an external program or the 'indentexpr' option to specify a (Vimscript) indent expression.

For more details, see:

  • :help C-indenting
  • :help cinoptions
  • :help cino-h
  • :help =
  • :help 'equalprg'
  • :help 'indentexpr'
  • :help indent-expression

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.