Skip to content

Instantly share code, notes, and snippets.

@pinzolo
Created September 19, 2012 23:09
Show Gist options
  • Save pinzolo/3752938 to your computer and use it in GitHub Desktop.
Save pinzolo/3752938 to your computer and use it in GitHub Desktop.
保存時にマジックコメントがなければ挿入する.vimrc設定
" マジックコメント自動追加関数
function! AddMagicComment()
let pos = getpos('.')
let line_index = 1
let magic_comment = '# coding: utf-8'
if &filetype == 'ruby'
let line = getline(line_index)
if line[0:1] == '#!'
let line_index = 2
endif
elseif &filetype == 'haml'
let magic_comment = '-'.magic_comment
elseif &filetype == 'eruby'
let magic_comment = '<%'.magic_comment.' %>'
endif
let line = getline(line_index)
if line =~ 'coding:'
return
endif
call cursor(line_index, 0)
execute ':normal O'.magic_comment
call setpos('.', pos)
endfunction
autocmd BufWritePre *.rb,*.erb,*.haml call AddMagicComment()<CR>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment