i am searching for a substitution command in vim for removing every semicolon in a file, if it is the last character of the line.

i already found this command, which toggles a semicolon at the end of the line:

nnoremap ;; :s/\v(.)$/\=submatch(1)==';' ? '' : submatch(1).';'<CR>

Can someone help me out here?


  • the command i was searching for, was part of a little greater cleanup i want to do after saving a file. the complete solution looks like that

" greenkeeping files " consecutive blank lines: https://unix.stackexchange.com/questions/12812/replacing-multiple-blank-lines-with-a-single-blank-line-in-vim-sed " whitespaces: https://stackoverflow.com/questions/356126/how-can-you-automatically-remove-trailing-whitespace-in-vim " last empty line: https://stackoverflow.com/questions/7495932/how-can-i-trim-blank-lines-at-the-end-of-file-in-vim " last semicolon: https://unix.stackexchange.com/posts/462443 autocmd BufWritePre *.test silent! :%s/\s\+$//e | silent! :%s/\(\n\n\)\n\+/\1/ | silent! :%s#\($\n\s*\)\+\%$## | :%s/;$

  • 3
    Do you mean just :%s/;$/, or is the keymapping in your example important for you? – Michael Homer Aug 14 '18 at 6:51
  • @MichaelHomer are you not missing a second / at the end? – Bananguin Aug 14 '18 at 7:13
  • @Bananguin - that second /is redundant... as is the current actually. :%s/;$ works also. – Lieven Keersmaekers Aug 14 '18 at 10:08
  • the mapping wasnt important. :%s/;$ is doing exactly what i want! thx! – divramod Aug 15 '18 at 4:29
nnoremap ;; :%s/;$//<CR>

where pressing ;; in normal mode substitutes all ; at line end (;$) with nothing (//)


The below command will remove the semicolon if it is the last character of the line.

Try the below command using sed,

sed 's/;*$//g' <file1> file2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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