Indentation is invalid on vim-8.0.1763 with `set autoindent` - vim

I have the following in my $HOME/.vimrc :
set autoindent
set expandtab
set shiftwidth=4
set softtabstop=4
However when I open C file, it seems indentation does not work, e.g.
int func(int a)
{ ...
Here I push ENTER after `{` but the cursor goes on the next line under `{`
I expect to have a cursor indented by 4 on the next line. If I set cindent, then it works as expected, but is it the right way? How do I make sure that indentation still works with other source files, e.g. python ?
Thanks.

Related

VIM ignores my tab-related settings in ~/.vimrc (and paste is not set)

Here's my ~/.vimrc on some machine:
set softtabstop=0 noexpandtab nosmarttab
set shiftwidth=4
set tabstop=4
colorscheme murphy
... but when I edit files, tabs show up as 8 spaces, and typing in a tab produces for spaces. I looked around and found this question:
set expandtab in .vimrc not taking effect
but I don't have set paste in my ~/.vimrc, so that can't be the problem. What's causing this? And how can I enforce my tab preferences?
Edit: It seems my settings are only ignored for some filetypes, not for simple text files. How can I force my settings to apply to all filetypes? Or at least to specific types?
Solution for a specific file type (rather than for all file types):
For file type foo Add the following to your ~/.vimrc:
augroup foo
autocmd!
autocmd FileType foo setlocal softtabstop=0 noexpandtab nosmarttab shiftwidth=4 tabstop=4
augroup END
and replace foo with the file type in both occurrences.
Note: foo is not necessarily a file extension. For example, the Python language (.py typically), file type is python.

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:
ubuntu 13.04
vim 7.3.547 + vim-scripts
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
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.
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.

Vim '=' does not work (CentOS 6)

When i try gg=G or something like that i get error:
:.,$!indent
shell returned 127
4 lines filtered
Press ENTER or type command to continue
On my Fedora destop it works fine. What's wrong?
config:
set autoindent
set tabstop=4
set shiftwidth=4
set number
set smartindent
set showmatch
You can unset equalprg option:
set ep=
When ep is empty, vim will use the internal formatting function.
You don't have the indent tool installed.
yum install indent

Why doesn't setting ts=4 in .vimrc work?

I have a stupid question but I am stuck.
I do set set ts=4 in my .vimrc file, but it looks not work. Open a new file, tab is still extended as 3 spaces. And under command mode, "set ts" got a "tabstop=3". Is there a final file like .vimrc that overwrite my ts?
Even I type :set ts=4, in vim, when I type tab, it only has 3 spaces in length.
Here is my suspicious options that cause this annoying result in my .vimrc
set autoindent
set shiftwidth=4
set softtabstop=4
set backspace=2
Thanks.
Try this:
:verbose set tabstop?
in vim, it will tell you where the tabstop option value is coming from.

Vim editor indent problem when the first character of the line is a sharp # character

This haven been bugging me since the first day using Vim for 3 years. Whenever I try to indent a line via Shift + > when the FIRST CHARACTER of the line starts with a "#", it doesn't work at all, regardless of the file types (.php, .txt, etc.). Because # is used for comment in PHP and I also use it for decoration for text files something like:
# This is a comment
### 1. Instruction one
# ------------ this is an sample --------------
I use Vim 7.2 in Ubuntu with the following .vimrc settings
syntax on
set t_Co=256
set incsearch
set hlsearch
set number
set nowrap
set nowrapscan
set ignorecase
set et
set sw=4
set smarttab
set smartindent
set autoindent
set textwidth=0
set noequalalways
set formatoptions=1
set lbr
set vb
set foldmethod=marker
Thanks!
Insert the following in your .vimrc:
set nosmartindent
It is smartindent that causes lines beginning with # to not be indented as you want. You can read more about it by typing :help smartindent. If you use an indenting file for python scripts (or any other syntax), include the following too.
filetype indent on
You can use:
inoremap # X^H#
I think this behaviour is not totally wrong for C/C++, therefore I just change it in python/php.
autocmd FileType python,php inoremap # X^H#
:help smartindent says:
When typing # as the first character in a new line, the indent for
that line is removed, the # is put in the first column. The indent
is restored for the next line.
If you don't want this, use this mapping: :inoremap # X^H#, where ^H is entered with CTRL-V CTRL-H.
When using the >> command, lines starting with # are not shifted right.

Resources