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

后端 未结 4 1219
一生所求
一生所求 2021-02-18 18:38

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


The
wheels
go
round.


        
相关标签:
4条回答
  • 2021-02-18 19:01

    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.

    0 讨论(0)
  • 2021-02-18 19:03

    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>

    0 讨论(0)
  • 2021-02-18 19:06

    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

    0 讨论(0)
  • 2021-02-18 19:12

    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.

    0 讨论(0)
提交回复
热议问题