Reputation: 1447
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?
Upvotes: 4
Views: 1534
Reputation: 13252
New answer:
d^o<c-u><esc>p
Figured it out. This was one of my early attempts, but in the form <esc>d^o<esc>p
, which has the problem that the inserted line gets a comment leader. <c-u>
fixes that.
d^o<c-u><c-o>p
is of course useful if one wants to stay in Insert mode.
This solution works, but the answer by Randy Morris is better.
Suggested key sequence:
<esc>mpa<cr><esc>dd`pP`pa
(Where p
can be replaced with any other mark.)
This means <esc>
enter Normal mode, mp
mark current position as p
, a
enter Insert mode, <cr>
break the line and put the comment on the next line at the correct indentation, <esc>
go to Normal mode, dd
delete line, `p
go to the marked position, P
put the deleted line before the current line, `p
go to the marked position, a
go to Insert mode. To avoid wearing your fingers out, map it:
:inoremap <F2> <esc>mpa<cr><esc>dd`pP`pa
The indentation will not be correct if the comment is at the end of a line that increases or decreases indentation.
Simpler sequence that will not work on the last line in the buffer
To perform this operation on a line that isn't the last line in the buffer, the following will do. With the cursor on the first slash, in Insert mode: <cr><esc>ddkPjA
.
The <cr>
breaks the line and puts the comment at the correct indentation, <esc>
go to Normal mode, dd
delete line, k
go to previous line, P
put the deleted line before the current line, jA
to end up in Insert mode where you were when you started.
To map it:
:inoremap <F2> <cr><esc>ddkPjA
Upvotes: 2
Reputation: 31419
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)
Upvotes: 3
Reputation: 40927
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.Upvotes: 3