I want to join all lines in a file into a single line. What is the simplest way of doing this? I\'ve had poor luck trying to use substitution (\\r\\n
or
Another way:
ggVGJ
"ggVG
" visually selects all lines, and "J
" joins them.
You can do it with 3 keystrokes starting from normal mode:
:%j
:
enters command mode%
refers to all lines in the filej
executes the join commandNow it seems that this adds a space between the lines. I am not sure if you want this.
Ah, I found the answer.
:1,$join
Works like a charm.
EDIT: As pointed out in the comment:
:%join -or- :%j
...removes the range.
Cryptic way:
qqqqqJ@qq@q
(the first three q
's clear the q
register, the qqJ@qq
records a macro to the q
register that performs a Join, then calls q
, and the last @q
runs it.
You can do it in three fewer keystrokes:
:1,$j
isn't ed grand?
I’m surprised no one even mentioned the other way:
:%s/\n/ /
I am equally surprised that no one pointed out that the range 1,$
has a shorthand that’s written %
.
(This doesn’t do the same thing as joining the lines, but depending on circumstances that may in fact be more appropriate.)