简体   繁体   中英

vim: autoindent not working

my autoindent is not working, any diagnostic tests to figure it out?

my ":set" is:

:set --- Options --- cindent laststatus=2 scroll=17
tabstop=4 window=36
filetype=cpp number
smartindent ttyfast
helplang=en paste
syntax=cpp ttymouse=xterm2
backspace=indent,eol,start
fileencoding=utf-8
fileencodings=ucs-bom,utf-8,default,latin1 printoptions=paper:letter
runtimepath=~/.vim,/var/lib/vim/addons,/usr/share/vim/vimfiles,/usr/share/vim/vim72,/usr/share/vim/vimfiles/af ter,/var/lib/vim/addons/after,~/.vim/after suffixes=.bak,~,.swp,.o,.info,.aux,.log,.dvi,.bbl,.blg,.brf,.cb,.ind,.idx,.ilg,.inx,.out,.toc

try:

:set ai

or:

:set autoindent

find more about auto-indent:

:h ai

Otherwise, it's might be something with file type detection.

I had a stale function in indentexpr which persisted after changing the filetype. This eventually fixed it for me:

:set indentexpr=

In case someone else face the same issue, I had a similar issue that none of the above fixed. What was wrong for me was the tab interpretations. here is the set up that made it work:

set expandtab
set tabstop=2
set shiftwidth=2
set autoindent
set smartindent

And to check when indenting if the indentation was correct, I added the following, still in my vimrc file:

" helper for indent mistake
set list listchars=tab:»·,trail:·

Which display a "»" instead of the regular "·" if my indent is wrong. Very handy. Hope it helps.

I had the same issue and these settings fixed it.

filetype on
filetype plugin on
filetype indent on

您可能应该关闭smartindent并使用:filetype indent oncindent (似乎也已设置)。

Here's one way to test out whether you have the configuration correct, then persist your configuration so Vim always operates thusly. This font indicates text which should be typed in literally, except <CR> means press the "Enter" or "Return" key.

  • Create a new system user, with a new home directory.
  • Start Vim. All settings should be set however they ship with Vim by default.
  • Open a file, say, test.txt.
  • Make sure autoindent is enabled ( :set ai?<CR> )
  • Prove that autoindent doesn't happen:
    • Type a space or two, then hit enter.
    • When the cursor advances to the next line, it should return to column 1, the far-left column.
  • Turn on autoindent ( :set ai<CR> )
  • Make sure autoindent is enabled ( :set ai?<CR> )
  • Prove that autoindent happens:
    • Type a space or two, then hit enter.
    • When the cursor advances to the next line, it should still be in the same column.
  • Persist autoindent with :mkvimrc<CR> .

Hope that helps! Here are some other notes:

  • These instructions might be specific to left-to-right locales.
  • Here's my vimrc

"The 'autoindent' option is reset when the 'paste' option is set". So try to remove 'paste' from your settings ( vim-options ).

I had the same problem, and I have tried many commands, all failed.
At last, I use the following command, and it works:
autocmd VimEnter * set autoindent
It's not a elegant method, however, it does works.

:set smartindent? showd: nosmartindent

Then activating it with :set smartindent solved problem for me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM