For example, if I have four lines as follows:

the first line
the second line
the third line
the fourth line

I want to reverse them to

the fourth line
the third line
the second line
the first line

How could I do this in vim?

link|improve this question

73% accept rate
feedback

2 Answers

up vote 9 down vote accepted

To reverse all the lines in a file,

:g/^/m0

For an explanation see

:help 12.4

which also shows how to reverse just a range of lines.

link|improve this answer
feedback
" Reverse the lines of the whole file or a visually highlighted block.
    " :Rev is a shorter prefix you can use.
    " Adapted from http://tech.groups.yahoo.com/group/vim/message/34305
command! -nargs=0 -bar -range=% Reverse
    \       let save_mark_t = getpos("'t")
    \<bar>      <line2>kt
    \<bar>      exe "<line1>,<line2>g/^/m't"
    \<bar>  call setpos("'t", save_mark_t)

(Based on: http://tech.groups.yahoo.com/group/vim/message/34305 )

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.