If I open files I created in Windows, the lines all end with ^M
.
How do I delete these characters all at once?
相关问题
- Is shmid returned by shmget() unique across proces
- What is the best way to do a search in a large fil
- how to get running process information in java?
- Spring Integration - Inbound file endpoint. How to
- Error building gcc 4.8.3 from source: libstdc++.so
I typically use
which seems a little odd, but works because of the way that vim matches linefeeds. I also find it easier to remember :)
You can use:
or
dos2unix utility
.I found a very easy way: Open the file with nano:
nano file.txt
Press CTRL+O to save, but before pressing Enter, press: ALT+D to toggle betwen DOS and Unix/Linux line-endings, or: ALT+M to toggle betwen Mac and Unix/Linux line-endings then press Enter to save and CTRL+X to quit.
You can use the following command:
:%s/^V^M//g
where the '^' means use CTRL key.
dos2unix is a commandline utility that will do this, or
:%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.CtrlM is the character
\r
, or carriage return, which DOS line endings add. CtrlV tells vim to insert a literal CtrlM character at the command line.Taken as a whole, this command replaces all
\r
with nothing, removing them from the ends of lines.