332

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?

derekerdmann
  • 17,696
  • 11
  • 76
  • 110

13 Answers13

701

If you are on the first line, pressing (upper case) 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.

Tuna
  • 2,937
  • 4
  • 37
  • 61
Xhantar
  • 7,208
  • 1
  • 14
  • 10
  • 37
    As noted in another answer, `gJ` will avoid adding spaces which Vim may choose to add when using `J`. – Michael Mior Apr 21 '14 at 19:46
  • 12
    You can also select all the lines you want to join using `V` (Visual Line Mode) then press `J` or `gJ` – Sbu Jan 24 '17 at 06:13
  • If you want to keep the cursor position use `:join` and `:join!` that will join lines keeping cursor position and do not keep any space at the joining point. you can also map it like: `nnoremap j :joing` and `nnoremap gj :join!`. – SergioAraujo Dec 17 '17 at 23:23
99

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

:%s/\n/
TinkerTank
  • 5,685
  • 2
  • 32
  • 41
  • 1
    Thanks, but I don't want a global search and replace. – derekerdmann Oct 21 '10 at 00:53
  • 21
    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 Oct 21 '10 at 01:03
  • 1
    Or you can select a `V`-isual block and replace over it. – Victor Sergienko Jul 15 '14 at 13:06
62

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.

Mateusz Piotrowski
  • 8,029
  • 10
  • 53
  • 79
Alligator
  • 1,359
  • 11
  • 8
60

As other answers mentioned, (upper case) J and search + replace for \n can be used generally to strip newline characters and to concatenate lines.

But in order to get rid of the trailing newline character in the last line, you need to do this in Vim:

:set noendofline binary
:w
Néstor Waldyd
  • 924
  • 7
  • 6
32

J deletes extra leading spacing (if any), joining lines with a single space. (With some exceptions: after /[.!?]$/, two spaces may be inserted; before /^\s*)/, no spaces are inserted.)

If you don't want that behavior, gJ simply removes the newline and doesn't do anything clever with spaces at all.

ephemient
  • 198,619
  • 38
  • 280
  • 391
12
set backspace=indent,eol,start

in your .vimrc will allow you to use backspace and delete on \n (newline) in insert mode.

set whichwrap+=<,>,h,l,[,]

will allow you to delete the previous LF in normal mode with X (when in col 1).

laktak
  • 57,064
  • 17
  • 134
  • 164
10

All of the following assume that your cursor is on the first line:

Using normal mappings:

3Shift+J

Using Ex commands:

:,+2j

Which is an abbreviation of

:.,.+2 join

Which can also be entered by the following shortcut:

3:j

An even shorter Ex command:

:j3
kzh
  • 19,810
  • 13
  • 73
  • 97
3

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
  • 78,177
  • 11
  • 137
  • 148
  • 3
    *"I usually do this with A"* Yeah that's why I came here; to find a better way ;). The whole point of vim is not moving your hands from the home row. – Luc Jul 23 '14 at 08:25
3

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
  • 2,031
  • 2
  • 13
  • 17
2
<CURSOR>Evaluator<T>():
    _bestPos(){
}

cursor in first line

NOW, in NORMAL MODE do

shift+v
2j
shift+j

or

V2jJ

:normal V2jJ

Ivan Lopes
  • 31
  • 3
1

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
  • 327,991
  • 56
  • 259
  • 343
  • 7
    That's kind of overkill, don't you think? – derekerdmann Oct 21 '10 at 00:54
  • 1
    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 Oct 21 '10 at 01:02
  • 5
    Still easier to just open the file in vim and use `ggVGJ`. – too much php Oct 21 '10 at 01:48
  • 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 Oct 21 '10 at 01:55
  • I'm not processing a bunch of files; I'm just working with condensing a few lines of code. – derekerdmann Oct 21 '10 at 02:00
  • If you're not processing big files, then a solution appropriate for big files is irrelevant. That doesn't mean it should be presented! – Gene Callahan Sep 24 '15 at 23:12
1

The problem is that multiples char 0A (\n) that are invisible may accumulate. Supose you want to clean up from line 100 to the end:

Typing ESC and : (terminal commander)

:110,$s/^\n//

In a vim script:

execute '110,$s/^\n//'

Explanation: from 110 till the end search for lines that start with new line (are blank) and remove them

Sergio Abreu
  • 2,686
  • 25
  • 20
1

A very slight improvement to TinkerTank's solution if you're just looking to quickly concatenate all the lines in a text file is to have something like this in your .vimrc:

nnoremap <leader>j :%s/\n/\ /g<CR>

This globally substitutes newlines with a space meaning you don't end up with the last word of a line being joined onto the first word of the next line. This works perfectly for my typical use-case.

If you're wanting to maintain deliberate paragraph breaks, V):join is probably the easiest solution.

  • 1
    You're answering a different question really. If you feel like the question you're answering hasn't been asked, you could open it on https://vi.stackexchange.com/ and answer it yourself. – Cornelius Roemer Jul 03 '21 at 19:55
  • Ah, but you answered my question. so +1 for that. – j0h Jul 20 '21 at 18:52