" マジックコメント自動追加関数
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>