Vim Tips Wiki
(merged in comments and similar tips)
(Adding categories)
Line 52: Line 52:
   
 
==Comments==
 
==Comments==
  +
[[Category:Usage]]

Revision as of 04:21, 31 March 2009

Tip 29 Printable Monobook Previous Next

created March 7, 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)

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

See also

Comments