Reading DOS/Windows Text File in Linux
-
08-10-2019 - |
Question
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?
Solution
Add
set fileformats=unix,dos,mac
to your vimrc.
Run
:e ++ff=dos
in opened file if vim failed to detect correct line ending.
OTHER TIPS
" 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()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow