Skip to content

Instantly share code, notes, and snippets.

@wellle
Last active July 24, 2020 09:34
Show Gist options
  • Save wellle/019365c2c6a124aa228ef765d3cb4221 to your computer and use it in GitHub Desktop.
Save wellle/019365c2c6a124aa228ef765d3cb4221 to your computer and use it in GitHub Desktop.
function! Group(char)
" open blank line on top of file and jump back
execute "normal! ggO\<Esc>\<C-O>"
" save current position
let p = getpos('.')
" from this line downward move all lines matching the pattern to top of file
.,$g//m0
" select all lines matching the pattern
normal! vip
" redirect output to standard register
redir @">
" count lines, words, bytes
execute "normal! g\<C-G>"
" stop redirecting output
redir END
" paste redirected output to top of file (includes blank line above)
execute "normal! \<Esc>ggO\<C-R>\"\<Esc>"
" find Lines/Words/Bytes in message
execute "normal! F" . a:char
" reduce message to something like "3547 Bytes"
" append search pattern and first example line, separated by literal tabs
execute "normal! 3bd0wd2weCs \<C-V>\<Tab>\<C-R>/ \<C-V>\<Tab>\<Esc>J"
" delete all other lines matching the pattern
silent normal! }d3Gggdd
" restore cursor position
call setpos('.', p)
endfunction
" count lines/words/bytes
nnoremap <silent> <leader>l :call Group('L')<CR>
nnoremap <silent> <leader>w :call Group('W')<CR>
nnoremap <silent> <leader>b :call Group('B')<CR>
@wellle
Copy link
Author

wellle commented Jun 14, 2018

Preparation: Put this code into your vimrc. If you can't, put this file anywhere, then inside Vim call :source group.vim.

Usage: In Vim search for a pattern you want to group by, put your cursor on the line of the first match (or above, because it only groups from the cursor line downwards), then type \l, \w or \b to group all lines matching this pattern into a single line starting with the number total number of lines, words or bytes of all lines matching the pattern. The line will also contain the search pattern and the first example from the file, all separated by tabs.

Repeat this process with different patterns until all lines have been matched by one pattern. Now you can just sort all lines numerically (type !ipsort -nr) and you see which pattern used the most lines, words or bytes.

For easier navigation I'd suggest to put a blank line at the top of the file before starting. That might make it easier to spot where the raw data starts below the already grouped lines.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment