vim中的文本文件格式---fileformat
?
当然,有的时候,还是没有彻底把^M干掉。终极要义是用正则表达式将他们替换掉。
First ensure you have read the file with the appropriate file format. For example, use :e ++ff=dos to remove all CRLF and LF-only line terminators, or use :e ++ff=mac if the file uses CR as a line terminator,.
After reading with the correct file format, the buffer may still contain unwanted CR characters. You can search for these with /\r (slash starts a search; backslash r represents CR when searching; press Enter to search).
To delete ^M at line endings, and replace it with a space everywhere else (the c flag will prompt to confirm that you want each replacement, and the e flag prevents an error message if the string is not found):
:%s/\r\+$//e:%s/\r/ /gce
To process, say, all *.txt files in the current directory:
vim *.txt:set hidden:bufdo %s/\r\+$//e:bufdo %s/\r/ /ge:xa
To delete every ^M, regardless of where they occur in a line (this is not a good idea if two lines were separated only by a CR because the command joins the lines together):
:%s/\r//g
To replace every CR with LF (when searching, \r matches CR, but when replacing, \r inserts LF; this is not a good idea if LF occurs at the end of a line, because an extra blank line will be created):
:%s/\r/\r/g
后半部分是处理mac文件的,很少有人会感同身受,大家自己看看吧。
If a file uses CR line terminators, it should be read as mac (using :e ++ff=mac). After doing that, you may see unwanted ^J (LF) characters. In a mac buffer, all CR characters will have been removed because CR is the line terminator, and searching for \r will find unwanted LF characters. Use these commands to remove ^J from the start of all lines, and to replace all other ^J with a line break:
%s/^\r//e%s/\r/\r/ge
?
?
CR is carriage return (return cursor to left margin), which is Ctrl-M or ^M or hex 0D.
LF is linefeed (move cursor down), which is Ctrl-J or ^J or hex 0A. Sometimes, LF is written as NL (newline).
Mac OS version 9 and earlier use mac line endings, while Mac OS X and later use unix line endings.