You can remove line endings by joining lines together. There are a few commands that can accomplish this, for instance the J
Normal-mode command will join the current line and the next, or you can use a count to join more than the two lines, or use J
in Visual mode to join all lines spanning the selection.
(The :join
command that @D.BenKnoble mentioned and that takes a range is analogous to the J
Normal-mode command, so that's an option too.)
The J
command will add spaces between the joined lines (which, from your example, is what you want.) If you for some reason don't want to add spaces in between the joined lines, you can use gJ
instead. (Or, if you're using the Ex command, you can use :join!
with an extra !
at the end.)
When J
is adding spaces in between joined lines, in some situations it will add two spaces, when the initial line ends in a period, question mark, exclamation mark. You can control this behavior with the 'joinspaces'
option, which is on by default (the behavior of conditionally adding two spaces), but you can disable with :set nojoinspaces
.
Finally, if you want to use a :substitute
command to replace the newlines (instead of one of the join command variants), you can use \n
to match the newline, and replacing it will also have the effect of joining lines together.
The following :substitute
command is similar to :%join
:
:%s/\n/ /
Except that the :s
command will not respect the 'joinspaces'
behavior and it will add a space to the last line in the buffer (you could use a range of :1,$-1
to avoid that last part.)
Using a :s
command matching a \n
can be useful when you want to replace the newline with something other than a space, or otherwise collapse whitespace preceding the newlines, or perhaps only join consecutive blank lines, situations where using regexes give you a lot more flexibility than a command such as J
that can only do one thing.
= vector(1)
and so on, which I deleted via Visual Block mode. – rotton Dec 18 '20 at 11:39