Vim Tip I always forget

To get rid of all the crazy ^M’s that appear in files from windows boxes. In vim:


:%s/C-vC-m//g

(That’s Control-v, Control-m).

About Nathan Powell

I am a middle aged technologist freak-ball.
This entry was posted in sysadmin. Bookmark the permalink.

2 Responses to Vim Tip I always forget

  1. Jay says:

    or, from the command line:

    # dos2unix filename

    but I like the vim tip because not all boxes will have dos2unix (though I bet most do)

  2. Patrick says:

    Using an autocommand to switch the file format automatically from dos/mac to unix for source files works as well.

    autocmd FileType perl,pm,lisp,scheme,tcl,python,c setlocal ff=unix

    Another nicety strips off the trailing whitespace on writes:

    function! DeleteTrailingWS()
    normal mz
    %s/\s\+$//ge
    normal `z
    endfunction
    if has(“autocmd”)
    autocmd BufWrite *.py :call DeleteTrailingWS()
    endif