user2485337
user2485337

Reputation: 51

vim: smartindent (or cindent) is activated but does not work

Vim is not autoindenting the C source files I am working on, although it claims both the autoindent and cindent options are enabled when I type the :set command. Nothing is happening when I type in some code. For instance writing

int main()
{
return 0;
}

the "return 0;" statement stays on the left. However if I type the "=G" command, my file gets indented.

Here is my config:

vimrc is splitted into /etc/vim/vimrc and ~/.vimrc. The concatanated content is as follow:

runtime! debian.vim

if has("syntax")
  syntax on
endif

set background=dark

" Uncomment the following to have Vim jump to the last position when
" reopening a file
if has("autocmd")
  au BufReadPost * if line("'\"") > 1 && line("'\"") <= line("$") | exe "normal! g'\"" | endif
endif

if has("autocmd")
  filetype plugin indent on
endif

set showcmd
set showmatch

if filereadable("/etc/vim/vimrc.local")
  source /etc/vim/vimrc.local
endif

""""""" now this is ~/.vimrc """""

set runtimepath+=,/usr/share/vim-scripts

set autoindent
set noexpandtab                                                                 

" create ~<file> when saving modifications to <file>
set backup

" preserve source's format when pasting
set paste

" disable mouse usage
set mouse=

" colors
set t_Co=256
colorscheme mustang

set hlsearch

set number

set cursorline

if has("statusline")
   hi User1 ctermbg=red cterm=bold,reverse
   hi User2 ctermbg=darkblue cterm=bold,reverse
   hi User3 ctermbg=darkred cterm=bold,reverse
   hi User4 ctermbg=brown cterm=bold,reverse

   set laststatus=2
   set statusline=%h%f\ %y\ %1*%r%*%1*%m%*%=[col:%2*%c%*]\ [line:%3*%.6l%*/%4*%.6L%*\ -\ %p%%]
endif

set spellsuggest=5

match Error /\s\+$/

Do you have any idea ?

Thank you very much for your help.

Pierre

Upvotes: 3

Views: 5339

Answers (2)

romainl
romainl

Reputation: 196506

You should have read :help paste before adding set paste to your ~/.vimrc:

When the 'paste' option is switched on (also when it was already on):
    ... skipped ...
    - 'autoindent' is reset
    ... skipped ...
These options keep their value, but their effect is disabled:
    ... skipped ...
    - 'cindent'

'paste' is very toxic and should never be added to one's ~/.vimrc. See :help pastetoggle and/or use p instead.

Upvotes: 8

Abbasov Alexander
Abbasov Alexander

Reputation: 1938

Some information:

autoindent does nothing more than copy the indentation from the previous line, when starting a new line. It can be useful for structured text files, or when you want to control most of the indentation manually, without Vim interfering.

autoindent does not interfere with other indentation settings, and some file type based indentation scripts even enable it automatically.

smartindent automatically inserts one extra level of indentation in some cases, and works for C-like files. cindent is more customizable, but also more strict when it comes to syntax.

smartindent and cindent might interfere with file type based indentation, and should never be used in conjunction with it.

When it comes to C and C++, file type based indentations automatically sets cindent, and for that reason, there is no need to set cindent manually for such files. In these cases, the cinwords, cinkeys and cinoptions options still apply.

Generally, smartindent or cindent should only be set manually if you're not satisfied with how file type based indentation works.

If you plan on using file type based indentation, don't set smartindent or cindent. You may still set autoindent, since it doesn't interfere.

Upvotes: 5

Related Questions