1

I would like to indent everything in vim with tabs, except a particular case. For example I have this c++ code(where <tab> is a tab character series and <s> is a space character series):

<tab>if(true &&
<tab><s>true)
<tab>{
<tab><tab>//code here
<tab>}

I would like after writing '&&' and press 'o' to jump on the next line and start writing to make vim put a tab and the number of spaces till '(' from the line before.

Is it possible to define this coding style in vim?

Thanks!

William Pursell
  • 204,365
  • 48
  • 270
  • 300
Jacob Krieg
  • 2,834
  • 15
  • 68
  • 140

2 Answers2

5

I think what you're looking for is the (N option for cinoptions. Try set cinoptions+=(0. According to the documentation, this looks like the alignment that you seek.

More information can be found by using help command: :help cinoptions-values or looking at the online version of the help for cinoptions-values.

As far as tabs go, you'll want to disable expandtab with :set noexpandtab, and you'll want to make sure your tabstops, soft tabstops, and shiftwidth are all set accordingly. As an example, the Linux source code uses a style like you mention above, and I have this in my vimrc:

setlocal ts=8 sts=8 sw=8 tw=80

" Don't expand tabs to spaces.
setlocal noexpandtab

" Enable automatic C program indenting.
setlocal cindent

" Don't outdent function return types.
setlocal cinoptions+=t0

" No extra indentation for case labels.
setlocal cinoptions+=:0

" No extra indentation for "public", "protected", "private" labels.
setlocal cinoptions+=g0

" Line up function args.
setlocal cinoptions+=(0

" Setup formatoptions:
"   c - auto-wrap comments to textwidth.
"   r - automatically insert comment leader when pressing <Enter>.
"   o - automatically insert comment leader after 'o' or 'O'.
"   q - allow formatting of comments with 'gq'.
"   l - long lines are not broken in insert mode.
"   n - recognize numbered lists.
"   t - autowrap using textwidth,
setlocal formatoptions=croqlnt
John Szakmeister
  • 44,691
  • 9
  • 89
  • 79
0

Add following in your .vimrc

set tabstop=2
set expandtab
set shiftwidth=2
set smarttab
set linebreak
set smartindent
set cindent
set autoindent

This is all you need to roll out awesomeness in vim. :)

kiddorails
  • 12,961
  • 2
  • 32
  • 41
  • is it just me or this indents with space everywhere? should i show you my vimrc? also is it possible for it to work with other c++ blocks like while or for? – Jacob Krieg Mar 31 '13 at 11:58
  • The `expandtab` is telling Vim to use spaces instead of tabs; try `noexpandtab` instead. Also, when `cindent` is on, calling `smartindent` has no effect so that call can be removed. – Matthew Strawbridge Mar 31 '13 at 12:22
  • @MatthewStrawbridge: I apparently focused on getting the resultant spacing as John required. – kiddorails Mar 31 '13 at 12:26
  • @kiddorails FWIW, I didn't downvote this answer; it's not a million miles from what the OP wanted but does need tweaking – Matthew Strawbridge Mar 31 '13 at 12:31
  • No it not just you. He simply copied you his config without even trying to answer your question. – Karoly Horvath Mar 31 '13 at 12:47
  • @KarolyHorvath: agreed. I didn't know what `expandtab` was for. But, do know about others. Since OP asked for the indentation settings he needed for his vi, I offered him the indentation settings that perfectly works for me. :) – kiddorails Mar 31 '13 at 15:20
  • +1 I'm going to read the manual for each property. Thanks for your efforts! – Jacob Krieg Mar 31 '13 at 15:47