Quite a common task in programming is to remove the condition on the current block.
In vim, is there an easy way to delete the first line (the 'if' statement) and last line (the closing curly brace) of the current block and perhaps reindent accordingly. If there is no simple key combination for this built in, what is a straightforward way to script it?
Thinking about this a bit further, of course the condition on the if statement may span multiple lines so presumably a script is required to capture this completely. However, for my code just deleting the first and last lines would capture 95% of cases.
Possible solution
yiBvaBVpgv<
yiB
yanks the inner blockvaBV
viaually select a block, then select it linewisep
paste over visually selected textgv<
reselect text and de-indentSurround like mappings aka delete surrounding block:
nnoremap dsB yiBvaBVpgv<
ib provided a shorter solution. This solution will not mutate the visual marks: '<
, '>
diB]pkdk
diB
deletes the current inner block]p
paste the newly deleted text below the end of the block but adjust the indent.kdk
move up a line and then delete 2 lines up thereby deleting the start and end of the block.How about something like di{
dk
k
"2p
? Or a little more elegant, <i{
di{
dk
k
"2p
. Note that you have to be inside the block for this to work — if you're on the opening or closing lines of the block, you'll affect the surrounding block instead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With