Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

vim

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!

like image 772
Hassek Avatar asked Jul 29 '11 03:07

Hassek


2 Answers

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//
like image 122
ib. Avatar answered Sep 30 '22 03:09

ib.


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.
like image 30
Joe Holloway Avatar answered Sep 30 '22 04:09

Joe Holloway