简体   繁体   中英

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

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.

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM