ChatGPT解决这个技术问题 Extra ChatGPT

Delete newline in Vim

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?

Del at the end of line in vim's insert mode used to work for me. Not in every terminal, though.

T
Tuna

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.


As noted in another answer, gJ will avoid adding spaces which Vim may choose to add when using J.
You can also select all the lines you want to join using V (Visual Line Mode) then press J or gJ
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>.
T
TinkerTank

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

:%s/\n/

Thanks, but I don't want a global search and replace.
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
Or you can select a V-isual block and replace over it.
M
Mateusz Piotrowski

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.


N
Néstor Waldyd

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

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.
This worked for me. I was trying to get rid of a newline at the end of a single line file with vim.
Why :set noendofline doesn't work without binary?
Although this wasn't OP's specific question, it was my question, and an internet search landed me here, so thanks!
This is one of the simplest way of removing new lines for ad-hoc cases. Neat Trick, thanks.
e
ephemient

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.


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


k
kzh

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

Without spaces between them :j!3
W
Wolph

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


"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.
D
David Watson

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.


Sadly, that doesn't seem to work for me; it might be a PuTTY setting that I've missed.
probably the vi backspace option: :help 'backspace'
I
Ivan Lopes
<CURSOR>Evaluator<T>():
    _bestPos(){
}

cursor in first line

NOW, in NORMAL MODE do

shift+v
2j
shift+j

or

V2jJ

:normal V2jJ


g
ghostdog74

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

That's kind of overkill, don't you think?
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.
Still easier to just open the file in vim and use ggVGJ.
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.
I'm not processing a bunch of files; I'm just working with condensing a few lines of code.
S
Sergio Abreu

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


T
Theo Moolenaar

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.


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.
Ah, but you answered my question. so +1 for that.