How to add/maintain line break at end of file while saving in VIM

vim

Solution

I don't remember if there is solution for this build in vim. Check below snippet. It will add empty line at the end of file.

function! AddLastLine()
    if getline('$') !~ "^$"
        call append(line('$'), '')
    endif
endfunction

autocmd BufWritePre * call AddLastLine()

Problem

This feature is one of the concerns while editing others projects, editors like textmate and sublime I believe add line break for last line, but when I edit end portions of a file with vim and save it I sometimes forget to add the final line break. So, how do I automate this and make sure files always have at least one new line at the end.

Original source