Dealing with \r \n ^M ^@ in text files using vim

南笙酒味 提交于 2020-01-13 19:14:32

问题


When I save lines of data in excel files as tab delimited .txt files, and then open those files in VIM, I see that what was once a multi-line file in excel is now a single line file in VIM.

The "lines" can be separated in VIM using some substitution commands:

%s/^M/\r\n/g

After this, the "lines" are now separated by an ^@. I deal with it using another substitution command:

%s/^@//g

My questions are:

  1. Why do my multi-line txt excel files open as a single line in VI?
  2. What is ^@?
  3. Is there a better way to 'fix' my txt files?

回答1:


Try this command:

    :%s/^M/\r/g

\r is the carriage return character vim uses. The ^M character is a newline character that is literally displayed.




回答2:


You can often fix problems like this by running the following command:

dos2unix FILE_NAME

This will re-format the file's newline characters in place (ie it will modify your file).

(This command does not exist on Mac OS X)


If you don't have dos2unix (eg you're on a Mac), you can just use sed:

sed 's/^M$//' input.txt > output.txt

You can also use sed -i if you want to avoid creating a new file by performing the substitution in place.

You can enter ^M by typing CTRLV followed by CTRLM

More reading here



来源:https://stackoverflow.com/questions/13727248/dealing-with-r-n-m-in-text-files-using-vim

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!