How do I join two lines in vi?

VimVi

Vim Problem Overview


I have two lines in a text file like below:

S<Switch_ID>_F<File type>
_ID<ID number>_T<date+time>_O<Original File name>.DAT

I want to append the two lines in vi like below:

S<Switch_ID>_F<File type>_ID<ID number>_T<date+time>_O<Original File name>.DAT

The second line got deleted and the contents of the second line was appended to the first line.

How could I do it using command mode in vi?

Vim Solutions


Solution 1 - Vim

Shift+J removes the line change character from the current line, so by pressing "J" at any place in the line you can combine the current line and the next line in the way you want.

Solution 2 - Vim

Vi or Vim?

Anyway, the following command works for Vim in 'nocompatible' mode. That is, I suppose, almost pure vi.

:join!

If you want to do it from normal command use

gJ

With 'gJ' you join lines as is -- without adding or removing whitespaces:

S<Switch_ID>_F<File type>
_ID<ID number>_T<date+time>_O<Original File name>.DAT

Result:

S<Switch_ID>_F<File type>_ID<ID number>_T<date+time>_O<Original File name>.DAT

With 'J' command you will have:

S<Switch_ID>_F<File type> _ID<ID number>_T<date+time>_O<Original File name>.DAT

Note space between type> and _ID.

Solution 3 - Vim

This should do it:

J

Solution 4 - Vim

In vi, J (that's Shift + J) or :join should do what you want, for the most part. Note that they adjust whitespace. In particular, you'll end up with a space between the two joined lines in many cases, and if the second line is indented that indentation will be removed prior to joining.

In Vim you can also use gJ (G, then Shift + J) or :join!. These will join lines without doing any whitespace adjustments.

In Vim, see :help J for more information.

Solution 5 - Vim

Just replace the "\n" with "".

In vi/Vim for every line in the document:

%s/>\n_/>_/g

If you want to confirm every replacement:

%s/>\n_/>_/gc

Solution 6 - Vim

If you want to join the selected lines (you are in visual mode), then just press gJ to join your lines with no spaces whatsoever.

This is described in greater detail on the vi/Vim Stack Exchange site.

Solution 7 - Vim

Press Shift + 4 ("$") on the first line, then Shift + j ("J").

And if you want help, go into vi, and then press F1.

Solution 8 - Vim

In Vim you can also use gJ.

ََ

Solution 9 - Vim

Another way of joining two lines without placing cursor to that line is:

:6,6s#\n##

Here 6 is the line number to which another line will be join. To display the line number, use :set nu.

If we are on the cursor where the next line should be joined, then:

:s#\n##

In both cases we don't need g like :s#\n##g, because on one line only one \n exist.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionVijayView Question on Stackoverflow
Solution 1 - VimGJ.View Answer on Stackoverflow
Solution 2 - VimMaxim KimView Answer on Stackoverflow
Solution 3 - VimsamgView Answer on Stackoverflow
Solution 4 - VimLaurence GonsalvesView Answer on Stackoverflow
Solution 5 - VimCarsten C.View Answer on Stackoverflow
Solution 6 - VimBrad ParksView Answer on Stackoverflow
Solution 7 - Vimghostdog74View Answer on Stackoverflow
Solution 8 - VimjoshyView Answer on Stackoverflow
Solution 9 - VimvusanView Answer on Stackoverflow