Skip to content

Instantly share code, notes, and snippets.

@andresaquino
Last active May 3, 2018 13:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andresaquino/379e9deee06cb320aa01eb989b0e7151 to your computer and use it in GitHub Desktop.
Save andresaquino/379e9deee06cb320aa01eb989b0e7151 to your computer and use it in GitHub Desktop.
VIM Plugin para establecer los headers en scripts (bash, ruby, python, perl, vim, php, js, c/c++) de forma automática.
" Vim File
" vim:si:ai:et:ts=3:sw=3:ft=vim
" (c) 2018, Andres Aquino <inbox@andresaquino.sh>
" This file is licensed under the BSD License version 3 or later.
" See the LICENSE file.
"
" Based on work of Agapo (fpmarias@google.com), /usr/share/vim/vim70/plugin/header.vim
" LastModf: 20180326.034630
""
function s:filetype()
"
set ts=3 sw=3 et si ai
let s:options = "ts=3:sw=3:et:si:ai"
let s:fn = expand("%:t")
let s:ft = expand("%:e")
"
if s:ft ==# 'sh'
let s:comment = "#"
let s:header = s:comment . "!/usr/bin/env bash"
let s:footer = s:comment
elseif s:ft ==# 'ruby'
let s:comment = "#"
let s:header = s:comment . "!/usr/bin/env ruby"
let s:footer = s:comment
elseif s:ft ==# 'python'
let s:comment = "#"
let s:header = s:comment . "!/usr/bin/env python"
let s:footer = s:comment
elseif s:ft ==# 'perl'
let s:comment = "#"
let s:header = s:comment . "!/usr/bin/env perl"
let s:footer = s:comment
elseif s:ft ==# 'vim'
let s:comment = "\""
let s:header = s:comment . " Vim File"
let s:footer = s:comment
elseif s:ft==# 'php'
let s:comment = "\/\/"
let s:header = "<?PHP"
let s:footer = "?>"
elseif s:ft ==# 'js' || s:ft ==# 'javascript' || s:ft ==# 'javascript.jsx'
let s:comment = "\/\/"
let s:header = s:comment . " Javascript File"
let s:footer = s:comment
elseif s:ft ==# 'c' || s:ft ==# 'cpp'
let s:comment = "\/\/"
let s:header = s:comment . " C/C++ File"
let s:footer = s:comment
elseif s:ft ==# 'md' || s:ft ==# 'markdown'
let s:comment = "#"
let s:header = s:comment . " Markdown File"
let s:footer = s:comment
let s:options = "ts=3:sw=3:si:ai:noet:list"
set ts=3 sw=3 si ai noet list
elseif s:ft ==# 'htm' || s:ft ==# 'html'
let s:comment = ""
let s:header = "<!-- HTML File"
let s:footer = "-->"
let s:options = "ts=3:sw=3:si:ai:noet:list"
set ts=3 sw=3 si ai noet list
elseif s:ft ==# 'css'
let s:comment = "*"
let s:header = "/* CSS File"
let s:footer = "*/"
let s:options = "ts=3:sw=3:si:ai:noet:list"
set ts=3 sw=3 si ai noet list
elseif s:ft ==# 'cnf' || s:ft ==# 'conf' || s:ft ==# 'yaml' || s:ft ==# 'yml'
let s:comment = "#"
let s:header = s:comment . " " . s:fn . " (" . s:ft . ") "
let s:footer = s:comment
let s:options = "ts=3:sw=3:si:ai:noet:list"
set ts=3 sw=3 si ai noet list
else
let s:comment = "#"
let s:header = s:comment . " Text File"
let s:footer = s:comment
endif
endfunction
""
function s:insert()
"
call s:filetype()
"
set comments=
execute "normal i" . s:header . "\n"
execute "normal i" . s:comment . " vim:" . s:options . "\n"
execute "normal i" . s:comment . " \n"
execute "normal i" . s:comment . " (c) " . strftime("%Y") . ", " . expand("$U_NAME") . " <" . expand("$U_MAIL") . ">\n"
execute "normal i" . s:comment . " This file is licensed under the BSD License version 3 or later.\n"
execute "normal i" . s:comment . " See the LICENSE file.\n"
execute "normal i" . s:comment . " \n"
execute "normal i" . s:comment . " LastModf: " . strftime ("%Y%m%d.%H%M") . "\n"
execute "normal i" . s:footer . "\n"
execute "normal i \n"
execute "normal i \n"
execute "normal i \n"
unlet s:comment
unlet s:header
unlet s:footer
endfunction
""
function s:update()
"
call s:filetype()
"
call cursor(0,0)
let s:pattern = s:comment . " LastModf: [0-9]"
let [s:lnum, s:col] = searchpos(s:pattern, "n")
let s:modified = s:comment . " LastModf: " . strftime ("%Y%m%d.%H%M")
if s:lnum != 0
call setline(s:lnum, s:modified)
endif
unlet s:comment
unlet s:header
unlet s:footer
unlet s:pattern
unlet s:modified
endfunction
autocmd BufNewFile * call s:insert()
autocmd BufWritePre * call s:update()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment