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.