10

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.
  • 27,830
  • 11
  • 80
  • 100
Hassek
  • 8,715
  • 6
  • 47
  • 59
  • [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

17

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.
  • 27,830
  • 11
  • 80
  • 100
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.
  • 27,830
  • 11
  • 80
  • 100
Joe Holloway
  • 28,320
  • 15
  • 82
  • 92
  • perfect! this works amazingly good. Still, is there a keyword to use like the manual delete? – Hassek Jul 29 '11 at 03:46
2

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

tr -d '\n' < file
ib.
  • 27,830
  • 11
  • 80
  • 100
Dave
  • 10,964
  • 3
  • 32
  • 54