vim reverse range of lines code example

Example: vim reverse range of lines

To reverse lines a to b in vim, use this template:
    :a,bg/^/ma_minus_1
Explanation:
  - : enters command lines mode
  - a,b gives the range of lines to reverse; omit reverse all lines
  - g says "apply command to all lines matching this regex"
  - /^/ is a regex matching the start of a line, which all lines have
  - m is the Ex command for "move"
  - a_minus_1 is the move destination, which needs to be one less than the start
      of the range to reverse the lines, so a_minus_1 = a - 1
Examples:
  - Reverse lines 10-20:
      :10,20g/^/m9
    a = 10, b = 20, a_minus_1 = 9
  - Reverse all lines in the buffer:
      :g/^/m0
    a = 1, b = (last line), a_minus_1 = 0

Tags:

Misc Example