You are not logged in.

#1 2011-01-23 22:22:34

louis058
Member
Registered: 2010-11-12
Posts: 31

How to delete the EOL in Vim

This might sound really stupid, but how do you delete a line-break in Vim? Deleting the EOL after the cursor, when you're at the end of a line seems to be 'dw', as that will delete to the next word, therefore deleting the EOL in the middle of it. However, deleting the EOL before the cursor, when you're at the start of a line I can't figure out. 'db' would delete the EOL, but would also delete to the beginning of the word at the end of the line previous. The only way I've found I've been able to use is entering Insert mode and pressing Backspace. Surely this isn't the best method for doing this...

Offline

#2 2011-01-23 22:32:58

litemotiv
Forum Fellow
Registered: 2008-08-01
Posts: 5,026

Re: How to delete the EOL in Vim

I'm not sure what you mean, but have you tried shift-j?


ᶘ ᵒᴥᵒᶅ

Offline

#3 2011-01-23 23:05:29

cmtptr
Member
Registered: 2008-09-01
Posts: 135

Re: How to delete the EOL in Vim

I'm also not sure what you mean.  I interpreted two possible questions from this:

1) You're mistaking whitespace at the end of the line for the newline itself.  Try ":set list" to see non-printing characters.

2) You want to delete the newline at the end of a file.  I've never wanted to do this myself so I don't know, but I found a couple of suggestions here:
http://stackoverflow.com/questions/4133 … ine-or-eof

Last edited by cmtptr (2011-01-23 23:05:48)

Offline

#4 2011-01-23 23:15:36

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: How to delete the EOL in Vim

litemotiv wrote:

I'm not sure what you mean, but have you tried shift-j?

It will merge this line and the next one, not this one and the previous one.

The underline "_" is your cursor. You have:

1
2

and you want

12

If you have

1
2

then Shift+j will do the trick.

Last edited by karol (2011-01-23 23:21:51)

Offline

#5 2011-01-24 14:37:55

egan
Member
From: Mountain View, CA
Registered: 2009-08-17
Posts: 273

Re: How to delete the EOL in Vim

karol wrote:
litemotiv wrote:

I'm not sure what you mean, but have you tried shift-j?

It will merge this line and the next one, not this one and the previous one.

The underline "_" is your cursor. You have:

1
2

and you want

12

If you have

1
2

then Shift+j will do the trick.

Not to hijack the thread, but is there a way to do this so that it adds no space?

Offline

#6 2011-01-24 15:30:43

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: How to delete the EOL in Vim

egan wrote:

is there a way to do this so that it adds no space?

I don't know any such way, but you can always create a custom command / macro / mapping.

Offline

#7 2011-01-24 19:18:48

Yannick_LM
Member
Registered: 2008-12-22
Posts: 142

Re: How to delete the EOL in Vim

>> Not to hijack the thread, but is there a way to do this so that it adds no space?

:j!

(read the fine help  :help J would have told you that)

Offline

#8 2011-01-24 21:20:10

bernarcher
Forum Fellow
From: Germany
Registered: 2009-02-17
Posts: 2,281

Re: How to delete the EOL in Vim

Yannick_LM wrote:

>> Not to hijack the thread, but is there a way to do this so that it adds no space?

:j!

(read the fine help  :help J would have told you that)

Or use

<count>gJ

to join <count> lines (count can be negative in order to join lines upward). Or use visual mode with gJ.

Vim does not use the concept of deleting end of line characters but instead joins lines.
And it is all in :help J or :help join. wink

Last edited by bernarcher (2011-01-24 21:22:26)


To know or not to know ...
... the questions remain forever.

Offline

#9 2011-01-24 21:32:59

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: How to delete the EOL in Vim

I don't think any of those does what OP wanted - joining upwards.

@ bernarcher
I can't get the negative numbers to work. The manual says only

gJ                      Join [count] lines, with a minimum of two lines.
                        Don't insert or remove any spaces.  {not in Vi}

Does e.g. '-3gJ' really works for you? Maybe I have some weird settings in my .vimrc.


EDIT: I was thinking about something like

map II i<Backspace><Esc>

Of course you can change the 'II' mapping and maybe add '0':

map II i<Backspace><Esc>0

or whatever tells vim to go to the first char of the line.

'[ count ]II' won't work.

Last edited by karol (2011-01-24 21:43:05)

Offline

#10 2011-01-24 22:15:06

bernarcher
Forum Fellow
From: Germany
Registered: 2009-02-17
Posts: 2,281

Re: How to delete the EOL in Vim

karol wrote:

@ bernarcher
I can't get the negative numbers to work. The manual says only

gJ                      Join [count] lines, with a minimum of two lines.
                        Don't insert or remove any spaces.  {not in Vi}

Does e.g. '-3gJ' really works for you? Maybe I have some weird settings in my .vimrc

I tested it. At least I thought so. But trying again, negative counts appear not to work as wanted.
It is because the "-" effectively moves the cursor up a line.

But backwards joining still is possible. To join n lines backwards this should work:

<n-1>-<n>gJ

where <n>, <n-1> are the counts (without the brackets of course).


To know or not to know ...
... the questions remain forever.

Offline

#11 2011-01-24 22:32:20

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: How to delete the EOL in Vim

bernarcher wrote:

I tested it. At least I thought so. But trying again, negative counts appear not to work as wanted.
It is because the "-" effectively moves the cursor up a line.

Exactly.

'gJ' is great too, as you (well, at least I) usually move top to bottom.

Offline

Board footer

Powered by FluxBB