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 p开发者_运维问答lugin to do it.
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.
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
For those more comfortable with Visual Mode:
1. Identify the line number above the selection you want flipped using :set nu
.
2. Use Shift-V
to highlight selection you want flipped (visual mode).
3. :g/^/m <Line number from step 1>
.
Note that in visual mode it will automatically show up as
:'<,'>g/^/m <Line number>
when you type in the command from 3.
This command works by moving the selection one line at a time into the line number that you give it. When the second item gets pushed into the line number given, it pushes the first down to line number + 1. Then the third pushes the first and second down and so on until the entire list has been pushed into the single line number resulting in a reverse ordered list.
Further to Dave Kirby's answer and addressing the "how to do this simply" requirement, you could create a shortcut in your .vimrc
file. The following example maps the F5
key to this shortcut:
map <F5> :'<,'>!tail -r<CR>
or for OS X:
map <F5> :'<,'>!tac<CR>
精彩评论