Search code examples
vim

How to flip the lines of a visual selection in vim?


I want to take a visual selection and flip it so that the first line of the selection is on the bottom. From:

<other_stuff>
The
wheels
go
round.
<more_stuff>

Visual select the sentence and then flip:

<other_stuff>
round.
go
wheels
The
<more_stuff>

How to do this simply? I would rather not have to install a plugin to do it.


Solution

  • When you make a visual selection Vim automatically makes the bookmarks '< and '> at the start and end of the block respectively, so you can do what you want in a couple of ways.

    In normal mode: '>dd'<P

    As an ex command: :'>d | '<-1 put

    NB the bookmarks remain after you exit visual mode, so you do not have to stay in visual mode to use these.

    edit:

    Oops, I misread the question and thought you only wanted the last line put at the start, but you want the entire block reversed. The simplest solution if you are on a unix system:

    :'<,'>!tac
    

    This pipes the lines through the unix 'reverse cat' program - tac, part of coreutils.