Vim Tips Wiki
(Adjust previous/next navigation + minor manual clean)
Line 61: Line 61:
 
:I'm inclined to agree with the implication lurking in your question, but sometimes style triumphs over utility and the cuteness is sufficient to justify this tip. However, I actually used the command from this the tip a month ago when I had a log of lines in order newest-first, and I needed it in the reverse order. [[User:JohnBeckett|JohnBeckett]] 01:24, January 8, 2010 (UTC)
 
:I'm inclined to agree with the implication lurking in your question, but sometimes style triumphs over utility and the cuteness is sufficient to justify this tip. However, I actually used the command from this the tip a month ago when I had a log of lines in order newest-first, and I needed it in the reverse order. [[User:JohnBeckett|JohnBeckett]] 01:24, January 8, 2010 (UTC)
 
:I think without a range it has very few real uses, but with a range (which could even be a visual selection with <tt>'<,'></tt>) it becomes much more useful. With minor tweaking, using an actual pattern for the <tt>:g</tt> command and a range on the <tt>:m</tt> command (which could contain searches, etc.) this simple command becomes extremely versatile for moving large blocks of text and placing in reverse order. --[[User:Fritzophrenic|Fritzophrenic]] 14:32, January 8, 2010 (UTC)
 
:I think without a range it has very few real uses, but with a range (which could even be a visual selection with <tt>'<,'></tt>) it becomes much more useful. With minor tweaking, using an actual pattern for the <tt>:g</tt> command and a range on the <tt>:m</tt> command (which could contain searches, etc.) this simple command becomes extremely versatile for moving large blocks of text and placing in reverse order. --[[User:Fritzophrenic|Fritzophrenic]] 14:32, January 8, 2010 (UTC)
  +
:I just want to say thank you very much for this hint. I use vim to organize and search my account log (Kontobewegungen) and I receive the account info as list newest first so this hint was very helpful for me. --[[User:Peter Schneider|pu.schneider@gmx.net]] 10:00, October 4, 2010 (UTC)

Revision as of 08:01, 4 October 2010

Tip 29 Printable Monobook Previous Next

created 2001 · complexity intermediate · author slimzhao · version 5.7


This command given in :help 12.4 will reverse all lines in the current buffer:

:g/^/m0
  1. : start command-line mode.
  2. g means you'll take an action on any lines where a regular expression matches
  3. / begins the regular expression (could have used any valid delimiter)
  4. ^ matches the start of a line (which matches all lines in the buffer)
  5. the second / ends the regular expression; the rest is an Ex command to execute on all matched lines (i.e. all lines in buffer)
  6. m means move (:help :move)
  7. 0 is the destination line (beginning of buffer)

This takes advantage of the implementation detail noted just below :help :v, that :g works by first marking all lines that match, then visiting each mark in order to run the command. Hence, you first move the first line to the top, then the second above the first, then the third above the second, etc.

If you do not want to reverse the entire file, you can supply the range of lines to reverse to the command and adjust the destination accordingly. For example, to reverse only lines 100-150:

:100,150g/^/m99

If you are running Vim on a Unix-like operating system, the tac utility will reverse all lines for you. Like any external utility, you can call tac from Vim as an alternate method:

:%!tac

A range will also work with this method:

:100,150!tac

Either of these methods can easily be assigned a mapping or Ex command.

For example,

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

It is probably more useful to reverse the lines in-place, i.e. without moving them to the top:

command! -bar -range=% Reverse <line1>,<line2>g/^/m<line1>-1

See also

Comments

OK I give up. Why would you want to do this?

I'm inclined to agree with the implication lurking in your question, but sometimes style triumphs over utility and the cuteness is sufficient to justify this tip. However, I actually used the command from this the tip a month ago when I had a log of lines in order newest-first, and I needed it in the reverse order. JohnBeckett 01:24, January 8, 2010 (UTC)
I think without a range it has very few real uses, but with a range (which could even be a visual selection with '<,'>) it becomes much more useful. With minor tweaking, using an actual pattern for the :g command and a range on the :m command (which could contain searches, etc.) this simple command becomes extremely versatile for moving large blocks of text and placing in reverse order. --Fritzophrenic 14:32, January 8, 2010 (UTC)
I just want to say thank you very much for this hint. I use vim to organize and search my account log (Kontobewegungen) and I receive the account info as list newest first so this hint was very helpful for me. --pu.schneider@gmx.net 10:00, October 4, 2010 (UTC)