Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 316
stephenmm Avatar asked Apr 06 '11 03:04

stephenmm


People also ask

How do I reverse the order of lines in Vim?

command! -bar -range=% Reverse <line1>,<line2>g/^/m0|nohl " REVERSE line ordering, and move those lines to the top of the file.

How do I switch lines in Vim?

To swap the current line with the next one, type ddp while in command mode.

How do I use visual mode in Vim?

To get into the Vim Visual Line mode, press “Shift+V” while you are in Vim's Normal mode.

How do I select a visual block in vim?

While you are inside a visual mode, you can switch to another visual mode by pressing either v , V , or Ctrl-v . For example, if you're in line-wise visual mode and you want to switch to block-wise visual mode, just type Ctrl-v . Try it! This will start visual mode on the same selected area as the last visual mode.


2 Answers

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.

like image 86
Dave Kirby Avatar answered Sep 21 '22 03:09

Dave Kirby


According to :help 12.4 you can mark the first line with the mt, move to the last line you want reversed then use the command :'t+1,.g/^/m 't

like image 31
GWW Avatar answered Sep 19 '22 03:09

GWW