vim, is it possible to add increasing numbers next to words each time the words are repeated in a text? -


let's assume have text file following content:

abrader                 ah b r ey d ah abrader                 ah b r ey d ah r abraders                ah b r ey d ah z abrades                 ah b r ey d z april                   ey p r ah l april                   ey p r ah  april                   ey ah p r ah  

i execute vim command looks duplicates , adds counter (inside brackets) in each word repeated, checks first word in each line. in current example, resulting text should be:

abrader                 ah b r ey d ah abrader(2)              ah b r ey d ah r abraders                ah b r ey d ah z abrades                 ah b r ey d z april                   ey p r ah l april(2)                ey p r ah  april(3)                ey ah p r ah  

here pure vim solution, modeled after @kent's awk solution (oneliner split here on multiple lines readability):

let d = {} | \   global/^\s/ \       let cw = expand('<cword>') | \       let d[cw] = get(d, cw, 0) + 1 | \       if d[cw] > 1 | \           execute 'normal! elgr(' . d[cw] . ')' | \       endif 

it counts occurrences of first word (<cword>; :global puts cursor @ beginning of each non-empty line (^\s)), , uses gr append count without affecting existing alignment.


Comments

Popular posts from this blog

Payment information shows nothing in one page checkout page magento -

tcpdump - How to check if server received packet (acknowledged) -