Rafid
Rafid

Reputation: 20189

Reading DOS/Windows Text File in Linux

As you all know, the line endings in DOS/Windows file differ than those of Unix/Linux, so whenever I open a file that I have created using Windows, I see hundreds of ^M after each line. Many people suggested solving this problem using:

set fileformat=dos

I tried this but it doesn't work, because as far as I noticed, it tells Vim how to "save" the file, rather than how to "read" the file. What I want is keep line-endings as they are, be they Linux, Windows, or MAC, read them correctly, and save the file using the same format aftr editing.

Any idea?

Upvotes: 1

Views: 1967

Answers (2)

SergioAraujo
SergioAraujo

Reputation: 11810

" put this in your ~/.vimrc, resource then and try :Dos2Unix
" dos2unix ^M
fun! Dos2unixFunction()
    let _s=@/
    let l = line(".")
    let c = col(".")
    try
        set ff=unix
        w!
        "%s/\%x0d$//e
    catch /E32:/
        echo "sorry, first save your file."
    endtry
    let @/=_s
    call cursor(l, c)
endfun
com! Dos2Unix keepjumps call Dos2unixFunction()

Upvotes: 0

ZyX
ZyX

Reputation: 53634

  1. Add

    set fileformats=unix,dos,mac
    

    to your vimrc.

  2. Run :e ++ff=dos in opened file if vim failed to detect correct line ending.

Upvotes: 3

Related Questions