3

Vim 不会自动缩进我正在处理的 C 源文件,尽管它声称在我键入 :set 命令时同时启用了 autoindent 和 cindent 选项。当我输入一些代码时什么都没有发生。例如写作

int main()
{
return 0;
}

“返回 0;” 语句留在左边。但是,如果我键入“=G”命令,我的文件会缩进。

这是我的配置:

  • Ubuntu 13.04
  • vim 7.3.547 + vim 脚本

vimrc 分为 /etc/vim/vimrc 和 ~/.vimrc。拼接内容如下:

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\+$/

你有什么主意吗 ?

非常感谢您的帮助。

皮埃尔

4

2 回答 2

7

:help paste在添加set paste到您的之前,您应该阅读~/.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'非常有毒的,应该添加到一个人的~/.vimrc。请参阅:help pastetoggle和/或p改用。

于 2013-06-14T09:14:13.200 回答
3

一些信息:

autoindent在开始新行时,无非是从上一行复制缩进。它对结构化文本文件很有用,或者当你想手动控制大部分缩进而不用 Vim 干扰时。

autoindent不会干扰其他缩进设置,并且某些基于文件类型的缩进脚本甚至会自动启用它。

smartindent在某些情况下会自动插入一层额外的缩进,并且适用于类 C 文件。cindent更可定制,但在语法方面也更严格。

smartindent并且cindent可能会干扰基于文件类型的缩进,并且不应该与它一起使用。

对于 C 和 C++,基于文件类型的缩进会自动设置cindent,因此无需cindent手动设置此类文件。在这些情况下cinwordscinkeyscinoptions选项仍然适用。

通常,smartindent或者cindent仅当您对基于文件类型的缩进的工作方式不满意时才应手动设置。

如果您打算使用基于文件类型的缩进,请不要设置smartindentcindent. 您仍然可以设置autoindent,因为它不会干扰。

于 2013-06-14T09:12:18.593 回答