0

I was wondering which was the shortest way to turn upside down two adjacent lines in vim, for example:

Hi
How are you?

in

How are you?
Hi

Are there some special shortcuts or should I consider to write a macro for this?

Acsor
  • 1,011
  • 2
  • 13
  • 26
  • From the vim wiki: http://vim.wikia.com/wiki/Moving_lines_up_or_down If you have unimpaired installed you can use `[e` or `]e`, https://github.com/tpope/vim-unimpaired – Peter Rincker Jul 24 '13 at 17:58

2 Answers2

3

Use ddp when your cursor is on the first line.

dd deletes the current line.
p pastes the deleted line under the current one.

FDinoff
  • 30,689
  • 5
  • 75
  • 96
  • ddp is good, but it's something I do many times for my purposes. Anyway, it should be ddP. – Acsor Jul 24 '13 at 17:37
  • @none `ddP` puts the lines back exactly the way they were. And how many times are we talking? You might be able to do something with the global command if there is a pattern. – FDinoff Jul 24 '13 at 17:44
  • Well, you meant to do that on the first line, and not on the second. Also, ddP works only without any other lines below the second one. That's ok, thanks! – Acsor Jul 24 '13 at 17:48
2

What you need is a mapping: what that mapping does, even if it's 20 commands chained, doesn't really matter.

nnoremap <F6> :m-2<CR>==

and

nnoremap <F6> ddp

both do exactly what you want in slightly different ways. One command is complex and relatively smart while the other is simple and relatively dumb but they are equivalent in the way that they are both done with a single keystroke.

Of course you can use somerhing else than F6.

romainl
  • 186,200
  • 21
  • 280
  • 313