0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

vim で cpp ファイルで cindent が常に設定されてしまうのを直すメモ

Last updated at Posted at 2022-08-06

環境

vim 8.2(Ubuntu 22.04)
(vim 8.1 では起きないかもしれません)

背景

~/.vimrc

set nocp
set indentexpr=
set noautoindent
set nocindent                                                                                                                               set nosmartindent                                                                                                                             

filetype indent off 

au! BufNewFile,BufRead *.cc setl ts=2 sw=2 expandtab nocindent noautoindent nosmartindent                                                   au! BufNewFile,BufRead *.cpp setl ts=2 sw=2 expandtab nocindent noautoindent nosmartinden

としても, cpp ファイル(filetype=cpp と判定される)開くと常に cindent が設定され, 勝手にインデントされつらい... :cry:
(ts は設定変えると反映されるのに...)

原因

vim -V (verbose レベルを変えることができるが, デフォルトの 10 で十分)で cindent の設定を追うと,

...
autocommand setl ts=2 sw=2 expandtab nocindent noautoindent nosmartindent
Executing BufRead Autocommands for "*.cc"
autocommand if exists("cynlib_syntax_for_cc")|setf cynlib|else|setf cpp|endif
Executing FileType Autocommands for "*"
autocommand exe "set syntax=" . expand("<amatch>")
Executing Syntax Autocommands for "*"
autocommand call s:SynSet()
chdir(/usr/share/vim/vim82/syntax)
fchdir() to previous dir
line 25: sourcing "/usr/share/vim/vim82/syntax/cpp.vim"
chdir(/usr/share/vim/vim82/syntax)
fchdir() to previous dir
line 16: sourcing "/usr/share/vim/vim82/syntax/c.vim"
finished sourcing /usr/share/vim/vim82/syntax/c.vim
continuing in /usr/share/vim/vim82/syntax/cpp.vim
finished sourcing /usr/share/vim/vim82/syntax/cpp.vim
continuing in <SNR>4_SynSet
Executing Syntax Autocommands for "cpp"
autocommand if (exists('b:load_doxygen_syntax') && b:load_doxygen_syntax)^I|| (exists('g:load_doxygen_syntax') && g:load_doxygen_syntax)   | runtime! syntax/doxygen.vim | endif
Executing FileType Autocommands for "*"
autocommand call s:LoadFTPlugin()
chdir(/usr/share/vim/vim82/ftplugin)
fchdir() to previous dir
line 17: sourcing "/usr/share/vim/vim82/ftplugin/cpp.vim"
chdir(/usr/share/vim/vim82/ftplugin)
fchdir() to previous dir
line 12: sourcing "/usr/share/vim/vim82/ftplugin/c.vim"
finished sourcing /usr/share/vim/vim82/ftplugin/c.vim
continuing in /usr/share/vim/vim82/ftplugin/cpp.vim
finished sourcing /usr/share/vim/vim82/ftplugin/cpp.vim
continuing in <SNR>14_LoadFTPlugin
Executing FileType Autocommands for "*"
autocommand call s:LoadIndent()
chdir(/usr/share/vim/vim82/indent)
fchdir() to previous dir
line 14: sourcing "/usr/share/vim/vim82/indent/cpp.vim"
finished sourcing /usr/share/vim/vim82/indent/cpp.vim
...

autocomand BufRead の設定したあとに, /usr/share/vim/vim82/indent/cpp.vim で cindent が上書きされていました...
(setl を set にしても同様)

" Only load this indent file when no other was loaded.
if exists("b:did_indent")
   finish
endif
let b:did_indent = 1

" C++ indenting is built-in, thus this is very simple
setlocal cindent

解決 BufEnter も追加する

au!BufEnter も必要でした!
(BufEnter だけでいいかも)

au! BufNewFile,BufRead,BufEnter *.cc setl ts=2 sw=2 expandtab nocindent noautoindent nosmartindent                                          au! BufNewFile,BufRead,BufEnter *.cpp setl ts=2 sw=2 expandtab nocindent noautoindent nosmartindent 
...
Executing FileType Autocommands for "*"
autocommand call s:LoadIndent()
chdir(/usr/share/vim/vim82/indent)
fchdir() to previous dir
line 14: sourcing "/usr/share/vim/vim82/indent/cpp.vim"
finished sourcing /usr/share/vim/vim82/indent/cpp.vim
continuing in <SNR>15_LoadIndent
Executing BufRead Autocommands for "*"
autocommand if !did_filetype() && expand("<amatch>") !~ g:ft_ignore_pat | runtime! scripts.vim | endif
Executing BufRead Autocommands for "*"
autocommand if !did_filetype() && expand("<amatch>") !~ g:ft_ignore_pat    && (getline(1) =~ '^#' || getline(2) =~ '^#' || getline(3) =~ '^#'^I|| getline(4) =~ '^#' || getline(5) =~ '^#') |   setf FALLBACK conf | endif
Executing BufEnter Autocommands for "*.cc"
autocommand setl ts=2 sw=2 expandtab nocindent noautoindent nosmartindent
Executing BufEnter Autocommands for "*"
...

ident の設定を読み込んだあとに BufEnter で設定上書きしてくれました.
ややこしいですね...

バグ?

今回は .cc ファイルで filetype plugin が起動して indent ファイルを読んだことにより起こった... はず?

filetype indent off(filetype indent plugin off でも)だと, indent ファイルは読み込まないとあるのですけどね...
`

TODO

  • .vimrc でグローバルかつトップレベルで強制的に nocindent する方法を探す旅に出たい 🥺
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?