My development team decided to move to typescript and one of the style guidelines was to remove all semi-colons and they will probably do so in newer JavaScript files (I know its a bit petty) but yeah, that's what the team has decided.

Unfortunately, they don't use vim and I do because I love it. They don't fuss about what editors you use as long as you keep to the guidelines.


After years of programming and hitting ; at the end of each line, it's hard to break this habit.


I want to know if there is a nice way in vim to allow me to remove semicolons at the end of each line when I hit save?

Ideally I want to have a way to bind keys to trim all semicolons or when I hit :wq to save a file.

Is there already a plugin that does this?

1 Answer 1


Doing a search/replace on save is not too difficult, but ... is it also a good idea? Personally, I'm not a big fan of doing search-replace operations on save, as there almost always tend to be exceptions and unexpected behaviour. e.g. consider having a ; in a comment (possibly at the end of the line)?

My recommendation is to just highlight semicolons as errors. You'll get out of the habit of adding them soon enough anyway.

For example:

augroup typescript
    autocmd FileType typescript
        \  syn match semicolonError /;$/ display
        \| hi def link semicolonError Error
augroup end

Or, if you prefer, you can put it in ~/.vim/after/syntax/typescript.vim.

See :help syntax and this answer for more information on how syntax highlighting works.

If you really want to search/replace them on save, you can use the answer in What's the simplest way to strip trailing whitespace from all lines in a file? with a slightly different regexp:

fun! TrimSemicolon()
    let l:save = winsaveview()
    call winrestview(l:save)

" Automatically run on save
augroup typescript
    autocmd BufWritePre * :call TrimSemicolon()
augroup end

" Map it to a key
nnorenamap <Leader>s call :TrimSemicolon()

See my other answer for a more detailed explanation. :help autocmd.txt and the Autocommands chapter of Learn Vimscript the Hard Way are also recommended reading :-)


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.