Skip to content

Instantly share code, notes, and snippets.

@thenoseman
Created December 28, 2011 13:21
Show Gist options
  • Save thenoseman/1527916 to your computer and use it in GitHub Desktop.
Save thenoseman/1527916 to your computer and use it in GitHub Desktop.
Automatically adds the utf-8 magic comment to ruby files
" Adds the ruby magic comment to file
" Ignores SheBang-ed files
fun! AddMagicEncodingComment()
let line = getline(1)
let pos = getpos(".")
exec "normal gg"
if(match(line,"#!")) > -1
let line = getline(2)
exec "normal j"
endif
if(match(line,"encoding")) == -1
exec "normal O# -*- encoding : utf-8 -*-"
if pos[1] == 1
" new file:
call setpos(".", [pos[0], 2, pos[2], pos[3]])
else
" existing file
call setpos(".", pos)
endif
endif
endfun
au FileType ruby call AddMagicEncodingComment()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment