Vim Tips Wiki
Register
Advertisement

Duplicate tip

This tip is very similar to the following:

These tips need to be merged – see the merge guidelines.

Previous TipNext Tip

Tip: #29 - Reverse order of lines

Created: March 7, 2001 5:21 Complexity: intermediate Author: slimzhao--AT--21cn.com Version: 5.7 Karma: 43/27 Imported from: Tip#29

g/^/m0

well,

1. : bring you to command-line mode(also known as ex-mode) from normal-mode(also known as command mode).

2. g means you'll take an action through the whole file, generally perform a search, `v' also perform a search but it match the line not match the canonical expression.

3. / begins the regular express

4. ^ is a special character respect the start of a line.

5. the second / ends the regular express and indicate that the remains is action to do.

6. m means move, `t` and `co' for copy, `d' for delete

7. 0 is the destination line.


you can use

g/regexp/t$

to filter all lines and pick the match line together and copy them to the end of the buffer or

g/regexp/y A

to put them into a register(not eax, ebx...)

Comments

You can drop the '^' in the regexp, an empty regexp it will match any line, too:

g//m0


rufus--AT--o-town.de , April 16, 2002 6:12


actually an empty regex just matches the same thing as the previous regex, which will not necessarily match all lines.

Anonymous , August 16, 2003 13:54


A more intuitive solution is to use the unix "tac" utility, e.g. to reverse the entire file

%!tac

Anonymous , August 25, 2004 8:30


The tac utility isn't always available though. I've looked this tip up several times. Very handy!

Anonymous , November 21, 2005 17:36


great tip!

extended -- to reverse lines 100-150 :

100-150 g/^/m99

Incidentally, some versions installed out there still require the carrot (^).

-m

Anonymous , December 15, 2005 12:13


Advertisement