Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Vim, what is the simplest way to join all lines in a file into a single line?

Tags:

vim

windows

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.

like image 216
Jordan Parmer Avatar asked Dec 24 '08 15:12

Jordan Parmer


People also ask

How do I convert multiple lines to one line in Vim?

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.

Which command joins next line to 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.

What is the shortcut to get to the beginning of a line in Vim?

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]


1 Answers

Another way:

ggVGJ 

"ggVG" visually selects all lines, and "J" joins them.

like image 54
orip Avatar answered Sep 20 '22 21:09

orip