Anti-pattern of vimrc
file encoding
In Vim script, :scriptencoding
exists to specify a file encoding.
Generally it should define :scriptencoding
when you use multibyte character.
When you don't use multibyte character, you don't have to define :scriptencoding
.
Therefore you have to use :scriptencoding
at the head of your vimrc.
Bad pattern
" use multibyte charactor. let g:indentLine_char = '|'
Good pattern
scriptencoding utf-8 let g:indentLine_char = '|'
:scriptencoding
and :set encoding
Vim can not recognize the character code of your vimrc when :scriptencoding
is defined before :set encoding
.
When writing :set encoding
, it should be described before :scriptencoding
.
Bad pattern
scriptencoding utf-8 set encoding=utf-8
Good pattern
set encoding=utf-8 scriptencoding utf-8
Abbreviate option
I often see a vimrc using an abbreviate option and the same strict option. It's a bad coding. You should use a strict option only.
Bad pattern(1)
" 'modeline' of abbreviation. set ml
Good pattern(1)
set modeline
Bad pattern(2)
augroup vimrc " 'autocmd' of abbreviation. au! au FileType vim * :set expandtab augroup END
Good pattern(2)
augroup vimrc autocmd! autocmd FileType vim * :set expandtab augroup END
If you don't know an abbreviate option, please you try :h ml
in your Vim.
And you will know a strict name of the abbreviate option.
*'modeline'* *'ml'* *'nomodeline'* *'noml'*
Scope of variable
I often see a vimrc using g:mapleader
and mapleader
.
It's a bad coding. You should use a variable with a scope.
Bad pattern(1)
let mapleader = ' '
Good pattern(1)
let g:mapleader = ' '
Bad pattern(2)
for i in range(0,2) execute printf('source .local_%d.vim', i) endfor
Good pattern(2)
for s:i in range(0,2) execute printf('source .local_%d.vim', s:i) endfor
When you use a variable without a scope outside a function, implicitly its scope is global scope(g:
).
Generally when you use a variable outside a function, you have to define it with script scope(s:
).
:autocmd
without belonged to groups
In Vim script, :autocmd
exists. Its syntax is following syntax.
:au[tocmd] [group] {event} {pat} [nested] {cmd}
:autocmd
can omit [group]. It is a bad coding and that makes your Vim heavy.
Bad pattern
autocmd FileType cpp setlocal expandtab autocmd FileType make setlocal noexpandtab
Good pattern
" define a group `vimrc` and initialize. augroup vimrc autocmd! augroup END " register autocmds to group `vimrc`. autocmd vimrc FileType cpp setlocal expandtab autocmd vimrc FileType make setlocal noexpandtab
Why is it heavy?
If you define autocmds without a group, your Vim registers the same autocmd each :source ~/.vimrc. And your Vim executes the same autocmds each occurring a Event(e.g. FileType). In one word, it's heavy.