tags:

views:

77

answers:

6

Is there a way to delete the newline at the end of a line in Vim, so that the next line is appended to the current line?

For example:

Evaluator<T>():
    _bestPos(){
}

I'd like to put this all on one line without copying lines and pasting them into the previous one. It seems like I should be able to put my cursor to the end of each line, press a key, and have the next line jump onto the same one the cursor is on.

End result:

Evaluator<T>(): _bestPos(){ }

Is this possible in Vim?

A: 

Certainly. Vim recognizes the \n character as a newline, so you can just search and replace. In command mode type:

:%s/\n/
TumbleCow
Thanks, but I don't want a global search and replace.
derekerdmann
removing the % takes care of that. Then it will only happen on the line the cursor is on. Alternatively, you can specify a range such as :11,15s/\n/ (lines 11-15) or :,+7s/\n/ (this line and the next seven) or :-3,s/\n/ (previous three lines and this one)... you get the idea
Tristan
A: 

It probably depends on your settings, but I usually do this with A<delete>

Where A is append at the end of the line. It probably requires nocompatible mode :)

WoLpH
+1  A: 

I would just press A (append to end of line, puts you into insert mode) on the line where you want to remove the newline and then press delete.

David Watson
Sadly, that doesn't seem to work for me; it might be a PuTTY setting that I've missed.
derekerdmann
probably the vi backspace option: `:help 'backspace'`
glenn jackman
+5  A: 

While on the upper line in normal mode, hit shift+j.

You can prepend a count too, so 3J on the top line would join all those lines together.

Alligator
+8  A: 

If you are on the first line, pressing J will join that line and the next line together, removing the newline. You can also combine this with a count, so pressing 3J will combine all 3 lines together.

Xhantar
Exactly what I was looking for. Thanks!
derekerdmann
A: 

if you don't mind using other shell tools,

tr -d "\n" < file >t && mv -f t file

sed -i.bak -e :a -e 'N;s/\n//;ba' file

awk '{printf "%s",$0 }' file >t && mv -f t file
ghostdog74
That's kind of overkill, don't you think?
derekerdmann
no, its not. why do you think its overkill? Doing it inside Vim is manual. It depends on whether you want to do it manually by hand every time, or just execute these one liners to get it done in a jiffy. Vim is an editor. And any tools that can process files, are basically "editors" in disguise.
ghostdog74
Still easier to just open the file in vim and use `ggVGJ`.
too much php
sure, if you find it easier to do it by hand every time you need data changed that way. Go ahead. Also, try doing that on a big file.
ghostdog74
I'm not processing a bunch of files; I'm just working with condensing a few lines of code.
derekerdmann