Vim Tips Wiki

Indent C blocks where braces are opened and closed on a single line

1,624pages on
this wiki
Add New Page
Talk0 Share
Tip 1620 Printable Monobook Previous Next

created 2009 · complexity basic · author Sightless · version 7.0

This snippet, which should be put in ~/.vim/indent/c.vim to work, gives indentation like this:

for (;;)
    { do_something(); }

rather than having the contents of the loop at the same indent level as the 'for' keyword.

func! MyCindent(l)
  let extra = match(getline(a:l),'^\s*{.*}$')==-1?0:&sw
  return cindent(a:l)+extra
setlocal indentexpr=MyCindent(v:lnum)
setlocal cinkeys+=}
let &l:indentkeys=&l:cinkeys


Ad blocker interference detected!

Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.

Also on Fandom

Random Wiki