9

I have a huge file that I would like to format using Vim. For that, I would like to delete the newline characters at the end of every line.

For example,

I
want
this
to be
just one
line

should become

I want this to be just one line

I was thinking of doing it via the following command:

:%g/^/norm!‹a keyword that deletes the \n›

but I just don't know which keyword might work for that, to automate the pressing the Del key on every on every line.

Thanks in advance!

ib.
  • 25,324
  • 10
  • 71
  • 97
Hassek
  • 8,044
  • 6
  • 40
  • 54
  • [http://stackoverflow.com/questions/71323/how-to-replace-a-character-for-a-newline-in-vim](http://stackoverflow.com/questions/71323/how-to-replace-a-character-for-a-newline-in-vim) – Kracekumar Jul 29 '11 at 03:32

3 Answers3

16

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//
ib.
  • 25,324
  • 10
  • 71
  • 97
3

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.
ib.
  • 25,324
  • 10
  • 71
  • 97
Joe Holloway
  • 25,314
  • 12
  • 77
  • 90
  • perfect! this works amazingly good. Still, is there a keyword to use like the manual delete? – Hassek Jul 29 '11 at 03:46
1

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

tr -d '\n' < file
ib.
  • 25,324
  • 10
  • 71
  • 97
Dave
  • 10,479
  • 3
  • 27
  • 52