How do I delete the last character in each line using vim

Solution 1:

You could use :%s/.\{1}$// to delete 1 character off the end of each line.

Solution 2:

:%normal $x

  • : puts you into command line mode
  • % is a range representing the entire file
  • normal says we're running a normal mode command
  • $x deletes the last character in the line

Some overlap with other answers, but to me this one reads most simply.

Solution 3:

TLDR: :%s/.$//

For explanation and more examples read on.

  • :%s/.$//
    • deletes any character at the end of all lines (or better to say: replaces any character at the end with nothing)
    • s = substitute command
    • . = any character
    • if you want to delete . (not any character) use \.$ instead of .$
  • :'<,'>s/.$//g
    • deletes any character at the end of all lines of the current selection
    • '<,'> = current selection (you get this by SHIFT+V, then selecting the lines, then :)
  • :%s/;$//
    • deletes ; at the end of all line
    • useful if you copy & paste code from a language that needs ; to one that doesn't