Vim Tips Wiki

Search and sort by selection

1,624pages on
this wiki
Add New Page
Talk0 Share
Tip 758 Printable Monobook Previous Next

created 2004 · complexity basic · author mosh · version 6.0

I need to sort log file by some substring (that cannot be precomputed, because it depends on data).

I would select substring with mouse, then press :g/<S-Insert>/m0 to move matching lines out of the way, it gets tedious, so here's the automation using vmap:

  1. Select any part of the string with v<move>
  2. Press 0, to move all matching lines to top of the file.
  3. Or press p, to see other matches.
  4. Or press $, to move junk to end of file.
" Sort by selection.
:vmap 0 :<BS><BS><BS><BS><BS>g<M-x>\M<S-Insert><M-x>m0<CR>
:vmap $ :<BS><BS><BS><BS><BS>g<M-x>\M<S-Insert><M-x>m$<CR>
:vmap p :<BS><BS><BS><BS><BS>g<M-x>\M<S-Insert><M-x>p<CR>

I used M-x as a delimiter and very non magic \M modifier to avoid errors on subtrings like 'c:/xyz/*.*'


The tip as given assumes that the selection is automatically copied to the clipboard. The following maps are more compatible, but modify the unnamed register. I also switched the maps to start with m, to preserve the original motions.

vmap m0 y:g<M-x>\M<C-R>"<M-x>m0<CR>
vmap m$ y:g<M-x>\M<C-R>"<M-x>m$<CR>
vmap mp y:g<M-x>\M<C-R>"<M-x>p<CR>

Ad blocker interference detected!

Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.

Also on Fandom

Random Wiki