tags:

views:

390

answers:

7

This might be a small one!

I have a 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

What happened here is that the second line got deleted and and the contents of second line are appended to the first line.

How could I do it using in command mode in vi?

+6  A: 

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.

GJ
upper case J joins lines, lower case moves cursor
Lars Wirzenius
That was a capital J, not a lower-case J; hence Shift-J.
Jonathan Leffler
if you will press "j" it will move the cursor to the next line but if you will press "J" it will join the two lines.
GJ
2benjamin: note capital 'J'.
Maxim Kim
"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
yes GJ.you are right. thanks for the answer:)
Vijay Sarathi
Haha. `GJ` in `vim` will move to the last line and then try to join it to the next line, the only place a join command doesn't make sense. Yeah, I found this funny :-)
Alok
+3  A: 

This should do it:

J

samg
'J' gives extra space while joining lines.
Maxim Kim
A: 

press Shift-4 ("$") on the first line, then Shift-j("J"). And if you want help, go into vi, then press F1

$ moves to the end of the row, but it is not necessary for J
Lars Wirzenius
You don't have to be at the end of the line for J to join lines.
Jonathan Leffler
Pressing $ isn't necessary, actually.
Laurence Gonsalves
@laurence .pressing $ ensures that the cursor is placed just beside the new line charter and this is a must.
Vijay Sarathi
A: 

Just replace the "\n" with ""

in VI, VIM for every line in document

%s/>\n_/>_/g

if you want to confirm every replacment

%s/>\n_/>_/gc

Carsten C.
+9  A: 

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

Maxim Kim
+2  A: 

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 info.

Laurence Gonsalves
A: 

In vim you can also use gJ

joshy