Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim - move rest of line to new line above

Tags:

vim

How does one get from

int lel = 123; // this is a comment
               ^

to

// this is a comment
int lel = 123;

preferably when starting in insert mode, and with the right indentation?

My current way of doing it is C-c l d$ O C-c p, but as my auto-indent isn't perfect, the inserted line in not indented at all.

I think some editors use space+enter or something for this (at least I think I've seen it).

Is this possible in vim by default?

like image 754
MartinHaTh Avatar asked Oct 27 '25 00:10

MartinHaTh


2 Answers

Still not a very pretty answer, but assuming the cursor is where the "^" is above, another option would be:

d0=:puEnter

  • d0 deletes til the beginning of the line.
  • = reindents over the next motion.
  • :pu short for :put
  • Enter to run the command.
like image 74
Randy Morris Avatar answered Oct 29 '25 16:10

Randy Morris


I would probably use

DO<c-r>"

D deletes to the end of line. O opens the line above in insert mode (with the correct indentation). <c-r>" pastes the part that was deleted with D.

(This ends in insert mode)

like image 24
FDinoff Avatar answered Oct 29 '25 16:10

FDinoff