Skip to main content
4 of 5
Didn’t need to use \ze
Rich
  • 32.6k
  • 4
  • 74
  • 141

You can do this with the following command:

:sort /^\(# \)\?/

This skips the # if it exists, and then sorts on the remainder of the line. However, the pattern matches every line, so all lines are sorted into a single group.

You will want to add a range to the start of the command to apply this to the appropriate lines in your file.

The following command also works, coming at it from the opposite direction:

:sort r /\(# \)\?\zs.*/

Instead of skipping the matched part of the line, this sorts on the matched part, but we use \zs to exclude the # from the match, if it exists.

Rich
  • 32.6k
  • 4
  • 74
  • 141