mardi 17 février 2015

Vim function to remove ctrl characters


Vote count:

0




Unfortunately I have to look at files that have a lot of control characters embedded, some network and app logs for example. Can't be bothered finding an appropriate viewer and want to remove them in VIM.


I started to write a function to remove them one by one, with the intent to put a while loop round off a list of hex or unicode characters once it was working.


Something odd is happening. In the first file I tried the function on there are 33000 ^@ and 2000 ^M in the file.



function! CleanCtrlChar()
"Remove Ctrl Chars from file eg where ^ @ is CtrlQ-@
:%s/^ @//ge
:%s/^ M//ge
endfunction


Executing the function, snippet below, reports removing ~2300 ^@ and less than a 1000 ^M when it is called but doesn't actually remove that many. From the command line %s/^@//ge removes all 33000.


Changing :%s/^ @//ge to :%s/^ @/ /ge actually replaces the quoted number of matches with a space.


Why does the function report that it removed matches but removes less than it reports?


Why won't the function remove all the matches? Is it parsing differently than from the command line?



asked 32 secs ago

Steve

104






Vim function to remove ctrl characters

Aucun commentaire:

Enregistrer un commentaire