How to delete the newline characters at the end of every line in Vim?

ggVGJ

Broken down:

  1. gg: move to line 1;
  2. V: enter visual mode;
  3. G: move to last line;
  4. J: join selection to single line.

The most idiomatic way of joining all lines in a buffer is to use the :join command:

:%j!

(Remove the ! sign if you want Vim to insert space between joined lines. From the wording of the question I assume that you do not want to add spaces.)

The same could be done interactively, if you prefer:

ggVGgJ

(Again, use J instead of gJ if you want to separate joined lines with spaces.)

Another option is the :substitute command:

:%s/\n//

Why use Vim? You could easily use the following shell command:

tr -d '\n' < file

Tags:

Vim