Sunday, 2 August 2009
Clean up a Mucky Text File with a VIM function
We are often required to clean up a text file usually from kind of export to text from a database , Word file etc. These text files can be full of superfluous white space, non-asciis, control-M etc
Insert the following function into your .vimrc or load directly into the file you editing. Adapt it as necessary .
Execute with
: call Clean()
function! Clean()
" Clean up a text file
" delete pesky (MSDOS) control-M 's
exe ':%s/\r//ge'
" delete pesky non-asciis
exe ':%s/[\x00-\x1f\x80-\xff]/ /eg '
" compress multiple spaces
exe ':%s/\s\s\+/ /eg'
" delete end of line spaces
exe ':%s/\s\+$//e'
" compress multiple blank lines
exe ':silent! v/./,/./-j'
endfunction
Insert the following function into your .vimrc or load directly into the file you editing. Adapt it as necessary .
Execute with
: call Clean()
function! Clean()
" Clean up a text file
" delete pesky (MSDOS) control-M 's
exe ':%s/\r//ge'
" delete pesky non-asciis
exe ':%s/[\x00-\x1f\x80-\xff]/ /eg '
" compress multiple spaces
exe ':%s/\s\s\+/ /eg'
" delete end of line spaces
exe ':%s/\s\+$//e'
" compress multiple blank lines
exe ':silent! v/./,/./-j'
endfunction
Labels: gvim, text files, vim
Subscribe to Posts [Atom]
Post a Comment