184

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Vijay
  • 65,327
  • 90
  • 227
  • 319
  • Unrelated question: why do we require to join two lines? – Rishabh Bhatnagar May 07 '21 at 14:08
  • @RishabhBhatnagar, coz we need them to be joined. – Vijay May 10 '21 at 06: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. – Rishabh Bhatnagar May 11 '21 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 }``` – Rishabh Bhatnagar May 22 '23 at 15:10

9 Answers9

339

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.

ArtOfWarfare
  • 20,617
  • 19
  • 137
  • 193
GJ.
  • 4,518
  • 1
  • 19
  • 26
  • 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. – Laurence Gonsalves Dec 16 '09 at 07: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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Maxim Kim
  • 6,192
  • 2
  • 29
  • 28
  • 2
    Just want to point out that `g/pattern/join` works in VIM while `g/pattern/J` does not. Might help some future people. – jisaacstone Mar 25 '13 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? – David.Chu.ca Feb 26 '15 at 17:10
  • g/pattern/join! seems to do that. – fortboise Aug 25 '15 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 – userrandrand May 23 '23 at 17:29
26

This should do it:

J

samg
  • 3,496
  • 1
  • 25
  • 26
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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Laurence Gonsalves
  • 137,896
  • 35
  • 246
  • 299
  • One caveat when using `gJ` and `J` is that both chang the cursor to the joining point, but `:join!` and `:join` dont. – SergioAraujo Dec 15 '17 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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Carsten C.
  • 211
  • 1
  • 7
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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Brad Parks
  • 66,836
  • 64
  • 257
  • 336
1

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

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ghostdog74
  • 327,991
  • 56
  • 259
  • 343
1

In Vim you can also use gJ.

ََ

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
joshy
  • 71
  • 7
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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
vusan
  • 5,221
  • 4
  • 46
  • 81
  • 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. – Evgeni Sergeev May 18 '16 at 07:07
  • Good for selected multiple line join with preferred delimiter. Found one typo ... in normal mode or in visual mode? – vusan May 18 '16 at 09:21