336

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?

1
  • 1
    Del at the end of line in vim's insert mode used to work for me. Not in every terminal, though. Jul 15, 2014 at 13:08

13 Answers 13

707

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.

3
  • 37
    As noted in another answer, gJ will avoid adding spaces which Vim may choose to add when using J. Apr 21, 2014 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, 2017 at 6: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 <Leader>j :joing<cr> and nnoremap <Leader>gj :join!<cr>. Dec 17, 2017 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/
3
  • 1
    Thanks, but I don't want a global search and replace. Oct 21, 2010 at 0: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, 2010 at 1:03
  • 1
    Or you can select a V-isual block and replace over it. Jul 15, 2014 at 13:06
65

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
5
  • 1
    The question is about concatenating two lines in the middle of a file - this answer removes the newline at the end of the file, which isn't what I asked about. May 27, 2016 at 22:27
  • 4
    This worked for me. I was trying to get rid of a newline at the end of a single line file with vim.
    – Locane
    Dec 12, 2017 at 18:55
  • Why :set noendofline doesn't work without binary?
    – darw
    Jun 12, 2020 at 19:30
  • 5
    Although this wasn't OP's specific question, it was my question, and an internet search landed me here, so thanks! Apr 14, 2021 at 18:28
  • This is one of the simplest way of removing new lines for ad-hoc cases. Neat Trick, thanks. Jan 9, 2022 at 7:51
63

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.

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.

0
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).

0
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
1
  • 2
    Without spaces between them :j!3 Dec 17, 2017 at 23:29
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 :)

1
  • 3
    "I usually do this with A<delete>" 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, 2014 at 8: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.

2
  • Sadly, that doesn't seem to work for me; it might be a PuTTY setting that I've missed. Oct 21, 2010 at 0:52
  • 2
    probably the vi backspace option: :help 'backspace' Oct 21, 2010 at 10:59
2
<CURSOR>Evaluator<T>():
    _bestPos(){
}

cursor in first line

NOW, in NORMAL MODE do

shift+v
2j
shift+j

or

V2jJ

:normal V2jJ

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
6
  • 7
    That's kind of overkill, don't you think? Oct 21, 2010 at 0: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, 2010 at 1:02
  • 5
    Still easier to just open the file in vim and use ggVGJ. Oct 21, 2010 at 1: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, 2010 at 1:55
  • I'm not processing a bunch of files; I'm just working with condensing a few lines of code. Oct 21, 2010 at 2:00
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

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.

2
  • 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 vi.stackexchange.com and answer it yourself. Jul 3, 2021 at 19:55
  • Ah, but you answered my question. so +1 for that.
    – j0h
    Jul 20, 2021 at 18:52

Your Answer

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.