Delete newline in Vim

UnixVimShellSshVi

Unix Problem Overview


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?

Unix Solutions


Solution 1 - Unix

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.

Solution 2 - Unix

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

:%s/\n/

Solution 3 - Unix

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.

Solution 4 - Unix

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

Solution 5 - Unix

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.

Solution 6 - Unix

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

Solution 7 - Unix

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

Solution 8 - Unix

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 :)

Solution 9 - Unix

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.

Solution 10 - Unix

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

cursor in first line

NOW, in NORMAL MODE do

shift+v
2j
shift+j

or

V2jJ

:normal V2jJ

Solution 11 - Unix

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

Solution 12 - Unix

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

Solution 13 - Unix

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionderekerdmannView Question on Stackoverflow
Solution 1 - UnixXhantarView Answer on Stackoverflow
Solution 2 - UnixTinkerTankView Answer on Stackoverflow
Solution 3 - UnixAlligatorView Answer on Stackoverflow
Solution 4 - UnixNéstor WaldydView Answer on Stackoverflow
Solution 5 - UnixephemientView Answer on Stackoverflow
Solution 6 - UnixlaktakView Answer on Stackoverflow
Solution 7 - UnixkzhView Answer on Stackoverflow
Solution 8 - UnixWolphView Answer on Stackoverflow
Solution 9 - UnixDavid WatsonView Answer on Stackoverflow
Solution 10 - UnixIvan LopesView Answer on Stackoverflow
Solution 11 - Unixghostdog74View Answer on Stackoverflow
Solution 12 - UnixSergio AbreuView Answer on Stackoverflow
Solution 13 - UnixTheo MoolenaarView Answer on Stackoverflow