2

I want to reindent an nginx configuration file with the following simple rudimentary rules:

  • a { increases indentation
  • a } decreases indentation

The idea is cindent is just enough to do it. I have the following file:

include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}

I run vim -u NONE ./thefile.conf and type gg=G to apply = as an re-indenting operator to the whole file and vim indents the whole file by aligning the data below * to the same column:

include /usr/share/nginx/modules/*.conf;
                                  events {
                                  worker_connections 1024;
                                  }

On the other hand, if I remove the * star character from the first line, doing = operator on the whole file re-indents the whole file just fine:

include /usr/share/nginx/modules/.conf;
events {
    worker_connections 1024;
}

Removing star characters from the file fixes it - I can indent big files that way. How to tell vim = command not to increase indentation on * character when re-indenting the file? Why does * character break the indentation when using the = command?

Also, only /* breaks the indentation. Doing / * or /something* does not break it and indentation seem to work fine. Why? I tried to test other characters like & ^ instead of *, and they didn't have the same effect as *.

3
  • 3
    Presumably because /* is the beginning of a comment in some languages. What file types are affected the way you describe? – B Layer Feb 2 at 15:14
  • 1
    Suggested reading :h format-comments. It suggests that indentexpr may be better than cindent at dealing with three piece comments (which /* is part of, i.e. the first piece). 'fo' may also be involved here. What do you have for that setting? – B Layer Feb 2 at 15:23
  • Oooooch, right, how didn't I think of that.... The filetype is not set, it's empty, normal vim detects ft=conf. I will try :set comments= to something simple, thank you! – KamilCuk Feb 2 at 15:24
1

As Matt notes, smartindent is enough. Not only this, but cindent is actively harmful!

Many years ago, when I first got started with vim, I had these both set globally in my vimrc. Eventually, through spurious indenting issues such as these, I came to realize that in fact they make bad global settings. cindent is too specific; it works for C stuff. Don't set that globally. And smartindent is better, but for C-like programs! (Basically, around braces, and some hacks with cinwords.)

In the end, it is better to remove these local indent options from your global config, and relegate them to filetype-specific areas (e.g., ftplugins).

On the other hand, autoindent is a fine global indent option, as it often does what you want and stays out of the way when other options are set.

1
  • 1
    That's exactly what I have in my default/global config. Just 'ai'. – B Layer Feb 2 at 22:42
0

There's no need in cindent, as smartindent should be enough. So simply:

setlocal smartindent cinwords=
2
  • Well, so I did set smartindent cindent cinwords= and copied my file into empty file with unset filetype and got same result. Is lisp indenting kicking in then? The problem is smartindent works when you type - ie. I type { and it indents next line - this is great! But I want to re-indent (with =) an existing file - smartindent is not for that. Sorry for confusion. – KamilCuk Feb 2 at 19:46
  • @KamilCuk Have you read :h =? Smartindent is not about it. If you believe you need "equal" too you can write indentexpr of your own. – Matt Feb 2 at 20:32

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.