Vim Tips Wiki
No edit summary
 
(Change <tt> to <code>, perhaps also minor tweak.)
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
{{review}}
 
{{review}}
  +
{{TipImported
{{Tip
 
 
|id=758
 
|id=758
  +
|previous=757
|title=search and sort by selection
 
  +
|next=759
|created=July 12, 2004 20:51
+
|created=2004
 
|complexity=basic
 
|complexity=basic
|author=mosh cs albany
+
|author=mosh
 
|version=6.0
 
|version=6.0
 
|rating=12/5
 
|rating=12/5
  +
|category1=
|text=
 
  +
|category2=
Recently I discovered the potential uses of vmap ...
 
 
}}
 
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/&lt;S-Insert>/m0 to move matching lines out of the way, it gets tedious, so here's the automation using vmap:
 
#Select any part of the string with <code>v<move></code>
 
#Press 0, to move all matching lines to top of the file.
 
#Or press p, to see other matches.
 
#Or press $, to move junk to end of file.
   
  +
<pre>
 
" Sort by selection.
  +
:vmap 0 :<BS><BS><BS><BS><BS>g<M-x>\M&lt;S-Insert><M-x>m0<CR>
  +
:vmap $ :<BS><BS><BS><BS><BS>g<M-x>\M&lt;S-Insert><M-x>m$<CR>
  +
:vmap p :<BS><BS><BS><BS><BS>g<M-x>\M&lt;S-Insert><M-x>p<CR>
  +
</pre>
   
 
I used M-x as a delimiter and very non magic \M modifier to avoid errors on subtrings like 'c:/xyz/*.*'
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/&lt;S-Insert&gt;/m0
 
 
to move matching lines out of the way, it gets tedious,
 
 
so here's the automation using vmap:
 
 
 
 
With tip,
 
 
1. you select the any part of the string with
 
 
v&lt;move&gt;
 
 
2. then 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.
 
 
5. You figure out more uses.
 
 
 
 
" Sort by selection.
 
 
:vmap 0 :&lt;BS&gt;&lt;BS&gt;&lt;BS&gt;&lt;BS&gt;&lt;BS&gt;g&lt;M-x&gt;\M&lt;S-Insert&gt;&lt;M-x&gt;m0&lt;CR&gt;
 
 
:vmap $ :&lt;BS&gt;&lt;BS&gt;&lt;BS&gt;&lt;BS&gt;&lt;BS&gt;g&lt;M-x&gt;\M&lt;S-Insert&gt;&lt;M-x&gt;m$&lt;CR&gt;
 
 
:vmap p :&lt;BS&gt;&lt;BS&gt;&lt;BS&gt;&lt;BS&gt;&lt;BS&gt;g&lt;M-x&gt;\M&lt;S-Insert&gt;&lt;M-x&gt;p&lt;CR&gt;
 
 
 
 
I used M-x as a delimiter and very non magic \M modifier
 
 
to avoid errors on subtrings like 'c:/xyz/*.*'
 
 
 
 
Mosh
 
 
 
 
 
}}
 
 
== 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.
 
   
 
==Comments==
vmap m0 y:g&lt;M-x&gt;\M&lt;C-R&gt;"&lt;M-x&gt;m0&lt;CR&gt;
 
 
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 m$ y:g&lt;M-x&gt;\M&lt;C-R&gt;"&lt;M-x&gt;m$&lt;CR&gt;
 
vmap mp y:g&lt;M-x&gt;\M&lt;C-R&gt;"&lt;M-x&gt;p&lt;CR&gt;
 
   
  +
<pre>
 
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>
  +
</pre>
   
Breadman
 
, August 30, 2004 10:42
 
 
----
 
----
<!-- parsed by vimtips.py in 0.477628 seconds-->
 

Latest revision as of 05:46, 13 July 2012

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>