185

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?

4
  • Unrelated question: why do we require to join two lines? May 7, 2021 at 14:08
  • @RishabhBhatnagar, coz we need them to be joined.
    – Vijay
    May 10, 2021 at 6:30
  • Since J is a very easily reachable key, join must be one of the most frequently used operations. I just wanted to know the use-cases where joins are needed. May 11, 2021 at 10:11
  • After extensively using vim, I've realised why I'd use join. It is very much used while coding to merge comment lines or param list or cases when you want to merge expressions like err = someFunction(); if err != nil { return err } into if err := someFunction(); err != nil { return err } May 22 at 15:10

9 Answers 9

342

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.

1
  • 33
    "removes the line change character from the current line" is a pretty awkward way to describe what J does, and is also not really correct. J "joins" this line to the next. In the process it removes the newline, but also manipulates whitespace in other ways. Dec 16, 2009 at 7:26
64

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.

4
  • 2
    Just want to point out that g/pattern/join works in VIM while g/pattern/J does not. Might help some future people. Mar 25, 2013 at 18:18
  • I like your join with pattern, however, it joins two lines with white space. Anyway to join two lines with a pattern without white space? Feb 26, 2015 at 17:10
  • g/pattern/join! seems to do that.
    – fortboise
    Aug 25, 2015 at 22:54
  • If the next line has spaces at the beginning of the line, gJ also keeps those spaces whereas J seems to only place a single space character regardless of how many spaces there were at the beginning of the next line May 23 at 17:29
26

This should do it:

J

3
  • 6
    'J' gives extra space while joining lines.
    – Maxim Kim
    Dec 16, 2009 at 7:25
  • 4
    @MaximKim: And this is why. Note that :h J will tell you how to disable that feature.
    – ereOn
    Jan 20, 2015 at 20:01
  • 2
    Use gJ if you don't want vim to add a space when joining the lines
    – qwertzguy
    Apr 26, 2016 at 20:02
22

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.

1
  • One caveat when using gJ and J is that both chang the cursor to the joining point, but :join! and :join dont. Dec 15, 2017 at 11:47
5

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
3

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.

1

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

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

6
  • 4
    $ moves to the end of the row, but it is not necessary for J
    – user25148
    Dec 16, 2009 at 7:24
  • 2
    You don't have to be at the end of the line for J to join lines. Dec 16, 2009 at 7:25
  • 2
    Pressing $ isn't necessary, actually. Dec 16, 2009 at 7:27
  • 1
    @laurence .pressing $ ensures that the cursor is placed just beside the new line charter and this is a must.
    – Vijay
    Dec 16, 2009 at 9:04
  • @Vijay: It is not a must (at least not with Vim 8.1.2269). You get an extra space either way. (Does the configuration of vi/Vim play a role?) Using gJ avoids the extra space. What did you try it on? Oct 6, 2020 at 18:03
1

In Vim you can also use gJ.

ََ

1

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.

2
  • It's easier to use Shift+V to select all lines that are to be joined, except the last, and then :'<,'>s/\n/, / in this example joining lines while putting a comma and a space at the end of each. Note that when something is selected and we type : in normal mode, then the '<,'> appears on the command line automatically. May 18, 2016 at 7:07
  • Good for selected multiple line join with preferred delimiter. Found one typo ... in normal mode or in visual mode?
    – vusan
    May 18, 2016 at 9:21

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.