mollerhoj
mollerhoj

Reputation: 1338

Opposite of newline in vim

In vim, is there a command to delete the newline, and all empty space behind the cursor?

Say I stand in the middle of a text in insert mode and press Enter, what command would the reverse what I just did?

A) An example:

"some code{ in here }"

B) After pressing Enter:

"some code{
   in here }"

Now pressing backspace will delete one space of the indentation. I would rather have it delete all indentation, and jump back to A.

Can this be done in a command or by doing some remapping to the backspace key?

Upvotes: 4

Views: 693

Answers (5)

user202729
user202729

Reputation: 3955

It's also possible to use a remapping of the backspace key:

inoremap <expr> <bs> getline('.')[:col('.')-2]=~'^\s\+$' ? "<c-u><c-u>" : "<bs>"

Note that this mapping completely overrides the normal behavior the backspace key. This will only be useful when you don't intend to use its normal behavior. This is not recommended if you can easily access the other options (c-u or J)

However, (as far as I know) there's no way to distinguish between manually added leading white spaces and auto indent. If you use noexpandtab, you can edit the regex to only match tabs.

This also does not work in some modes of auto-indent (for example, in block comment in C, vim automatically start a new line starts with *)

Upvotes: 0

244an
244an

Reputation: 1589

One easy way is up one line, to end of that line and just delete. As long as you still are in insert mode it will do the same thing as J when deleting at the last position - like most other editors. For me that is the quickest alternative because I'm used to it from other editors.

That is: , End, Delete (when still in insert mode)

One quick alternative (the VIM-way) is (when still in insert mode):
, Ctrl+o, J (when still in insert mode)

(Ctrl+o is used in insert mode to enter one normal mode command.)

Upvotes: 1

Konrad Rudolph
Konrad Rudolph

Reputation: 545598

You can do ᴇꜱᴄ, K, Shift+J.

K jumps up to the previous line and Shift+J joins the two lines.

However, with properly configured indentation and syntax, a backspace doesn’t just delete a space, it deletes the full previous indentation block.

Upvotes: 1

Ingo Karkat
Ingo Karkat

Reputation: 172580

It's tragic how unknown the J command is. It joins lines in normal mode.

In insert mode, you can press <C-U> twice; first, it'll delete the indent before the cursor, then it'll join with the previous line. Note that this requires

:set backspace=indent,eol,start

Upvotes: 9

Kent
Kent

Reputation: 195059

did you try J (uppercase) ? it will give exactly what you want.

"some code{ cursor on this line, pressJ

in here }"

Upvotes: 5

Related Questions