Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid namespace content indentation in vim?

How to set vim to not indent namespace content in C++?

namespace < identifier > {     < statement_list > // Unwanted indentation } 

Surprisingly, 'cinoptions' doesn't provide a way to edit namespace content indentation.

like image 972
freitass Avatar asked Mar 30 '10 21:03

freitass


People also ask

How do I turn off indent in vim?

Globally Disable Auto-Indentation by Disabling the Filevim files (eg. sh. vim ) who set indentexpr option (eg to GetShIndent() ).

Should I indent namespace?

The contents of namespaces should not be indented.


2 Answers

Not sure when it was introduced but my installed version of vim, v7.3.353 has a cino option that handles cpp namespace explicitly. I am currently using the example value:

cino=N-s

and as per :help cinoptions-values

NN    Indent inside C++ namespace N characters extra compared to a   normal block.  (default 0).  cino=                      cino=N-s    namespace {                namespace {       void function();       void function();   }                          }    namespace my               namespace my   {                          {       void function();       void function();   }                          } 

The link the OP posted is for v7.3.162

like image 147
mmlb Avatar answered Oct 05 '22 20:10

mmlb


cpp.vim will solve your problem, but if you don't want the full-blown Google coding style then just take a peek at the plugin source and see how it handles namespaces. It's super simple:

function! IndentNamespace()   let l:cline_num = line('.')   let l:pline_num = prevnonblank(l:cline_num - 1)   let l:pline = getline(l:pline_num)   let l:retv = cindent('.')   while l:pline =~# '\(^\s*{\s*\|^\s*//\|^\s*/\*\|\*/\s*$\)'     let l:pline_num = prevnonblank(l:pline_num - 1)     let l:pline = getline(l:pline_num)   endwhile   if l:pline =~# '^\s*namespace.*'     let l:retv = 0   endif   return l:retv endfunction  setlocal indentexpr=IndentNamespace() 

In essence all you do is match the last non-blank line against /^\s*namespace/, and if it matches return 0 (as the indent position for indentexpr); otherwise return Vim's builtin cindent mechanism's value.

I essentially stole the code from the plugin, stripped anything that isn't namespace-related and renamed the indent function to IndentNamespace(). Save this as ~/.vim/indent/cpp.vim.

like image 34
wilhelmtell Avatar answered Oct 05 '22 21:10

wilhelmtell