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 \n
doesn't seem to get picked up correctly in the case of s/\r\n//
on Windows). Using J
in a range expression doesn't seem to work either (probably because the range is no longer in 'sync' after the first command is executed).
I tried :1,$norm! J
but this only did half of the file - which makes sense because it just joins each line once.
When you want to merge two lines into one, position the cursor anywhere on the first line, and press J to join the two lines. J joins the line the cursor is on with the line below. Repeat the last command ( J ) with the . to join the next line with the current line.
Joins consecutive lines. The J (join) command joins a specified number of lines together as one line. Number of consecutive lines, starting with the current line, to be joined to the current line.
I will remap some shortcut keys in my vimrc, most of them are cursor moving under the Insert mode. That means: Ctrl + a : Go to beginning of the line [Normal Mode && Insert Mode] Ctrl + e : Go to end of line [Normal Mode && Insert Mode]
Another way:
ggVGJ
"ggVG
" visually selects all lines, and "J
" joins them.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With