arij.us
Digital Garden

Vim

Last updated 2024-01-02

Move lines matching pattern to the bottom of file

:g/[pattern]/m $

Example

I had a bash file with repeated patterns of:

_some_function () {
  # ...
}
alias some_function=_some_function

_another_function () {
  # ...
}
alias another_function=_another_function

Wanted to detach function declarations and aliases, so that file would look like:

_some_function () {
  # ...
}

_another_function () {
  # ...
}

alias some_function=_some_function
alias another_function=_another_function

I used the following command:

:g/^alias/m $

Function declarations remain untouched, while all aliases go down to the bottom of file.

The :g[lobal] command is neat! :help :global

Reverse order of lines

:g/^/m0