How do I sort lines using only the text under a visual column?
Answer
:'<,'>sort /\%V.*$/
Explanation
When you need to sort records by a substring that starts at a visual column, Vim can do it without external tools. The \%V atom restricts a search pattern to the current Visual area, so :sort can use that region as the key. This is useful when you have fixed-width tables, aligned logs, or padded output where the meaningful field starts in a specific column.
How it works
:'<,'>tells Vim to run the command on the lines of the last Visual selection.:sort /.../sorts the range by the text matched by the pattern, not just by full-line start.\%Vlimits matching to the Visual area, so the sort key begins where your selection starts..*$captures the rest of the line from that selected column onward.
Example
Select the same key column across these lines, then run the command:
ID001 sev=3 host=zeta
ID002 sev=1 host=beta
ID003 sev=2 host=alpha
After sorting by the selected host=... column:
ID003 sev=2 host=alpha
ID002 sev=1 host=beta
ID001 sev=3 host=zeta
Tips
- Use Visual Block (
<C-v>) for precise column boundaries before running:sort. - Add
!(:sort!) to reverse the order. - Pair with
:set virtualedit=blockwhen columns are ragged and you need tighter block control.