Vim Tips Wiki
Register
Advertisement
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/*.*'

Comments[]

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>

Advertisement