3

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?

SOLUTION

  • 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/;$

divramod
  • 167

2 Answers2

2
nnoremap ;; :%s/;$//<CR>

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

nst0022
  • 486
  • 2
  • 5
0

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
Stack EG
  • 1,636