Lectura de archivos de texto DOS / Windows en 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.

¿Alguna idea?

preguntado el 09 de enero de 11 a las 09:01

2 Respuestas

  1. Añadir

    set fileformats=unix,dos,mac
    

    to your vimrc.

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

Respondido el 09 de enero de 11 a las 12:01

That is great! So you mean "set fileformats=unix,dos,mac" should do the job, but in my case it is failing to detect correct line endings? Why would it fail? I don't see anything hard in detecting line ends, especially for an advanced editor like Vim. - Rafid

@Rafid K. Abdullah If I remember correctly, it fails with mixed LF/CRLF line endings. - ZyX

" 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()

Respondido el 09 de enero de 11 a las 18:01

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.