Ccna final exam - java, php, javascript, ios, cshap all in one. This is a collaboratively edited question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.
Wednesday, May 23, 2012
How do I convert dos files to linux files in vim?
If I open files I created in windows, the lines all end with ^M.
You can also use mac or dos to respectively convert your file to macintosh or MS-DOS/MS-Windows file convention. And it does nothing if the file is already in the correct format.
Usually there is a dos2unix command you can use for this, just make sure you read the manual as the GNU and BSD versions differ on how they deal with the arguments.
# BSD version dos2unix $FILENAME $FILENAME_OUT mv $FILENAME_OUT $FILENAME
#GNU version dos2unix $FILENAME
Alternatively, you can create your own dos2unix with any of the proposed answers here, for example:
Following steps can convert the file format for dos to unix:
:e ++ff=dos Edit file again, using dos file format ('fileformats' is ignored).[A 1] :setlocal ff=unix This buffer will use LF-only line endings when written.[A 2] :w Write buffer using unix (LF-only) line endings.
dos2unix is a commandline utility that will do this, or
ReplyDelete:%s/^M//g
will if you use ctrl-v ctrl-m to input the ^M. Or you can:
:set ff=unix
and vim will do it for you. Docs on the 'fileformat' setting are here, and the vim wiki has a comprehensive page on line ending conversions.
Alternately, if you move files back and forth a lot, you might not want to convert them, but rather to do
:set ff=dos
so vim will know it's a DOS file and use DOS conventions for line endings.
Change the lineendings in the view:
ReplyDelete:e ++ff=dos
:e ++ff=mac
:e ++ff=unix
This can also be used as saving operation (:w alone will not save using the lineendings you see on screen):
:w ++ff=dos
:w ++ff=mac
:w ++ff=unix
And you can use it from the command-line:
for file in $(ls *cpp)
do
vi +':w ++ff=unix' +':q' ${file}
done
I prefer to use the following command :
ReplyDelete:set fileformat=unix
You can also use mac or dos to respectively convert your file to macintosh or MS-DOS/MS-Windows file convention. And it does nothing if the file is already in the correct format.
For more information, see the vim help :
:help fileformat
:%s/\r+//g
ReplyDeleteIn Vim, that strips all carriage returns, and leaves only newlines.
I typically use
ReplyDelete:%s/\r/\r/g
which seems a little odd, but works because of the way that vim matches linefeeds. I also find it easier to remember :)
from: http://vim.wikia.com/wiki/Change_end-of-line_format_for_dos-mac-unix
ReplyDelete[Esc] :%s/\r$//
:set fileformat=unix to convert from dos to unix.
ReplyDeleteUsually there is a dos2unix command you can use for this, just make sure you read the manual as the GNU and BSD versions differ on how they deal with the arguments.
ReplyDelete# BSD version
dos2unix $FILENAME $FILENAME_OUT
mv $FILENAME_OUT $FILENAME
#GNU version
dos2unix $FILENAME
Alternatively, you can create your own dos2unix with any of the proposed answers here, for example:
function dos2unix(){
[ "${!}" ] && [ -f "{$1}" ] || return 1;
{ echo ':set ff=unix';
echo ':wq';
} | vim "${1}";
}
:g/cntl-vcntl-m/s///
ReplyDeleteYou can use the following command:
ReplyDelete:%s/^V^M//g
where the '^' means use "Ctrl" key.
With the following command:
ReplyDelete:%s/^M$//g
Get the ^M to appear type Ctrl-V then Ctrl-M. Ctrl-V tells Vim to take the next character entered literally.
dos2unix can directly modify the file contents.
ReplyDeleteyou can diretly use on the file. no need for temp file redirection
dos2unix input.txt input.txt
the above uses the assumed US keyboard
use -437 option to use the UK keyboard.
dos2uinx -437 input.txt input.txt
in solaris we can use dd command also.
Following steps can convert the file format for dos to unix:
ReplyDelete:e ++ff=dos Edit file again, using dos file format ('fileformats' is ignored).[A 1]
:setlocal ff=unix This buffer will use LF-only line endings when written.[A 2]
:w Write buffer using unix (LF-only) line endings.
Reference: http://vim.wikia.com/wiki/Change_end-of-line_format_for_dos-mac-unix